Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

NOTE I don't want the php code to be executed, only displayed.

I want to display this php code <?php wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); ?> inside a post. I cannot make it work.

If I go to text type of editor and type <pre class="lang:php nums:false"> <?php wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); ?> </pre> there and save. I can see what I want to.

But when I switch to visual editor the php text is not there. There is box for it but the text is not there. If I save the post now html comments are added to the source code and it looks like <pre class="lang:php nums:false"> <!--?php wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); ?--></pre>. So when I then refresh the post page I got !-- characters there, something that shouldn't be there.

Any idea how to display php code with syntax highlight?

share|improve this question
    
Stay in the HTML editor and don't switch back and forth. –  Wyck Sep 30 '13 at 4:47
    
How do you then set up that is a code and div the pre tag a class? –  Radek Sep 30 '13 at 4:56

2 Answers 2

That may be a bit problematic. I use the plugin SyntaxHighlighter Evolved, but as the plugin description says (my emphasis):

TIP: Don't use the Visual editor if you don't want your code mangled. TinyMCE will "clean up" your HTML.

Another option is to embed Gists (or similar) using a Shortcode to pass the URL and let PHP render the script tag (as suggested by Marco Berrocal).

<script src="https://gist.github.com/user/GistID.js"></script>

And the Shortcode path could also be used with Custom Fields. Paste the script in the CF and pass its name to the Shortcode for rendering. The plugin Advanced Custom Fields comes to mind.

share|improve this answer

If I were you, I would do a custom Shortcode, clean the PHP and display it on the way out.

share|improve this answer
    
It sounds bit too complicated to me. –  Radek Sep 30 '13 at 4:59
    
Then perhaps you could look into a plug in? Sorry. –  Marco Berrocal Sep 30 '13 at 5:07
    
Marco, try to provide more details in an Answer, otherwise it's better to just make comment. @Radek, a shortcode is quite simple, much more simple than insert PHP in the visual editor. Research "conditional shortcode script" here in the archives –  brasofilo Sep 30 '13 at 5:20
    
@brasofilo I would comment but don't have privileges for this yet (need 50 rep). –  Marco Berrocal Sep 30 '13 at 5:23
    
@brasofilo: not sure if I understood if shortcode could be a solution but what I don't want the php code to be executed, only displayed. –  Radek Sep 30 '13 at 5:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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