• Hi Guys,

    I’m trying to add the <!--nextpage--> tag dynamically using a Shortcode call, but all it’s actually doing is adding it as an HTML Comment instead of rendering it as pagination links, here’s my code that I’m attempting to do;

    public function __construct() {
         add_shortcode('CONTINUED', array(&$this, 'continued_handle'));
    }
    
    public function continued_handle() {
        global $post;
        return $this->your_post_split($post);
    }
    
    public function your_post_split($content) {
        $output = '<div>In page 1</div>';
        $output .= '<!--nextpage-->';
        $output .= '<div>In page 2</div>';
        return $output;
    }

    If anyone can give me any advice as to how I can get it to render the <!--nextpage--> tag as Pagination links I’d be highly appreciated!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You can’t do this from a conventional shortcode handler. The nextpage tag is processed very early in the loop, so at that time, all that exists is your shortcode tag. By the time shortcodes are processed, it is way past time for the nextpage tag to have any effect. Thus it just ends up as a HTML content.

    For this to work, you’d have to hook something like the ‘loop_start’ action, search the content for your shortcode tag, and replace it with the nextpage tag.

    I’m curious, how is entering a shortcode tag any better than entering the nextpage tag? I don’t really see the point. (not that it matters, just curious)

    Thread Starter curt2008

    (@curt2008)

    Would you mind just providing an example of using the loop_start action please?

    I’ve tried the following to no avail:

    public function __construct() {
        add_shortcode('CONTINUED', array(&$this, 'replaceShortcode'));
        add_action('loop_start', array(&$this, 'replaceShortcode'));
    }
    
    public function replaceShortcode() {
        global $post;
    
        if(is_user_logged_in()) {
            return str_replace('[CONTINUED]', '<div>In page 1</div><!--nextpage--><div>In page 2</div>', $post->post_content);
         }
    }
    Moderator bcworkz

    (@bcworkz)

    You’ve hooked the action correctly, it’s what you do in the callback that’s the problem. The global $post is not even assigned the current post yet. However, a pointer to the current post object is passed to your call back. Try something like this:

    public function replaceShortcode($post) {
        if(is_user_logged_in()) {
            $post->post_content = str_replace('[CONTINUED]', '<div>In page 1</div><!--nextpage--><div>In page 2</div>', $post->post_content);
         }
         return;
    }

    You should eliminate the add_shortcode() line in your constructor as this callback is now inappropriate for shortcode handlers. Remember this is untested, this may not even be the right action hook, but I do believe this will work.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Add nextpage tag dynamically using Plugin Shortcode’ is closed to new replies.