Title: Java script to restrict content
Last modified: August 31, 2016

---

# Java script to restrict content

 *  Resolved [gailwingate1](https://wordpress.org/support/users/gailwingate1/)
 * (@gailwingate1)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/java-script-to-restrict-content/)
 * 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('http://www.mders.org/my-membership-options');
   
       }
   
       }
       }
       ```
   
 * Thank you!
    Gail
 * [https://wordpress.org/plugins/custom-css-js/](https://wordpress.org/plugins/custom-css-js/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [SilkyPress](https://wordpress.org/support/users/diana_burduja/)
 * (@diana_burduja)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/java-script-to-restrict-content/#post-7047433)
 * 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](https://wordpress.org/plugins/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('http://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](https://wordpress.org/support/users/gailwingate1/)
 * (@gailwingate1)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/java-script-to-restrict-content/#post-7047570)
 * 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.

 * ![](https://ps.w.org/custom-css-js/assets/icon-128x128.png?rev=1303730)
 * [Simple Custom CSS and JS](https://wordpress.org/plugins/custom-css-js/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/custom-css-js/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/custom-css-js/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-css-js/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-css-js/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-css-js/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [gailwingate1](https://wordpress.org/support/users/gailwingate1/)
 * Last activity: [10 years, 4 months ago](https://wordpress.org/support/topic/java-script-to-restrict-content/#post-7047570)
 * Status: resolved