cregox
cregox

Reputation: 18408

What could make wordpress add_shortcode stop working?

I've installed WordPress on top of wordpress many times, through SVN and replacing folders... The database remained always the same. Suddenly a fresh copy from SVN can't work in two different machines with the following code, from wp survey and quiz tool:

function wpsqt_main_site_quiz_page($atts) {

    extract( shortcode_atts( array(
                    'name' => false
    ), $atts) );

    if ( !$name ){
        require_once WPSQT_DIR.'/pages/general/error.php';
    }

    require_once WPSQT_DIR.'/includes/site/quiz.php';
    ob_start();
    wpsqt_site_quiz_show($name);
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}

add_shortcode( 'wpsqt_page' , 'wpsqt_main_site_quiz_page' );// Deprecated and will be removed
add_shortcode( 'wpsqt_quiz' , 'wpsqt_main_site_quiz_page' );

If I use echo to see where the code is being reached, add_shotcode is being reached while inside the function is not and the page just displays this:

[wpsqt_quiz name="test"]

Instead of replacing it with the expected quiz.php.

Now I just deleted the database got a fresh install of wordpress and the plugin, and of course everything worked fine. If I get the SVN version, which isn't all that modified (it just got 1 plugin - Magic Fields - and a customized theme), remove the plugin and install it again, it still doesn't work!

What could be going wrong here? What is everything needed to make add_shortcode work?

Upvotes: 3

Views: 4541

Answers (1)

cregox
cregox

Reputation: 18408

That problem had been bugging me since yesterday. Finally found out the reason, (now) obviously on the customized template.

The header included a call to query_posts, which supposedly can only be called once per page load. Then there comes wp_reset_query to rescue. But wait! Seems like both of those functions are deprecated and neither should be used! Instead, we should always use WP_query object.

So, this works but it's wrong:

<?php query_posts('showposts=10'); ?>  
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>  
   <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>  
<?php endwhile; endif; ?>  
<?php wp_reset_query(); ?>  

and this is the right and proper way:

<?php $r = new WP_Query(array('showposts' => '10', 'what_to_show' => 'posts', 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1)); ?>  
<?php if ($r->have_posts()) : while ($r->have_posts()) : $r->the_post(); ?>  
   <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>    
<?php endwhile; endif; ?> 

Without that, the subsequent query_posts on the page itself are not properly loaded and thus the [wpsqt_quiz name="test"] inside them (in the page post) is never called.

Also, it seems like [wpsqt_quiz name="test"] can't be added to the template page.

That's all.

Upvotes: 2

Related Questions