Can I add a ‘show all pages’ link to wp link pages?

I use wp_link_pages to paginate individual pages and posts.

Is there a way I can add to it an extra link which’ll show all pages together, on a single page rather than split?

1 Answer
1

Here’s a suggestion using the wp_link_pages and content_pagination filters:

The aim is to add a “Show All Content” link below the content pagination links:

enter image description here

and when we click it, the link should display “Show Content Pagination” and vice versa.

Demo plugin

We can do that with the following demo, where we introduce the wpse_show_all_content GET variable:

add_filter( 'wp_link_pages', function( $output, $args )
{
    // Validate user input
    $show_all_content = (bool) filter_input( 
        INPUT_GET, 
        'wpse_show_all_content', 
        FILTER_VALIDATE_BOOLEAN 
    ); 

    // Generate link
    $link = add_query_arg( 
        'wpse_show_all_content', 
        ! $show_all_content, 
        get_permalink() 
    );

    // Generate link title
    $title = $show_all_content 
        ? 'Show Content Pagination' 
        : 'Show All Content';

    // Append    
    $output .= sprintf( 
        '<a href="https://wordpress.stackexchange.com/questions/239209/%s">%s</a>', 
        esc_url( $link ),
        esc_html__( $title, 'mydomain' )
    );

    return $output;

}, 10, 2 );

To disable the content pagination when wpse_show_all_content is true, we can use (PHP 5.4+):

add_filter( 'content_pagination', function( $pages )
{
    // Validate user input
    $show_all_content = (bool) filter_input( 
        INPUT_GET, 
        'wpse_show_all_content', 
        FILTER_VALIDATE_BOOLEAN 
    ); 

    // Disable content pagination
    if( $show_all_content )
        $pages = [ join( '', $pages ) ];

    return $pages;

} );

Hope you can adjust this further to your needs!

Update

As per discussion, here’s a way to target only a specific wp_link_pages() instance:

add_filter( 'wp_link_pages', function( $output, $args )
{
    // Nothing to do if 'wpse_show_all_link' is missing or not a true boolean string
    if( 
            ! isset( $args['wpse_show_all_link'] ) 
        ||  ! wp_validate_boolean( $args['wpse_show_all_link'] )
    )
        return output;

    // Validate user input
    $show_all_content = (bool) filter_input( 
        INPUT_GET, 
        'wpse_show_all_content', 
        FILTER_VALIDATE_BOOLEAN 
    ); 

    // Generate link
    $link = add_query_arg( 
        'wpse_show_all_content', 
        ! $show_all_content, 
        get_permalink() 
    );

    // Generate link title
    $title = $show_all_content 
        ? 'Show Content Pagination' 
        : 'Show All Content';

    // Append    
    $output .= sprintf( 
        '<a href="https://wordpress.stackexchange.com/questions/239209/%s">%s</a>', 
        esc_url( $link ),
        esc_html__( $title, 'mydomain' )
    );

    return $output;
}, 10, 2 );

where we introduce a custom wpse_show_all_link attribute that can take values like

1, 0, '1', '0', 'yes', 'no', true, false, 'true', 'false'.

Usage:

wp_link_pages( ['wpse_show_all_link' => true, ...] );

Leave a Comment