• Resolved Lion73

    (@lion73)


    Hi nsinelnikov,
    I had written this function for Deny access to the requested profile page if a certain metakey value of required user not equal to the current user’s display name.
    It worked perfectly up to version 2.1.2,
    In version 2.1.3 something has changed and no longer works properly.
    Can you suggest any changes?

    
    add_action("template_redirect","um_custom_page_restriction", 9999 );
    function um_custom_page_restriction(){
    
    $user_id = wp_get_current_user();
    $display_name = $user_id->display_name;
    
    $profile_id = um_get_requested_user();
    um_fetch_user( $profile_id );
    $meta_value = get_user_meta( $profile_id, 'mymetakey', true );
    
      if ( ! empty( $meta_value ) && is_user_logged_in() ) {
        if (ICL_LANGUAGE_CODE == 'en') {
            if ( um_is_core_page('user') && $display_name != $meta_value ) {
    	  exit( wp_redirect( 'https://mysite/mypage-en' ) );
    	}
        }
    
        if (ICL_LANGUAGE_CODE == 'de') {
            if ( um_is_core_page('user') && $display_name != $meta_value ) {
    	  exit( wp_redirect( 'https://mysite//mygage-de' ) );
    	}
        }
      }
    }
    

    Thnaks in advance
    Lion

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Mykyta Synelnikov

    (@nsinelnikov)

    Hi @lion73

    Could you use debug steps via var_dump() or error_log() PHP functions and check what condition stops working?

    Maybe some UM function return other value or ICL_LANGUAGE_CODE isn’t defined.

    Let me know,
    Regards

    Thread Starter Lion73

    (@lion73)

    Hi nsinelnikov, with var_dump () I could verify that the um_get_requested_user() function returned bool(false), so I added a few lines and now it seems to work. I have yet to check with version 2.1.3.
    Do you have any suggestions on the new code below?
    Thank you in advance
    Lion

    
    add_action("template_redirect","um_custom_page_restriction", 9999 );
    function um_custom_page_restriction(){
    
    //Current user
    $user_id = wp_get_current_user();
    $display_name = $user_id->display_name;
    $role = $user_id->roles[0];
    
    //Requested user
    if ( !function_exists( 'un_get_requested_user' ) ) {
       require_once 'SERVERPATH/wp-content/plugins/ultimate-member/includes/um-short-functions.php';
    }
    $profile_id = um_get_requested_user();
    um_fetch_user( $profile_id );
    $meta_value = get_user_meta( $profile_id, 'MYMETAKEY', true );
    
    //var_dump ( $VARTOTEST );
    
    //Condition
      if ( ! empty( $meta_value ) &&  $role == 'um_CHOSEN_ROLE' ) {
        if (ICL_LANGUAGE_CODE == 'en') {
            if ( um_is_core_page('user') && $display_name != $meta_value ) {
    	  exit( wp_redirect( 'https://mysite//mygage-en' ) );
    	}
        }
    
        if (ICL_LANGUAGE_CODE == 'de') {
            if ( um_is_core_page('user') && $display_name != $meta_value ) {
    	  exit( wp_redirect( 'https://mysite//mygage-de' ) );
    	}
        }
      }
    }
    
    Plugin Author Mykyta Synelnikov

    (@nsinelnikov)

    Hi @lion73

    add_action("template_redirect","um_custom_page_restriction", 9999 );
    function um_custom_page_restriction(){
    
    if ( ! um_is_core_page('user') ) {
    return;
    }
    
    //Current user
    $user_id = wp_get_current_user();
    $display_name = $user_id->display_name;
    $role = $user_id->roles[0];
    
    $profile_id = um_get_requested_user();
    um_fetch_user( $profile_id );
    $meta_value = get_user_meta( $profile_id, 'MYMETAKEY', true );
    
    //var_dump ( $VARTOTEST );
    
    //Condition
      if ( ! empty( $meta_value ) &&  $role == 'um_CHOSEN_ROLE' ) {
        if ( $display_name != $meta_value ) {
           if (ICL_LANGUAGE_CODE == 'en' ) {
    	  exit( wp_redirect( 'https://mysite//mygage-en' ) );
           } elseif (ICL_LANGUAGE_CODE == 'de' ) {
    	  exit( wp_redirect( 'https://mysite//mygage-de' ) );
           }
        }    
      }
    }

    Let me know,
    Regards

    Thread Starter Lion73

    (@lion73)

    Perfect nsinelnikov, it works well. Thanks!!!
    Can you also suggest me something here EVEN IF IT WORKS:
    This shows only users with the “MYMETAKEY” field equal to the “display_name” field of the logged in user.

    add_filter('um_prepare_user_query_args', 'um_my_custom_query_args', 99, 2);
    function um_my_custom_query_args( $query_args, $args ) {
    
    if ( ! function_exists( 'wp_get_current_user' ) ) {
            return 0;
    }
    
    $user = wp_get_current_user();
    $display_name = $user->display_name;
    
    // Validation form ID my-special-users directory en and de
    if( $args["form_id"] == "30" OR $args["form_id"] == "308" ) {  
         $query_args['meta_query'][] = array( 
                "relation" => "OR",
                array(
                    'key' => 'MYMETAKEY', 
                    'value' => serialize( $display_name ), 
                    'compare' => 'LIKE' 
                ),
                array(
                    'key' => 'MYMETAKEY',
                    'value' => $display_name,
                    'compare' =>  '='
                )
         ); 
    
     } 
      return $query_args; 
    }
    

    Thanks in advance, Lion

    Plugin Author Mykyta Synelnikov

    (@nsinelnikov)

    Hi @lion73

    If you need to make this directory show only logged-in user you need to set some unique metasearch when a user isn’t logged in.

    add_filter('um_prepare_user_query_args', 'um_my_custom_query_args', 99, 2);
    function um_my_custom_query_args( $query_args, $args ) {
    
        if ( $args["form_id"] != "30" && $args["form_id"] != "308" ) {
            return $query_args;
        }
    
        if ( ! is_user_logged_in() || ! function_exists( 'wp_get_current_user' ) ) {
            //search by not real meta key and value to show empty directory for not logged in user
            $query_args['meta_query'][] = array( 
                "relation" => "OR",
                array(
                    'key' => 'xxxxxxxx', 
                    'value' => '99999999999999', 
                    'compare' => '=' 
                ),
            ); 
        } else {
            $user = wp_get_current_user();
            $display_name = $user->display_name;
    
            $query_args['meta_query'][] = array( 
                "relation" => "OR",
                array(
                    'key' => 'MYMETAKEY', 
                    'value' => serialize( $display_name ), 
                    'compare' => 'LIKE' 
                ),
                array(
                    'key' => 'MYMETAKEY',
                    'value' => $display_name,
                    'compare' =>  '='
                )
           ); 
        }
        return $query_args; 
    }

    You could try that.

    Please let me know if I can mark this topic as resolved,
    Regards

    Thread Starter Lion73

    (@lion73)

    Nsinelnikov, it works well. Thanks!!!
    Please wait some time so I can check if all works well…then I mark topic as resolved.
    Lion

    Thread Starter Lion73

    (@lion73)

    Nsinelnikov, It is possible to add a condition that allows the admin role to see all results?

    Thread Starter Lion73

    (@lion73)

    Nsinelnikov are you there?

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @lion73

    Sorry for the late response.

    You can enable the users with administrator roles in the Member Directory settings.

    Feel free to re-open this thread if there’s any question that may come up.

    Regards,

    Thread Starter Lion73

    (@lion73)

    No Champ Camba, via code, read the post carefully before replying.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Ver. 2.1.3 – Something has changed’ is closed to new replies.