You are Viewing : HomewordpressWhen your wordpress post content goes missing!

When your wordpress post content goes missing!

After two straight days of procrastination and leisure, today I woke and fired up my local server, only to find that all my test posts being weird and empty. Content of all posts had mysteriously disappeared.

  post-content-missing-1

Oh no.. not again.

As you can see Above image, the post content is missing, while all the other -Title, meta output just fine. And this really seemed worth playing with.

Causes

Most common – Some vaguely written function hooking into the the_content filter. Also note,

  • Output of the_excerptwas ok.
  • Problem appeared in all the themes, including the wp3.0 default twentyten, so problem is probably not related to a theme.

Debugging

Deactivating plugins one by one is the most widely suggested solution in the interwebs. Well, you can always try that out. But I didn’t want to disable/enable plugins one by one. First, it takes too much time, is boring and I always forget to enable a plugin or two and scratch my head for hours why something isn’t working.

As we know, wordpress hook/function the_content outputs the post’s content. Filters are attached to it, few from the core and some from the plugins.

1. Lets check if the content is fine. The function get_the_content(),  outputs the unfiltered content. I inserted echo get_the_content into 2010′s loop-single.php

get_the_content

Output the unmodified content with get_the_content()

And the result.

get_the_output

Content appears ok!

Fixing

Well, something is messing with the_content() hook here. To trace the culprit, I decided to use one fantastic snippet written by Rarst, that outputs the list of all hooks and respective filters attached to them.

function list_hooked_functions($tag=false){
	global $wp_filter;
	if ($tag) {
		$hook[$tag]=$wp_filter[$tag];
		if (!is_array($hook[$tag])) {
			trigger_error("Nothing found for '$tag' hook", E_USER_WARNING);
			return;
		}
	} else {
		$hook=$wp_filter;
		ksort($hook);
	}
	echo '<pre>';
	foreach($hook as $tag => $priority) {
		echo "<br />&gt;&gt;&gt;&gt;&gt;\t<strong>$tag</strong><br />";
		ksort($priority);
		foreach($priority as $priority => $function){
			echo $priority;
			foreach($function as $name => $properties) echo "\t$name<br />";
		}
	}
	echo '</pre>';
	return;
} // END function list_hooked_functions.
 echo '</pre>';
 return;
}

 echo '</pre>';
 return;
}

Running the above function without the argument(here the hook name) lists all the hooks with functions. In our case, specific problem is with the_content hook, So let’s try the below.

<?php
	// the_content is our hook.
	list_hooked_functions( 'the_content' );
?>

Let’s see the result now. Ha!

gotcha_the_content

the_content hook and functions hooked.

So I found that a function I started and forgot to finish – clean_pre_shjs() was the reason behind this. I rewrote the function and now the content appears ok!

Let’s summarize the causes of my problem(pun intended).

Reasons for missing post content in WordPress

Further

The article just points you out how much smart and interesting debugging can be. Try this function on any wordpress hook, core or yours own.

Rarst, who wrote the above snippet went further and has a new set of hook debug functions here. Please take a look at them.

+ Kavin.

  • http://www.Rarst.net Rarst

    Glad you found my snippets useful. :) One small correction to your illustration – “hook priority if any” is not entirely correct. There is no such thing like hooked function without priority.

    So the priority is not displayed for every line, but it’s same for all functions until next priority is displayed. And inside of single priority order of execution is same as order function were added in – my code preserves that.

    • http://kav.in kavin

      Thanks for the time and comment. Corrected. :D