I’m running some PHP code on my WordPress site and when the PHP runs to echo out some information it doesn’t echo it in the right place. An abbreviated version of the code is:
<div class="description cms">
<div id="home_left_middle">
<h2>Search By Category</h2>
[xyz-ips snippet="Category-List"]
</div>
</div>
The shortcode is for the plugin XYZ PHP Code which is basically just a way to use ‘includes’ in WordPress posts. I’ve set up some customization in the WP database but it’s just basic database calls. All the information is returned successfully from the database so there’s no issue there. The PHP code I’m using there is:
$sql = "SELECT * FROM wp_categories_table";
$result = $wpdb->get_results($sql);
echo "<ul class='category_list'>";
foreach( $result as $results ) {
$category = $results->category;
$number = $results->number_of;
$category_html = htmlentities($category);
echo "<li><a href='?search-class=DB_CustomSearch_Widget-db_customsearch_widget&widget_number=preset-default&cs-all-0=&cs-post_cat-1=".$category_html."&search=Search'>".$category." (".$number.")</a></li>";
}
echo "</ul>";
What happens is that when the PHP code runs, it echoes it right after the div.description (and before the div#home_left_middle) and just the H2 remains in the home_left_middle div. That’s not where the code is being run.
The curious thing about this is that I was using the exact same code on another site (I duplicated this site because we were just changing servers, exact same content though) and it works fine on the other server. This is a VPS so I’m wondering if there is some sort of PHP extension that I haven’t installed on the server correctly that may be causing this? I know that’s a bit of a reach but I’m confused as to why the exact same code, in the same version of WordPress and using the same theme and CSS files would cause different results on two different sites. The only difference I see is possibly the server unless I’m overlooking some small error?
EDIT
For those asking about an output buffering issue, I’ve recently copied the php.ini file over from the last server and the output buffering code looks like this:
; output_buffering
; Default Value: Off
; Development Value: 4096
; Production Value: 4096
So it appears to be commented out.
Like maioran84 suggested in the comments, this is probably some issue with the output buffers. I would suggest you copy over the php.ini file from the working site to the non-working site server.
Looking inside the PHP configuration file, there are a few settings that may be causing the output to be displayed before wordpress is ready for it.
Looking at the WordPress documentation for
add_shortcode()
, the following block of text really stood out. You can easily change all of yourecho
statements to append the value to a string and then return the string as a whole.https://codex.wordpress.org/Function_Reference/add_shortcode
Hopefully this will help you with this issue, but I have never heard of any error like this happening before.
I know this question is old and has been resolved, but I had the same issue and managed to resolve it differently, so I thought I’d share 🙂
I was using the same wordpress plugin you were, XYZ php code, and it was outputting above the div it was being called in. I switched to a different plugin called ‘PHP Code for posts’, which works exactly the same was as XYZ, expect with a slightly nicer code editor and it outputted exactly as expected.
It’s odd because I normally use XYZ in my WP builds and its always worked fine, so all I can guess is there’s a bad plugin interaction. Obviously there’s a difference in the way the plugins work, but I’m not feeling intrepid enough today to figure out what ;).