最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Control page content visibility based on URL parameter

programmeradmin4浏览0评论

A visitor visits a page

for example: .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?

A visitor visits a page

for example: https://www.example/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?

Share Improve this question asked Aug 13, 2019 at 17:02 Jim DugganJim Duggan 892 silver badges9 bronze badges 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/wp-login.php is automatically redirected to site/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
Add a comment  | 

2 Answers 2

Reset to default 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/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/login/?action= (empty "action")
Example 2: example/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. :)

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()

发布评论

评论列表(0)

  1. 暂无评论