Code escaping keeps breaking my code snippets

  • I am toying around with wordpress to see whether I can post some code related things with it and found issues with editing of posts which do have code in them.

    I created a new post and pasted the xml code from this page https://gist.githubusercontent.com/Seikilos/a07d9af22181b0262440/raw/698cb92d9ae45f634e58175d29dd94f99d9a6900/newestfiles.msbuild.xml

    in the visual editor. The result is this entry
    https://thingsiaskmyself.wordpress.com/2015/07/21/first-paste-of-xml-code/
    It is displayed correctly.

    How ever if I decide to edit this entry and save it, it gets escaped too much:
    https://thingsiaskmyself.wordpress.com/2015/07/21/test-one/

    When I edit the initial post, every bracket is already escaped as <
    If I was in the Text and not Visual Editor I still have those < in there, so saving it, breaks my content persistently.

    I disabled WordPress should correct invalidly nested XHTML automatically just in case.

    I changed revisions to v1 (the initial paste) but this is broken too.

    Regarding code paste wordpress gives me a hard time here.

    The blog I need help with is: (visible only to logged in users)

  • I have had this issue before, and it can be frustrating. However, there is a solution! Check out the shortcode used for wrapping a number of different code formats.

    [code language="xml"]
    Your code goes here
    [/code]

    I just tested it with the XML in question, and was able to post, edit, and re-edit without errors.

    https://en.support.wordpress.com/code/posting-source-code/

    Hope this helps!

  • Note: You use that [code…] shortcode in the Visual Editor — no need to use the Text / HTML view.

  • I have used short code, it is not the tags that break the behaviour for me.

    I created a new post, ensured the editor is visual and pasted this:

    In visual editor
    
    [code language="xml"]
    
    <some><xml /></some>
    
    [/code]

    Then reload the edit-view by leaving it, so that the WYSIWYG editor is no longer loaded (e.g. click on “My Site” on top left)
    Then click on Blog Posts and select the post you’ve just made.
    In my instance I see now:

    In visual editor
    
    [code language="xml"]
    
    <some><xml /></some>
    
    [/code]

    Saving this totally kills my formatting then.

  • Edit: The second one is of course broken, it looks like

    & lt;some& gt;& lt;xml /& gt;& lt;/some& gt;

    without the whitespaces of course.

  • Thanks for the explanation.

    I was able to get this to behave with both your original github XML and the small snippet in your explanation. There must be one thing we’re doing differently.

    When you paste your XML into the visual editor, are you clicking on “Paste as Plain Text” toolbar button first? Please try that.

  • Paste as plain breaks it too.
    Furthermore the small code snippet I’ve posted above was handwritten, no copy paste.

    Are you saying that your online instance of WP is not showing this behaviour? Which browser are you using? I am current only latest Firefox.

  • Yes, it’s a WordPress.com site I use mostly to test things and help people out. I switched it to the P2 theme, which seems to be what your site is using, and reproduced the steps you outlined. This includes leaving the Edit page and coming back in to edit the same post later.

    I noted that when I come back to edit the page, the block of text (including the shortcode) is formatted as “Preformatted.” That makes sense, as it prevents any special characters from being escaped.

    The thing is, I did not select “Preformatted” on my own — the editor did so because of the shortcode.

    When you return to your edit screen, is the content block styled as “Preformatted,” or something else? In my case, even if I try to change it to some other style like “Paragraph,” the editor fixes the shortcode block back to “Preformatted” as soon as I update the post.

    https://gamebuilders.wordpress.com/2015/07/21/code-sample/

  • I can confirm that the code part of the post is set to preformatted when I return.

    However when I create a new page pasting xml code the editor automatically switches to preformatted style in the [code] segment.

    Since this forum does not allow to upload anything, here are some screenshots:

    Original post
    https://thingsiaskmyself.files.wordpress.com/2015/07/orig.png
    Reloaded broken one
    https://thingsiaskmyself.files.wordpress.com/2015/07/broken.png

    Just in case: I did this from both WordPress backends, the one that comes with the blog and the general wp-admin area thing.
    Both break my code.

  • The topic ‘Code escaping keeps breaking my code snippets’ is closed to new replies.