1

I have a function I tried creating that if on certain page it will check user meta for value and if no value or incorrect value then auto submit the form to redirect the user. It's not working currently and redirects all users. the meta key is = pro_status_confirmed_true and the value is professional

<?php } if ( is_page ('1150')){?>

global $current_user;

get_currentuserinfo();

if ( $current_user ) {

$permission = get_user_meta( $current_user->ID, 'pro_status_confirmed_true' , true );

if ( empty( $permission ) {

<script type="text/javascript">jQuery(document).ready(function(){ jQuery('form#gform_190').trigger('submit');});</script>

}

}

<?php }

1 Answer 1

0

Your <?php/?> tags are aligned such that none of your actual PHP code is being interpreted as PHP code (except the is_page ('1150') conditional) - it's actually just printing it all into your markup, including the <script> element. So since it's not running the logic to check the user or the user meta-data, the script element is always there.

I'd simplify it all like this:

<?php
if( is_page( '1150' ) ) {
  $current_user_id = get_current_user_id();

  if( $current_user_id > 0 ) {
    $permission = get_user_meta( $current_user_id, 'pro_status_confirmed_true' , true );

    if( empty( $permission ) ) {
      ?>
      <script type="text/javascript">jQuery(document).ready(function(){ jQuery('form#gform_190').trigger('submit');});</script>
      <?php
    }
  }
}
?>

If all submitting this form does is instantly redirect "non-pro" status users however, you may want to read up on performing the redirect using wp_redirect(). This is more efficient than printing a script element, and also more secure - in printing a script element to redirect "unauthorized" users, they still load the page body - if the script is meant to keep them off that page, a user could still find the page in their browser cache, prevent the redirect, or simply disable JavaScript in order to circumvent the check. Redirecting using wp_redirect() before content is sent to the client will also be somewhat faster.

2
  • how would I go about checking the user meta for the value and using wp redirect if they don't? I'd really like to use that method as I use it extensively elsewhere...
    – poto
    Feb 24, 2017 at 1:51
  • okay I realized what you meant...its working great now thank you very much!
    – poto
    Feb 24, 2017 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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