0

I want users to change their avatar on a general site, because some plugins only work with that. for example

www.example.com/members/user1/profile/change-avatar/

And

www.example.com/members/user2/profile/change-avatar/

Should both redirect to

www.example.com/change-avatar/

1

Solved it.

add_action('init','redirect_to_change_avatar');

function redirect_to_change_avatar() {
    if ( strpos($_SERVER['REQUEST_URI'], '/profile/change-avatar/') !== false ) {
        wp_redirect('/change-avatar/');
        exit;
    }
}
0

I would recommend something a little more robust for your logic, that way if the URL changes you aren't still using the same position arguments:

function redirect_to_change_avatar() {
    $request_uri = filter_input( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_STRING );

    // Strpos will return boolean FALSE if the string does not match at all.
    // N.B the strict === comparison:
    if ( false === strpos( $request_uri, '/profile/change-avatar/' ) {
        return;
    }

    wp_redirect( '/change-avatar/' );
    exit;
}

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.