0

A visitor visits a page

for example: https://www.example.com/wp-login.php?action=rp&key=abc&login=jim

I want to have a block of text that displays only if the URL has ?action=rp

and a block of text that displays if the url is naked.

There are many content visibility plugins that can control visibility based on role etc.

But I can't find one that can control visibility based on a URL parameter.

Does one exist? If not, is there another way I can achieve that?

  • 1
    You mean, a text in the post content? – Sally CJ Aug 13 at 23:36
  • yep, some text in the post content. – Jim Duggan Aug 14 at 2:01
  • Ok, but the example URL is for the standard WordPress login page. So how are you displaying the post content there? Or are you referring to just any URLs having the action=rp in the query string? – Sally CJ Aug 14 at 2:30
  • 1
    Great question! I am using a plugin called Restrict Content Pro which has it's own login form. I can put that login form on any page with the shortcode [login_form]. I put it on a page called login. That's now set as my login page. So any query to site.com/wp-login.php is automatically redirected to site.com/login/ Since the login page is just like any other page, I can add post content on it as I like. I want it to display some text when it is normal (with no parameters) and some other text when the form becomes a reset password form (with parameters ?action=rp) – Jim Duggan Aug 14 at 16:24
  • I see. And have a look at my answer. That should work. :) – Sally CJ Aug 15 at 5:19
1

You can use add_shortcode() to register custom Shortcodes for restricting access to certain parts of the post content.

And here's a working example, with specific to your case:

function my_rp_shortcode( $atts = [], $content = '', $tag = '' ) {
    if ( 'if-rp' === $tag ) {
        // Return $content if the URL query string has action=rp.
        return ( isset( $_GET['action'] ) && 'rp' === $_GET['action'] ) ? $content : '';
    } else { // if-not-rp
        // Return $content if the URL query string doesn't have action=rp.
        return ( empty( $_GET['action'] ) || 'rp' !== $_GET['action'] ) ? $content : '';
    }
}
add_shortcode( 'if-rp', 'my_rp_shortcode' );
add_shortcode( 'if-not-rp', 'my_rp_shortcode' );

You'd add the code to the theme functions file and use the Shortcodes like so:

[if-rp]
This content is shown only if the query string "action" is set to "rp".
Example: example.com/login/?action=rp
[/if-rp]

[if-not-rp]
This content is shown only if the query string "action" is NOT set or the value is NOT "rp".
Example: example.com/login/?action= (empty "action")
Example 2: example.com/login/       (no "action")
[/if-not-rp]

You can change the logic, use two independent Shortcode functions, replace the tags (if-rp and if-not-rp), etc. if you want to. :)

  • 1
    Thank you Sally! This is fantastic. It works well and I think I understand the code. Thank you also for explaining in detail how to use it. I really appreciate that you took the time to help me. Thank you! – Jim Duggan Aug 16 at 17:37
  • You're welcome. I'm glad I could help. :) – Sally CJ Aug 16 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.