Title: WordPress PHP code in post content BUG
Last modified: November 20, 2016

---

# WordPress PHP code in post content BUG

 *  [crosp](https://wordpress.org/support/users/crosp/)
 * (@crosp)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/wordpress-php-code-in-post-content-bug/)
 * _[ Moderator note: [moved to How-to and Troubleshooting](https://wordpress.org/support/topic/wheres-my-topic-gone?replies=1&view=all).]_
 * Hi,
 * I have just found interesting bug
 * Try to create post with following content
 *     ```
       1. Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
       2. Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
   
       3. Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
       4. Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
   
       <?php
       //This callback registers our plug-in
       echo "HELLO I AM BUG";
       function wpse72394_register_tinymce_plugin($plugin_array) {
           $plugin_array['wpse72394_button'] = 'path/to/shortcode.js';
           return $plugin_array;
       }
   
       //This callback adds our button to the toolbar
       function wpse72394_add_tinymce_button($buttons) {
                   //Add the button ID to the $button array
           $buttons[] = "wpse72394_button";
           return $buttons;
       }
   
       5. Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
   
       6. Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
       ```
   

Viewing 8 replies - 1 through 8 (of 8 total)

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [9 years, 7 months ago](https://wordpress.org/support/topic/wordpress-php-code-in-post-content-bug/#post-8457750)
 * You can’t just drop PHP into the editor and have it work in post.
 * Are you trying to *show* PHP code or execute it?
 *  Thread Starter [crosp](https://wordpress.org/support/users/crosp/)
 * (@crosp)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/wordpress-php-code-in-post-content-bug/#post-8457767)
 * I don’t want it to work. I have good understanding of what is happening in browser,
   server side….
    Please see my updated post, I’ve accidentally submitted the post,
   and I have no way to edit it.
 *  Thread Starter [crosp](https://wordpress.org/support/users/crosp/)
 * (@crosp)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/wordpress-php-code-in-post-content-bug/#post-8457755)
 * I got following result
 * ![Unclosed PHP tag](http://imgur.com/a/UJnmQ)
 * In this case php tag is unclosed, and everything after code disappears, so we
   won’t see neither 5 nor 6 sentence.
 * In case of wrapping this code inside `code` tag like that
 *     ```
       <code><?php
       //This callback registers our plug-in
       echo "HELLO I AM BUG";
       function wpse72394_register_tinymce_plugin($plugin_array) {
           $plugin_array['wpse72394_button'] = 'path/to/shortcode.js';
           return $plugin_array;
       }
   
       //This callback adds our button to the toolbar
       function wpse72394_add_tinymce_button($buttons) {
                   //Add the button ID to the $button array
           $buttons[] = "wpse72394_button";
           return $buttons;
       }</code>
       ```
   
 * The result is following
 * ![Wrapping inside code tag](http://imgur.com/a/508W0)
 * And if I close php tag it will be displayed without this code here it is
 * ![With closing tag](http://imgur.com/a/s5fMc)
 * You may say you have wrap the code into HTML/CSS tags.
 * I am currently on the stage of creating my own plugin for syntax highlighting,
   but here is an example of formatted code
 *     ```
       <span class="cp"><?php</span>
       </span><span class="c1">//This callback registers our plug-in</span>
       <span class="hll"><span class="k">function</span> <span class="nf">wpse72394_register_tinymce_plugin</span><span class="p">(</span><span class="nv">$plugin_array</span><span class="p">)</span> <span class="p">{</span>
       </span>    <span class="nv">$plugin_array</span><span class="p">[</span><span class="s1">'wpse72394_button'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'path/to/shortcode.js'</span><span class="p">;</span>
           <span class="k">return</span> <span class="nv">$plugin_array</span><span class="p">;</span>
       <span class="p">}</span> 
       ```
   
 * Please note `<span class="cp"><?php</span>` but STILL problem exists even if 
   php tag is wrapped inside `span` tag in this case.
 * I thought that it would be problem with some plugins that use `the_content` filter,
   but I’ve just set up new installation of WordPress completely clean (all examples
   were tested with it).
 * Is this is problem with WordPress or maybe browser.
 * Please share your thoughts.
 * Thanks
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [9 years, 7 months ago](https://wordpress.org/support/topic/wordpress-php-code-in-post-content-bug/#post-8457967)
 * It is still unclear what you’re trying to accomplish. Are you tring to execute
   PHP code in-line in the content? If so, the best way to do that is to put your
   code into a shortcode then put the shortcode into the editor.
 *  Thread Starter [crosp](https://wordpress.org/support/users/crosp/)
 * (@crosp)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/wordpress-php-code-in-post-content-bug/#post-8457995)
 * Thank you for you reply, I am not trying to execute php code, I just want to 
   include in my post and in my case with the help of my plugin to highlight.
    For
   example post explaining how to build wordpress plugin. I need to include some
   code snippets, consider my example and if I miss PHP end tag (?>) than I will
   get weird behavior as described earlier.
 *  Thread Starter [crosp](https://wordpress.org/support/users/crosp/)
 * (@crosp)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/wordpress-php-code-in-post-content-bug/#post-8458006)
 * I am doing exactly as you’ve suggested. I using custom shortcode in text mode(
   because in visual mode TinyMCE will automatically add html markup to the code,
   but I don’t need this because my plugin will do this work)
 *     ```
       [code-highlight line-numbers="table" linenostart="53" highlight-lines="1,3,8" style="native" lang="html+php" pyg-id="1" ]
       <?php
       //This callback registers our plug-in
       function wpse72394_register_tinymce_plugin($plugin_array) {
           $plugin_array['wpse72394_button'] = 'path/to/shortcode.js';
           return $plugin_array;
       }
   
       //This callback adds our button to the toolbar
       function wpse72394_add_tinymce_button($buttons) {
                   //Add the button ID to the $button array
           $buttons[] = "wpse72394_button";
           return $buttons;
       }
       ?>
       [/code-highlight]
       ```
   
 * Here is an example of full my code, it is displayed correctly but in case of 
   missing closing tag the behavior is the same. Next part of the post after the
   code snippet with unclosed tag is missed.
    As I’ve already explained even if 
   php tag inside `span` or any other tag without closing tag it doesn’t work.
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [9 years, 7 months ago](https://wordpress.org/support/topic/wordpress-php-code-in-post-content-bug/#post-8458047)
 * OK, got it. you want to show code in a post. So it seems that the issue is either
   with what’s passed to your shortcode or what your shortcode does with it. If 
   you dump $content inside the shortcode, is it all there?
 *  Thread Starter [crosp](https://wordpress.org/support/users/crosp/)
 * (@crosp)
 * [9 years, 7 months ago](https://wordpress.org/support/topic/wordpress-php-code-in-post-content-bug/#post-8458127)
 * Thanks for answer. As far as I am creating my own plugin, I have done $content
   dump a lot of times.
    Lets clarify all moments. I am using Text Editor for post,
   not Visual, because Visual one adds some markup. 1. As I’ve described, to be 
   sure that my plugin or any other plugin can cause this problem, I installed pure
   wordpress distro. But the problem still exists. Without any plugin. You can try
   by your own. If tag is not closed all content after code snippet starts to look
   weird.
 * 2. Here are images that I posted earlier (but inserted incorrect link)
 *  First :
    ⌊Unclosed PHP tag⌉
 *  Second:
    ⌊Wrapping inside code tag⌉ Third: ⌊With closed tag⌉
 * 3. My plugin just takes code between shortcodes, processes it, saves to db and
   than output formatted code.
 *  Here is unformatted code :
 *     ```
         [code-highlight line-numbers="table" linenostart="53" highlight-lines="1,3,8" style="native" lang="html+php" pyg-id="1" ]
       <?php
       //This callback registers our plug-in
       function wpse72394_register_tinymce_plugin($plugin_array) {
           $plugin_array['wpse72394_button'] = 'path/to/shortcode.js';
           return $plugin_array;
       }
   
       //This callback adds our button to the toolbar
       function wpse72394_add_tinymce_button($buttons) {
                   //Add the button ID to the $button array
           $buttons[] = "wpse72394_button";
           return $buttons;
       }
       ?>[/code-highlight]
       ```
   
 * And now formatted code that goes directly to the output
 *     ```
       <table class="highlighted-source native html-php table"><tr><td class="linenos"><div class="linenodiv"><pre>53
       54
       55
       56
       57
       58
       59
       60
       61
       62
       63
       64
       65
       66</pre></div></td><td class="code"><div class="highlighted-source native html-php "><pre><span></span><span class="hll"><span class="cp"><?php</span>
       </span><span class="c1">//This callback registers our plug-in</span>
       <span class="hll"><span class="k">function</span> <span class="nf">wpse72394_register_tinymce_plugin</span><span class="p">(</span><span class="nv">$plugin_array</span><span class="p">)</span> <span class="p">{</span>
       </span>    <span class="nv">$plugin_array</span><span class="p">[</span><span class="s1">'wpse72394_button'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'path/to/shortcode.js'</span><span class="p">;</span>
           <span class="k">return</span> <span class="nv">$plugin_array</span><span class="p">;</span>
       <span class="p">}</span>
   
       <span class="hll"><span class="c1">//This callback adds our button to the toolbar</span>
       </span><span class="k">function</span> <span class="nf">wpse72394_add_tinymce_button</span><span class="p">(</span><span class="nv">$buttons</span><span class="p">)</span> <span class="p">{</span>
                   <span class="c1">//Add the button ID to the $button array</span>
           <span class="nv">$buttons</span><span class="p">[]</span> <span class="o">=</span> <span class="s2">&quot;wpse72394_button&quot;</span><span class="p">;</span>
           <span class="k">return</span> <span class="nv">$buttons</span><span class="p">;</span>
       <span class="p">}</span>
       <span class="cp">?></span>
       </pre></div>
       </td></tr></table>
       ```
   
 * This code works because of `<span class="cp">?></span>`
    Again this problem is
   not in my plugin.
 * 4. It doesn’t work even with following HTML `<span><</span><span>?</span>php</
   span>
    </span>` content is just missing, only one line after code snippet is 
   present, no html at all than.
 * Maybe I am going crazy but I have no idea what is wrong. I am not aware of each
   core part of WordPress maybe there is core filter that tries to fix unclosed 
   tags ..
 * _[Moderator note: Please, [no bumping](https://codex.wordpress.org/Forum_Welcome#No_Bumping).]_
    -  This reply was modified 9 years, 7 months ago by [crosp](https://wordpress.org/support/users/crosp/).
    -  This reply was modified 9 years, 7 months ago by [crosp](https://wordpress.org/support/users/crosp/).
    -  This reply was modified 9 years, 7 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘WordPress PHP code in post content BUG’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 2 participants
 * Last reply from: [crosp](https://wordpress.org/support/users/crosp/)
 * Last activity: [9 years, 7 months ago](https://wordpress.org/support/topic/wordpress-php-code-in-post-content-bug/#post-8458127)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
