questions

Dynamic menu URL for each user role

Rate this post

I have the usual “my account” with redirect to “/my-account”, but if I am logged as an administrator I want to redirect me to “/other-page”. I did a part of the code but I am not sure how to change that href.

// Replace my_account url for administrator
function nrc_replace_my_account_url(){
    $user = wp_get_current_user();
    if ( in_array( 'administrator', (array) $user->roles ) ) { ?>
        <script type="text/javascript">
            var my_account = document.getElementsByClassName("my_account_icon");
            my_account.a.href ="newURL"  // here I am stuck 
        </script>
    <?php }
}

add_action('init', 'nrc_replace_my_account_url');

aa9

 

✔️Solution:

// Replace my_account url for administrator
function nrc_replace_my_account_url(){
    $user = wp_get_current_user();
    if ( in_array( 'administrator', (array) $user->roles ) ) { ?>
        <script type="text/javascript">
            var my_accountLink = document.querySelector(".my_account_icon a");
            my_accountLink.href ="newURL"  // here I am stuck 
        </script>
    <?php }
}

add_action('init', 'nrc_replace_my_account_url');

Leave a Reply

Your email address will not be published.

Back to top button