0

Below code adds My Page button in Admin Menu. It will take you to your author page when you click on it. However, it also opens up new page(Empty page).

I want it to just take you to author page and do not open a new page in backend. How do I achieve that? I also make this menu show up for all user roles(it only shows up on Admin role now)

I know my code is wrong. Please let me know if you have any suggestion to write it better.

add_action( 'admin_menu', 'add_my_custom_menu' );

function add_my_custom_menu() {
    //add an item to the menu
    add_menu_page (
        'My Page',
        'My Page',
        'manage_options',
        'my-page',
        'see_my_page_function',
        'dashicons-controls-play',
        '100'
    );
}

function see_my_page_function() {
    $current_user = wp_get_current_user();
    ?>
    <script>
        url = "<?php echo get_site_url().'/author/'.$current_user->user_login.'/'; ?>";
        window.open(url, '_blank');
    </script>
    <?php
}
  • What happens if you remove the second parameter in window.open and just have it as window.open(url) – czerspalace Sep 18 '15 at 20:45
  • Thank you for the comment! That did not work. Some how it acts exactly the same with _blank. I would like to open url on new tab anyways tho. – Ohsik Sep 18 '15 at 20:51
0

I've experimented with various solutions or workarounds proposed elsewhere on this site - for instance here: Adding an Arbitrary Link to the Admin Menu? and this one here: http://www.techedg.com/2014/09/06/5575/a-simple-way-to-add-an-external-link-to-the-wordpress-admin-menu/

All of the solutions that I tested that worked included that behavior of adding an unwanted blank admin page. I'm not sure that there is not some solution buried somewhere - or some trick you can use to close the page after it's been opened! - but the indication is that WP does not want you to do what you want to do.

I think the cleanest solution you can try is to add a link directly to a submenu. You'll find some indications on the pages I linked above.

You might also consider simply adding the link - or the re-direct - via the blank page: So, in other words, cooperate with WordPress load the page, and work it from there: Add a link to the author page that also appears there, initiate some other process, point to the user-profile page, or incorporate other functions for editing the user's profile information.

To answer your minor question, to make the link available to all users, use 'read' instead of 'manage_options' as the third, capabilities variable under the add_menu_page() array.

  • Thank you so much for the answer. I believe you have a typo on "For make the link available to all users", I tried read and it worked :) – Ohsik Sep 19 '15 at 18:41
  • Fixed! If a better answer doesn't show up, please consider marking my answer as an answer - at least on the part that's answerable as asked. ;) – CK MacLeod Sep 19 '15 at 18:48
0

In your case, this will automatically open author url of logged in author:

function add_custom_menu_item_logged_in_author(){
    add_menu_page( 'My Posts', 'My Posts', 'manage_options', 'author-redirect', 'custom_menu_item_redirect', 'dashicons-nametag', 1 );
}
add_action( 'admin_menu', 'add_custom_menu_item_logged_in_author' );


function custom_menu_item_redirect_logged_in_author() {
        global $current_user;
        get_currentuserinfo();
        // change $my_redirect_url if accessing a different url other than author page
        $my_redirect_url = get_author_posts_url($current_user->ID); 
        $menu_redirect = isset($_GET['page']) ? $_GET['page'] : false;

        if($menu_redirect == 'author-redirect' ) {
            wp_safe_redirect( $my_redirect_url );
            exit();
        }

}
add_action( 'admin_init', 'custom_menu_item_redirect_logged_in_author', 1 );

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.