I installed a syntax highlighter plugin so I could post and highlight code snippets.

My problem is wordpress is converting special characters in my code (e.g. from & to &), when I really just want it to leave my code alone.


I tracked this solution down from the WordPress Trac ticket found here: http://core.trac.wordpress.org/ticket/6969

The solutions seems to be to add this to your functions.php file:

function foobar_run_shortcode( $content ) {
    global $shortcode_tags;

    // Backup current registered shortcodes and clear them all out
    $orig_shortcode_tags = $shortcode_tags;

    add_shortcode( 'foobar', 'shortcode_foobar' );

    // Do the shortcode (only the one above is registered)
    $content = do_shortcode( $content );

    // Put the original shortcodes back
    $shortcode_tags = $orig_shortcode_tags;

    return $content;

add_filter( 'the_content', 'foobar_run_shortcode', 7 ); ?>

Source: http://www.viper007bond.com/2009/11/22/wordpress-code-earlier-shortcodes/


I've tried the solution above, but I've not had any luck getting it to work. I've actually being trying different methods for the same problem with some luck, but none of them really work 100% without completely removing wp_autop and wp_texturize. I have found a way, but it takes an extra step. I'll post my solution in case your interested.

My Way:

  1. First copy your php code
  2. Go to http://quickhighlighter.com/
  3. Paste your code into the textarea
  4. Click Highlight!
  5. When the code pops up, click the "Toggle Code View" link
  6. Copy the Code (not the css, but the HTML)
  7. Go to your post edit screen and select the post your adding the code to
  8. Click on HTML View
  9. Paste the code into the editor and click save (switching from HTML to Visual won't break the code since it's in HTML format and the characters have already been converted) The CSS Provided by the highlighter doesn't quite work (it isn't wrapped, and is full page).

You can use the CSS I've put together if you like: http://snipplr.com/view/58362/wordpress-code-highlighter-css/

|improve this answer|||||

After trying everything under the sun to fix this issue, I was able to fix it by simply installing an older version of WP. You can easily install and use a WP plugin to do the downgrading. FWIW, I used a plugin called WP Downgrade. You can find it at wordpress.org.

After installing the plugin, it'll ask you for the WP version to downgrade to. (I went with 4.9.8 since I'm used to editing WP sites with this version). Hit the install. This will then show you a confirmation 'core' button. You then click this and confirm the install at the 'core' page. Very easy to do and the problem was solved in 5 ~ 10 minutes (ex: 1. Finding and installing the wp plugin. 2. Type in the wp version you want to downgrade to. 3. Click in the install button and 'core' install button.)

|improve this answer|||||

To people coming from a Google search, using the Crayon Syntax Highlighter and experiencing the same issue as noted in the OP:

Go to the Settings of Crayon Syntax Highlighter and tick on the option Decode HTML entities in code. This solves the issue.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.