David Stockdale's Scrapcode

Hide Content With Shortcode Based On URL Parameters

I was recently told to make a thank you message visible on a page only if the user had been sent there as a result of filling out a form.

Naturally my first thought was a url parameter.

With a url parameter that is only used in a specific hyperlink combined with a bit of shortcode you can make hidden content only for users who arrive via that specific link.

The first step in doing this is adding this simple bit of code to your “functions.php” file:

 * Hides/Shows something hidden by shortcode depending on the url.
 * Query: ?submit=true
 * To show if ?submit=true:
 * [if-submit]
 * your text here
 * [/if-submit]
 * To show otherwise:
 * [if-not-submit]
 * your text here
 * [/if-not-submit]
add_shortcode( 'if-submit', 'submit_hider' );
add_shortcode( 'if-not-submit', 'submit_hider' );
function submit_hider( $atts = [], $content = '', $tag = '' ) {
    if ( 'if-submit' === $tag ) {
        // Return $content if the URL query string has submit=true.
        return ( isset( $_GET['submit'] ) && 'true' === $_GET['submit'] ) ? $content : '';
    } else { // if-not-submit
        // Return $content if the URL query string doesn't have submit=true.
        return ( empty( $_GET['submit'] ) || 'true' !== $_GET['submit'] ) ? $content : '';

Then you need only add these shortcodes before and after your hidden content:


<Your Content Here>


And you can even have content that shows only if the parameter is not in the url:


<Your Content Here>


And once you have done all this you can test it out by simply adding “?submit=true” to the url of the page/post.

If you want to try it out click this link and see what changes: https://davidstockdalescrapcode.co.uk/hide-content-with-shortcode-based-on-url-parameters?submit=true.

Leave a Reply