Title: Trigger AgeGate with JavaScript
Last modified: December 17, 2019

---

# Trigger AgeGate with JavaScript

 *  [nushara](https://wordpress.org/support/users/nushara/)
 * (@nushara)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/trigger-agegate-with-javascript/)
 * Hello 🙂
 * I wanted to show agegate only to users who set their buddypress profile to be
   mature content.
 * I have already setup some code but I am not able to trigger agegate with javascript
   and also need a “if agegate has been approved” check.
 *     ```
       function mature_content_restriction() {
           function restrict_content(){
               add_filter( 'age_gate_restricted', true, 0);
           }
           $user_id = bp_displayed_user_id();
           $user_profile_mature = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( 90, $user_id ) );
           echo ('<input type="hidden" id="mature-content-filter" value="' . $user_profile_mature . '" />' );
           ?>
           <script>
               var matureCheck = document.getElementById("mature-content-filter").value;
               if(matureCheck === "1"){
                   console.log("Ist Mature User");
                   <?php restrict_content(); ?>
                   var style = "<style id='mature-content-blur' type='text/css'>.yz-header-cover,.avatar{filter:blur(15px);}</style>";
                   jQuery('head').append(style);
                   if(AgeGate = 'passed'){
                       jQuery('#mature-content-blur').remove;
                   }
               }
               var slug = window.location.pathname;
               if('/wordpress/profiles/' === slug){
                   console.log("Ist Profiles Mature");
                   <?php restrict_content(); ?>
               }
           </script>
           <?php
       }
       add_action( 'wp_head', 'mature_content_restriction');
       ```
   
 * Would be great if you could help me here, since I can’t figure out how to do 
   this.
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Ftrigger-agegate-with-javascript%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  Plugin Author [Phil](https://wordpress.org/support/users/philsbury/)
 * (@philsbury)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/trigger-agegate-with-javascript/#post-12345886)
 * Hi [@nushara](https://wordpress.org/support/users/nushara/),
 * Sorry for the really slow reply here! Do you still need help with this?
 * Thanks
    Phil
 *  Thread Starter [nushara](https://wordpress.org/support/users/nushara/)
 * (@nushara)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/trigger-agegate-with-javascript/#post-12587298)
 * I have found a better way to trigger it. But now the time it takes to show the
   agegate is like 3-4 seconds.
 * This is my code on my functions.php:
 *     ```
       function remove_age_gate_restriction(){
       	?>
       	<script>
       		jQuery(document).on('agegateshown', function () {
       			jQuery('head').append("<style id='mature-content-blur' type='text/css'>.yzm-user-cover,.avatar,.index-honor-user,.yz-header-cover,#twitter-feed,#yz-bp:before,#custom_gallery_tab,#picarto-stream-container{opacity:0;}</style>");
       		});
       		jQuery(document).on('agegatepassed', function () {
       			jQuery('#mature-content-blur').remove();
       		});
       	</script>
       	<?php
       }
       add_action ( 'wp_head', 'remove_age_gate_restriction', 10 );
   
       //Enable age gate for mature content
       add_filter('age_gate_restricted', function ($restrict, $meta) {
       	$user_mature = bp_get_profile_field_data( array( bp_displayed_user_id(),'field' => 90 ) );
       	if($user_mature != 'Yes'){
       		return $restrict;
       	}else{
       		return true;
       	}
       }, 1, 2);
       ```
   
 * Everything works fine but the delay is horrable. Can you help me find a way to
   get this firing faster?

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

The topic ‘Trigger AgeGate with JavaScript’ is closed to new replies.

 * ![](https://ps.w.org/age-gate/assets/icon-256x256.png?rev=2783003)
 * [Age Gate](https://wordpress.org/plugins/age-gate/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/age-gate/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/age-gate/)
 * [Active Topics](https://wordpress.org/support/plugin/age-gate/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/age-gate/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/age-gate/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [nushara](https://wordpress.org/support/users/nushara/)
 * Last activity: [6 years, 2 months ago](https://wordpress.org/support/topic/trigger-agegate-with-javascript/#post-12587298)
 * Status: not resolved