Preview Post Broke in WordPress?

I’ve had a problem with my blog that has been on the back of my mind for a while now; whenever I click the “Preview” button it simply shows my homepage. No errors…. just my homepage. I had a search about and there were suggestions it may be because of the use of a sub-domain (or lack of in my case). So I tried changing a few settings in the admin panel, but still no luck. I thought it may be a .htaccess issue until I enabled the now famous Kubric theme, also know as ‘default’; and suddenly the preview function worked. So it was an issue with my theme… Oops!

My immediate thought was the ‘functions.php’ file since the ‘single.php’ file was pretty much the same as Kubrics. After a few minutes of head scratching I finally solved the issue, my sidebar was breaking things:

1
2
3
4
5
6
7
8
9
10
//Register Sidebars
$p = array(
    'before_widget'  =>   '<li id="%1$s" class="widget %2$s">',
    'after_widget'   =>   '</li>',
    'before_title'   =>   '<h3 class="widgettitle">',
    'after_title'    =>   '</h3>'
);
if (function_exists('register_sidebars')){
    register_sidebars(2, $p);
}

Note the use of ‘$p’, it must be used internally by WordPress. My functions.php was overwriting this variable and breaking the preview function. Once this is changed to ‘$args’ everything worked perfectly:

1
2
3
4
5
6
$args = array(
    //settings here
    );
if (function_exists('register_sidebars')){
    register_sidebars(2, $args);
}

So there you go kids, don’t use ‘$p’ in your themes / plug-ins, use something a little more descriptive for your variable names.

Benjamin Robinson on July 9 13 / 189 Permalink

I came across your page while looking for a solution to a problem with similar symptoms. I solved it, and thought I’d share the solution I came up with, too. In my case, I was doing some custom query filtering with pre_get_posts to include two post types in the main query. For the “general” query which included all post types possible, I was using: $query->set(‘post_type’, get_post_types(”,’names’) );

But! When get_post_types() has a blank in that first argument, post previews don’t work. It only queries publicly availabled (published) posts. So, to allow previews correctly again, I had to change it to:

if(is_preview()) {$query->set(‘post_type’, get_post_types(‘false’,'names’) ); } else { $query->set(‘post_type’, get_post_types(”,’names’) ); }

Leave a Comment

Your email will not be published. Required fields are marked *.