I'm not sure if I've asked the question properly, apologies for that! What I'm trying to do is, output a <!--nextpage--> tag via a shortcode. But it doesn't work, I guess because shortcodes are applied when the content is rendered, whereas <!--nextpage--> is resolved into links beforehand. Does anyone know if this can be done? Can I apply the <!--nextpage--> tag through my shortcode when the post is rendered?

  • Ever implemented the below? Nov 25 '11 at 23:51
  • @JohannesPille Actually I'm doing it for a self project, hence I work on it only on weekends, as I got full time work on weekdays! I'll be implementing it tomorrow and will definitely get back here! :) Nov 26 '11 at 3:48

I don't know in what exact order filters are applied to the_content(); and whether that's early enough, but if it doesn't work for you, I believe it is safe to assume that you're right in thinking that the shortcode is applied to late.

From /wp-includes/shortcodes.php (line 296, wp 3.2.1) it can be seen that shortcodes are naturally resolved like so:

add_filter('the_content', 'do_shortcode', 11); // AFTER wpautop()

Executing do_shortcode instead when the the_posts hook runs should make sure they are executed early enough. the_posts runs immediately after posts are retrieved from the database. The following should work:

add_filter('the_posts', 'rutwick_shortcode_exec');

function rutwick_shortcode_exec($posts) {
    $post_count = count($posts);
    for ($i = 0; $i < $post_count; ++$i) {
    return $posts;

It might be that lowering the priority will suffice:

add_filter('the_content', 'do_shortcode', 9);

Let it be noted, that I have not tested the above and I'm not guaranteeing nothing. Also, you might run into conflicts with wpautop, because if the above is applied, shortcodes (all shortcodes!) are now resolved before it filters the content.

EDIT: It might be safer to run your own replacement function that early (the following will assume your shortcode is called [next]):

function do_rutwick_shortcode($content) {
    $content = preg_replace('{\[next\]}','<!--nextpage-->',$content);
    return $content;

and call that using either one of the two above methods (i.e. hooking it into the_content with a lower priority number or replacing do_shortcode(...) with do_rutwick_shortcode(...) in the above for loop). If you choose to do that, using add_shortcode becomes superfluous. Also, if you opt for hooking into the_posts the preg_replace could be run directly in said function, two would not be needed, i.e.:

$posts[$i]->post_content =

would save you a function call.

  • Thanks for a detailed explanation! I was thinking of using the save_posts action to replace my shortcodes in the content with the <!--nextpage--> tag. But I'll try this solution. Nov 20 '11 at 3:14
  • @RutwickGangurde I like your idea also. Only caveat of hooking into save_posts would be the permanence of the replacement - when editing posts later, the you wouldn't see the shortcode in the editor anymore, <!--nextpage--> would be in there for good. Nov 20 '11 at 3:25
  • You're right... I didn't think of that! Nov 20 '11 at 3:31
  • Awesome! Hooking into the_posts worked! Now I can return the <!--nextpage--> tag from my shortcode! I checked the database entry for the post with <!--nextpage--> tags added directly into the post content, the entry has the tags. It means the tags too are resolved at a later point, even after the_posts. Thanks a lot! :) Nov 27 '11 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.