Question

I have 2 urls:

  • www.site.com/post1/
  • www.site.com/post1/?content=onepage

I have the following function that controls the layout of the post through the URL parameters in BOLD:

www.site.com/post1/?content=onepage activates the function below:


    function onepage(){

        // condition(s) if you need to decide not to disabling shortcode(s)
        if( empty( $_GET["content"] ) || "onepage" !== $_GET["content"] )

            return;

        // Condition(s) at top are not met, we can remove the shortcode(s)


    function remove_shotcode($content) {
        return str_replace('[/shortcode1]', '', $content);    
        return str_replace('[shortcode2]', '', $content);
    }
    add_filter( 'the_content', 'remove_shotcode', 6); 

    /**
     * Ignore the <!--nextpage--> for content pagination.
     * 
     * @see http://wordpress.stackexchange.com/a/183587/26350
     */

    add_action( 'the_post', function( $post )
    {
        if ( false !== strpos( $post->post_content, '<!--nextpage-->' ) ) 
        {
            // Reset the global $pages:
            $GLOBALS['pages']     = [ $post->post_content ];

            // Reset the global $numpages:
            $GLOBALS['numpages']  = 0;

           // Reset the global $multipage:
            $GLOBALS['multipage'] = false;
        }

    }, 99 );
    }

    add_action('wp','onepage');

How to make the url www.site.com/post1/ and www.site.com/post1/?content=onepage load the same function by default.

I think it just need a simple condition for:

This one is for parameter content set to onepage

if( empty( $_GET["content"] ) || "onepage" !== $_GET["content"] )

And another for if no url parameters set.

Was it helpful?

Solution

I want this to apply to all posts

First off, making the content query string defaults to onepage is actually equivalent to enabling the onepage() for all URLs/pages.

And for example to enable it by default on single post pages only (for any post types), then you can replace this:

if( empty( $_GET["content"] ) || "onepage" !== $_GET["content"] )
    return;

with this:

if( ! is_single() && ( empty( $_GET["content"] ) || "onepage" !== $_GET["content"] ) )
    return;

which means the onepage() would also be applied on pages/URLs where the content=onepage presents in the query string.

Check the developer docs for other conditional tags like is_singular().

Does that answer your question?

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top