Why are my [code] blocks being re-formatted by WP?
-
I’m doing my best to use the [code] tag offered by WP to display custom C# code in my posts. Information on this tag can be found here: http://en.support.wordpress.com/code/posting-source-code/
For some reason, WP regularly re-formats my painstakingly hand-formatted code and add in HTML entities in place of my line breaks, quotes, or other special characters. WP also really loves to condense all of my code to a single line that is unreadable.
I'm unsure of why this is happening, and I'm finding it difficult to avoid. I'd really appreciate any help that can be offered in avoiding this problem or any information on using a different method of displaying code entirely. Otherwise, it's probably going to drive me insane.
The blog I need help with is: (visible only to logged in users)
-
Hi Adam,
Can you post a draft where the code isn’t working as expected?
I saved this example draft: https://mecwarriors.wordpress.com/wp-admin/post.php?post=284&action=edit&message=10 (note: not published!)
The
[code]{insert code here}[/code]shortcode is appearing as I’d expect. Please note that this isn’t the HTML code tag, but a specific bracket-enclosed shortcode for WordPress.com.Thanks!
-
Here is an example of code I might use:
————–
[code language="csharp" light="true"]
public AnimationClip walkAnimation;public float walkMaxAnimationSpeed = 0.75f;
private Animation _animation;
[/code]
————–But when WP adds formatting to my code it might end up like this:
————–
[code language="csharp" light="true"]public AnimationClip walkAnimation;public float walkMaxAnimationSpeed = 0.75f;
private Animation _animation;
[/code]
————–
(Except it would be all on one line.)This formatting usually occurs when I change something in the post or click the Save Draft button and reload my draft. I can’t pinpoint the exact circumstances, but it does happen often. If I don’t go back and fix the code afterward, it shows up incorrectly in the published post.
-
Oh, crap. The HTML got removed from the example: Here’s the WP-formatted code. I added the ‘ symbol so the could wouldn’t be used as formatting in the post this time.
[code language="csharp" light="true"]&l't;b'r /&g't;&l't;b'r /&g't;public AnimationClip walkAnimation;&l't;b'r /&g't;&l't;b'r /&g't;public float walkMaxAnimationSpeed = 0.75f;&l't;b'r /&g't;&l't;b'r /&g't;&l't;b'r /&g't;&l't;b'r /&g't;private Animation _animation;&l't;b'r /&g't;&l't;b'r /&g't;[/code]
-
Are you using the Visual Editor, the Text Editor or both?
I’m test a few different scenarios to see if I can duplicate, but my initial thought is a switch between the two editors may be a factor?
Thanks for your patience and your willingness to help troubleshoot.
-
Yes, I do use both. I mostly use the Visual Editor, but sometimes I go into the Text Editor to add in-line styling to text blocks in my posts.
-
Hi Adam,
A couple members of the team here and myself haven’t been able to “break” the code shortcode yet. Can you add the code you’d like to post in a new page/post on your site and save it as a draft?
Ideally, if you could upload a screenshot to your media library right before you do anything after adding the text (in case it borks again for you), that would be extremely helpful to help me figure out what is tripping it up for you—might be something like a specific string is tripping the regex process we’re using—so need a bit more to determine if I can duplicate it.
I’ve been using example of C# from MSDN to test, without success in breaking it yet.
Looking at your example below, I see broken br / html tags. Are you manually adding the tag or is that being included as part of the failed formatted?
Thanks again!
-
Hi again,
I think I was finally able to reproduce a variation of it.
When I added this test code: https://cloudup.com/c6PLvJpT0h4
It worked fine switching between Text and Visual and publishing the post: (example post, with line breaks added before publishing to check that) http://kraftsandbox.com/2013/11/06/chrome-test/If after publishing the post, I switched from the Text Editor to Visual, it returned with:
https://cloudup.com/cCIwhJ1wBUR , which would obviously failed when any changes were saved.That isn’t quite like what you described, but getting closer at least. Does this experience match what you’ve been seeing?
If so, in the meantime while we track down the actual bug, would adding the code in the Text view as the last step before saving and only editing the post in Text view after that be a possibility?
Thanks again for your patience.
-
Yeah, it’s not quite the same, but what you’ve recreated seems to be along the same lines as my issue.
To answer your previous question, I wrote in the html tags in my example code just as a visual representation of what was going on.
I made a post on my site titled “[code] test for WP" that you can take a look at. (It is published with a password "code", just to keep it out of circulation.) I copied some of my previous problem code and my expectations of how it might be reformatted if we can recreate the problem. The screenshot you requested is in my Media titled "codeTest".
So far I've been unable to figure out where the problem occurs, but I'll let you know if I come across it.
I also neglected to mention before that I'm using Firefox, if that changes anything..
-
Adam,
Thanks a ton for the helpful information. With that post, I can’t duplicate the issue now—only able to break the shortcode when using code that could be seen as an html tag and switching the editor immediately after publishing/updating.
Is the code appeared as expected for you currently? Switching editors making an impact? It is working for in Chrome and Firefox (latest post on OS X). If it is still failing for you, I’ll grab my wife’s computer to test on Win8.
Thanks!
-
I’m unable to reproduce the issue with the new post, so I’m afraid I don’t have much I can give you to go on. I’m sure I’ll be using the code tag again within the next week, so if I run across anything I’ll be sure to let you know.
Thanks for all the effort you’ve put into this so far. I hope the problem appears again so it can be looked at in more detail.
- The topic ‘Why are my [code] blocks being re-formatted by WP?’ is closed to new replies.