• Resolved gailwingate1

    (@gailwingate1)


    Hi, I hope you can help me out. I’m not a java script developer, but through lots of searching and reading on my part I’ve tried to patch together some code to solve my problem.

    The problem is this: When a user sees a post title link on the results from a search engine such as google, yahoo, bing, etc., and clicks on that link, I need our site to determine if that user has access to that content or not. Some of our content can be viewed by the public and some cannot. We have created a custom taxonomy called Role that is used to determine what content our users can view. If Role has the value of general-public, then the user can see the post content. If Role does not have that value, then the user must be redirected to our membership options page.

    I’ve come up with the following code and created it through this plugin. I set the options to External File, Header, and In Frontend, although I’m not sure if that’s even correct. Can you help me with this?

    $url = parse_url($_SERVER['HTTP_REFERER']);
    $sites = array('google','yahoo','bing');
    foreach ($sites as $site) {
    if stristr($url['host'],$site) {
    $terms = get_the_terms( get_the_ID(), role );
    if ( is_array( $terms ) && in_array( 'general-public', $terms ) ) {
    
    	//do nothing
    
    }
    
    else {
    
    	wp-redirect('https://www.mders.org/my-membership-options');
    
    }
    
    }
    }

    Thank you!
    Gail

    https://www.ads-software.com/plugins/custom-css-js/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Diana Burduja

    (@diana_burduja)

    Hello Gail,

    that is not Javascript code, that is PHP.

    You can use that code in a WordPress hook, for example:

    function redirect_from_search_engines() {
      // your code here ...
    }
    add_action( 'wp', 'redirect_from_search_engines' );

    and you can write the code in your theme’s functions.php or use a plugin like My Custom Functions, the effect should be the same.

    Also, you forgot quotes for “role” on line 5. That will throw an error.

    Try the following code.

    function redirect_from_search_engines() {
    	$url = parse_url( $_SERVER['HTTP_REFERER'] );
    	$sites = array( 'google', 'yahoo', 'bing' );
    	foreach( $sites as $site ) {
    		if ( ! stristr( $url['host'], $site ) ) continue;
    
    		$terms = get_the_terms( get_the_ID(), 'role' );
    
    		if ( ! is_array( $terms ) || ! in_array( 'general-public', $terms ) ) continue;
    
    		wp-redirect('https://www.mders.org/my-membership-options');
    	}
    }
    add_action( 'wp', 'redirect_from_search_engines' );

    I didn’t test the code’s functionality, but it shouldn’t throw any errors.

    Thread Starter gailwingate1

    (@gailwingate1)

    Hi Diana, thank you so much for providing this and fixing my code. I really appreciate it. I’ve installed the plugin you suggested and am working with it now.

    Gail

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Java script to restrict content’ is closed to new replies.