Support » Fixing WordPress » Pagination not working within shortcode generated page

  • I’m using a “php in page” plugin to allow me to get content into a page from a plugin’s table. It works fine, except that the <!--nextpage--> code isn’t being processed. I can see in the page source that the <!--nextpage--> tag is there, but no pagination is occurring. Other shortcode within the loaded content is being processed, just not the <!--nextpage-->.

    Here’s the php code I’m using to get and load the content into the page:

    <?php
    global $wpdb, $user_ID;
    $reportQuery = "SELECT details FROM ".WATUPRO_TAKEN_EXAMS." WHERE ID=".$_GET['taking_id']";
    $thisReport = $wpdb->get_results($reportQuery, OBJECT);
    $thisReport = apply_filters('the_content', $thisReport[0]->details);
    print $thisReport;
    ?>

    Any ideas what I need to do to get the pagination working on content loaded into the page this way? Is it possible?

    Thanks,
    Scott

Viewing 6 replies - 1 through 6 (of 6 total)
  • I am not sure of a few things:
    A) shortcodes use square brackets, you are showing html comments, maybe the filter expects this, but maybe you mean “[nextpage]” instead.
    B) What is the context of the php snippet above ? Is it in the page template, is it in a shortcode function ? If it is in a shortcode function you should be RETURNing the text rather than displaying it.

    Thread Starter Scott McCulloch

    (@sinemac)

    A) In “normal” pages (i.e., where I type the contents in the editor) using <!–nextpage–> results in pagination. I guess it’s not technically a ‘shortcode’? I don’t know what in WordPress does the actual pagination when it comes across <!–nextpage–>, but it “just works” in the normally generated pages.

    B) The code is placed into the ‘content’ area of a page via a shortcode (using the PHP Code for Posts and Pages plugin).

    I create a ‘snippet’ in the plugin settings, then put the shortcode “[php snippet=3]” into the page content, and it displays the resulting text in the page. (I tried RETURNing the text, but get nothing… PRINTing or ECHOing work – other than the lack of pagination).

    If I take the exact same content text, and copy it into an actual page via the content editor, then the <!–nextpage–> tags work and the content is paginated.

    Somehow, I think, whatever processes the <!–nextpage–> tags is missing this shortcode generated content… for example, if in a page I do something like this:

    Some text here
    <!--nextpage-->
    more text here
    <!--nextpage-->
    [php snippet=3]
    <!--nextpage-->
    last text

    The resulting page will be paginated… BUT, it only paginates there the <!–nextpage–> tags are in the editor generated content… any <!–nextpage–> tags in the text from the php snippet are ignored.

    My guess is that the pagination at <!–nextpage–> points is being done by a filter. Further that this filter is upstream of where your shortcodes are being expanded. Maybe you can change the priority of the filtering ? Maybe you can put the output through the filter again.

    Thread Starter Scott McCulloch

    (@sinemac)

    Yes, I think that’s probably right… I’m having a heck of a time figuring out how to deal with it though.

    I had hoped the “…apply_filters(‘the_content’…” part would do the trick.

    I also found another possibility – placing this in my page template:

    <?php
     if(isset($_GET['taking_id'])){
    	global $wpdb, $user_ID;
    
    function display_report($content){
    	$queryReports = "SELECT * FROM ".WATUPRO_TAKEN_EXAMS." WHERE ID=".$_GET['taking_id']";
    	$thisReport = $wpdb->get_row($queryReports, OBJECT);
    	$thisReport = $thisReport->details;
    	$content = $content . $thisReport;
    	return $content;
    	}
    add_filter('the_content', 'display_report');
    }
    ?>

    So far, no dice.

    Maybe you can search the sourcecode for “<!–nextpage–>”, this may give you clues as to how it gets replaced.

    Of some practical concern is the example you posted, it is extremely dangerous to pass through to mysql the results of GET. Just imaging what would happen when (not if) someone rewrites a link as:
    http://yourname.com/yourfile.php?taking_id="'1'; update wp_users set email='hijack@blackhat.com';"

    NOW all the user email addresses are pointing to the blackhat’s chosen address, he requests a password reset, and owns your site.

    The fix for this is:

    $queryReports = $wpdb->prepare(“SELECT * FROM ” . WATUPRO_TAKEN_EXAMS . ” WHERE ID=%d”, $_GET[‘taking_id’] );

    Prepare recognises the evasive quoting, and defeats the attack.
    If there were any doubt about the content of WATUPRO_TAKEN_EXAMS, you would use prepare to scrub it too.
    This is called an SQL injection attack.

    Thread Starter Scott McCulloch

    (@sinemac)

    Thank you for the tip re: $_GET variables… I will definitely make that change!

    I think I found something in the WP code… /wp-includes/post-template.php

    At line 695:

    * Displays page links for paginated posts (i.e. includes the <!–nextpage–>.
    * Quicktag one or more times). This tag must be within The Loop.

    So it appears the problem is indeed that because my content is being rendered via the shortcode it isn’t technically “in the Loop” as it’s rendered. I think I need to followup on my approach of adding the code to my page.php template, and figure out how to get the custom query to take the place of the default query to generate “the_content”

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Pagination not working within shortcode generated page’ is closed to new replies.