skip to Main Content

I have the following code in the functions.php file.

add_action( 'init', 'add_admin_tools_account_endpoint' );
function add_admin_tools_account_endpoint() {
        add_rewrite_endpoint( 'wp-admin', EP_PAGES );
}

add_filter ( 'woocommerce_account_menu_items', 'custom_account_menu_items', 10 );
function custom_account_menu_items( $menu_links ){
        if ( current_user_can('administrator') ) {
                $menu_links = array_slice( $menu_links, 0,0 , true )
                + array( 'wp-admin' => __('Admin tools') )
                + array_slice( $menu_links, 0, NULL, true );
        }
        return $menu_links;
}

add_action( 'woocommerce_account_admin-tools_endpoint', 'admin_tools_account_endpoint_content' );
function admin_tools_account_endpoint_content() {
        if ( current_user_can('administrator') ) {
                echo "<h3 class='headline'>Admin Tools</h3>
                <p style='text-align:center;'>Test of various functions.</p>";
        }
}

My problem is that it points to: https://{PATH}/my-account/wp-admin. I need to go to https://{PATH}/wp-admin instead.

2

Answers


  1. You should remove the endpoint from the my-account page using the remove_rewrite_endpoint function. Then, in the custom_account_menu_items function, we add a direct link to the wp-admin page using the admin_url function.

    add_action( 'init', 'add_admin_tools_account_endpoint' );
    function add_admin_tools_account_endpoint() {
        // Remove the endpoint from the my-account page
        remove_rewrite_endpoint( 'wp-admin', EP_PAGES );
    }
    
    add_filter ( 'woocommerce_account_menu_items', 'custom_account_menu_items', 10 );
    function custom_account_menu_items( $menu_links ){
        if ( current_user_can('administrator') ) {
            // Add the link to the wp-admin page
            $menu_links['wp-admin'] = __('Admin tools');
        }
        return $menu_links;
    }
    
    add_action( 'woocommerce_account_menu_items', 'admin_tools_account_menu_link' );
    function admin_tools_account_menu_link( $menu_links ) {
        if ( current_user_can('administrator') ) {
            $menu_links['wp-admin'] = '<a href="' . admin_url() . '">Admin tools</a>';
        }
        return $menu_links;
    }
    
    Login or Signup to reply.
  2. Try this..

    add_action( 'init', 'add_admin_tools_account_endpoint' );
    function add_admin_tools_account_endpoint() {
        add_rewrite_endpoint( 'wp-admin', EP_ROOT );
    }
    
    add_filter ( 'woocommerce_account_menu_items', 'custom_account_menu_items', 10 );
    function custom_account_menu_items( $menu_links ){
        if ( current_user_can('administrator') ) {
            $menu_links = array_slice( $menu_links, 0,0 , true )
                + array( 'wp-admin' => __('Admin tools') )
                + array_slice( $menu_links, 0, NULL, true );
        }
        return $menu_links;
    }
    
    add_action( 'template_redirect', 'admin_tools_account_endpoint_redirect' );
    function admin_tools_account_endpoint_redirect() {
        global $wp_query;
        if ( isset( $wp_query->query_vars['wp-admin'] ) ) {
            wp_redirect( site_url( '/wp-admin/' ) );
            exit;
        }
    }
    
    add_action( 'woocommerce_account_wp-admin_endpoint', 'admin_tools_account_endpoint_content' );
    function admin_tools_account_endpoint_content() {
        if ( current_user_can('administrator') ) {
            echo "<h3 class='headline'>Admin Tools</h3>
                  <p style='text-align:center;'>Test of various functions.</p>";
        }
    }

    In the add_admin_tools_account_endpoint() function, we have changed the EP_PAGES argument to EP_ROOT. This sets the endpoint to the root URL instead of to the account page
    I have added a new function admin_tools_account_endpoint_redirect() that redirects the endpoint to site_url( ‘/wp-admin/’ ) if the wp-admin query var is present in the URL. This is necessary because we have changed the endpoint to the root URL, so we need to manually redirect it to the correct page
    We have changed the action in the add_action() call for displaying the endpoint content to woocommerce_account_wp-admin_endpoint to match the new endpoint name.

    With these changes, the URL for the "Admin tools" link in the account menu should point to https://{PATH}/wp-admin, as desired

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search