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?

5
  • 1
    You mean, a text in the post content?
    – Sally CJ
    Commented Aug 13, 2019 at 23:36
  • yep, some text in the post content.
    – Jim Duggan
    Commented Aug 14, 2019 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
    Commented Aug 14, 2019 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
    Commented Aug 14, 2019 at 16:24
  • I see. And have a look at my answer. That should work. :)
    – Sally CJ
    Commented Aug 15, 2019 at 5:19

2 Answers 2

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. :)

2
  • 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
    Commented Aug 16, 2019 at 17:37
  • You're welcome. I'm glad I could help. :)
    – Sally CJ
    Commented Aug 16, 2019 at 23:47
0

If you don't want to use shortcodes you can use enqueue to add custom CSS to the header, add classes to your content and then toggle display none in CSS.

Add this to your functions.php or custom-functions.php file:

function wp_89494_enqueue_scripts() {
  if ( isset( $_GET['src'] ) && 'pc' === $_GET['src'] ) {
    wp_enqueue_style( 
      'wpse_89494_style_1', 
      get_stylesheet_directory_uri(). '/pcast.css' 
    );
  }
  
}

add_action( 'wp_enqueue_scripts', 'wpse_89494_enqueue_scripts' );

Then this to your url:

/?src=pc

And this to a file pcast.css placed in your child theme folder:

.stuff-to-hide {display:none !important;}

Inspired by this thread:

How to enqueue the style using wp_enqueue_style()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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