• Resolved timurkayzer

    (@timurkayzer)


    Hello. There is a problem with hooks in plugin, or i may not know some features of WordPress. There are hooks, where function is referred from initialized object:

    add_action( 'template_redirect', array($instance, 'profile_redirect'), 10);

    I’d like to remove redirect to /profile/%username% and leave just /profile, and let users only see their own profile pages, this is why i need this hook. Could you please help me with this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Stiofan

    (@stiofansisland)

    Hello,

    I will ask the UWP developer to reply about the hook, but i am not sure its the best way to do what you want.

    If your goal is to just only let users see their own profile there is other ways to do it, like simply checking if the current user ID matches the author page and if not do a redirect or show a message.

    Thanks,

    Stiofan

    Hi,

    It is not recommended to remove %username% parameter from the URL as it is required parameter to load the data of that user in the profile page.

    If you want to allow access to own profile page only and redirect to home page while accessing other profile page then you can put following code in your functions.php file of the currently active theme.

    add_action('template_redirect', 'uwp_profile_page_redirect', 99);
    function uwp_profile_page_redirect(){
    
        if (!is_page()) {
            return false;
        }
    
        global $wp_query, $post;
        $current_page_id = $post->ID;
        $profile_page = uwp_get_option('profile_page', false);
    
        if ( $profile_page && ((int) $profile_page ==  $current_page_id ) ) {
            if (isset($wp_query->query_vars['uwp_profile'])) {
                $url_type = apply_filters('uwp_profile_url_type', 'slug');
                $author_slug = $wp_query->query_vars['uwp_profile'];
                if ($url_type == 'id') {
                    $user = get_user_by('id', $author_slug);
                } else {
                    $user = get_user_by('slug', $author_slug);
                }
    
                if (isset($user->ID) && is_user_logged_in() && $user->ID == get_current_user_id()) {
                    return false;
                } else {
                    wp_redirect( home_url('/')); // redirect for other users
                    exit();
                }
            }
        }
    
        return false;
    }

    Let me know if it helps you.

    Regards,
    Patrik

    Thread Starter timurkayzer

    (@timurkayzer)

    Hello,

    thank you for reply, it helped me to restrict access to other users pages.

    But i’d like to remove username from URL, because it will be more “user-friendly” – user has access only to his own page anyway. Userdata can be checked by WordPress itself, like wp_get_current_user() etc.

    Is there any way to do it? Because deleting this redirection hook from plugin code would be wrong – it works, but becomes unupdateable then.

    Actually, wp_get_current_user() will work for current logged in users while in the plugin we have other users profile visible to all users so this function will not work for the global plugin. It can work for your specific requirements. You may need to make changes in the core to achieve your requirements.

    Thread Starter timurkayzer

    (@timurkayzer)

    Thank you for supporting plugin!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Hooks’ is closed to new replies.