Removing the query string from Ajax loaded pagination links

I’m loading my entire site via AJAX using this wordpress plug-in.

Everything works fine on standard WP Queries, but when I try to paginate a custom query’s results after being loaded via Ajax, Ajax is inserting the query string into the URL breaking the links.

Read More

How the paginated url’s should look :

http://www.example.com/stuff/page/3

After an AJAX load they render like this :

http://www.example.com/stuff/?_=1457614717299/page/3

I’ve tried using both of the following to reset the base url after an Ajax load, removing the query string but neither are working :

if( ! empty( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) &&
    strtolower( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ]) == 'xmlhttprequest' ) {

    $pagination_args['base'] = user_trailingslashit( trailingslashit( remove_query_arg('s',get_pagenum_link(1) ) ) . 'page/%#%/', 'paged');

}

And :

if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {

   $pagination_args['base'] = user_trailingslashit( trailingslashit( remove_query_arg('s',get_pagenum_link(1) ) ) . 'page/%#%/', 'paged');

}

Here is the complete php code for the query and pagination (without the base url reset as mentioned above for clarity) :

$rows_per_page = 3;

$current = (intval(get_query_var('paged'))) ? intval(get_query_var('paged')) : 1;

$rows = $wpdb->get_results("SELECT post_id FROM wp_favorite_show WHERE user_id = $user_ID");
global $wp_rewrite;

$pagination_args = array(
'base' => @add_query_arg('paged','%#%'),
'format' => '',
'total' => ceil(sizeof($rows)/$rows_per_page),
'current' => $current,
'show_all' => false,
'type' => 'plain',
);

if( $wp_rewrite->using_permalinks() )
$pagination_args['base'] = user_trailingslashit( trailingslashit( remove_query_arg('s',get_pagenum_link(1) ) ) . 'page/%#%/', 'paged');

if( !empty($wp_query->query_vars['s']) )
$pagination_args['add_args'] = array('s'=>get_query_var('s'));

$start = ($current - 1) * $rows_per_page;
$end = $start + $rows_per_page;
$end = (sizeof($rows) < $end) ? sizeof($rows) : $end;

Then after my loop I display the pagination like this :

echo paginate_links($pagination_args);

I’ve tried a few other ways of doing it too but nothing is removing the query.

Anybody have any idea of how to prevent the query string being injected into the returned Ajax’d URLs?

Related posts

1 comment

  1. Ok just in case anybody else is having a similar issue I managed to get it working. It’s by no means ideal, it works by hard coding the base url for pretty links. Obviously this would be no use in a repeating function, but if you are using it in a single page-template (as I am) it will work just fine :

     if( $wp_rewrite->using_permalinks() )
    $pagination_args['base'] = 'http://www.example.com/stuff/' .'page/%#%/';
    

Comments are closed.