How do I change "add new" button link in the admin panel to something else

Hey, a seemingly simple question here.

I’d like to change the “add new” link in the posts section of the backpanel so that it links to something other than wp-admin/post-new.php.

If possible, I’d like this done only for a specific role, but it’s not necessarily.

I could use a simple redirect, but I was thinking there should be a better way to do it in wordpress. I’m familiar with the menu editor, but there doesn’t seem to be a button editor.

Thanks for any help.

  • Kasia Swiderska
    • Support nomad

    Hello Robert,

    Please check this thread http://wordpress.stackexchange.com/questions/169074/exists-filter-or-action-that-change-add-new-post-link – there is filter that allows to change that button url. You can combine it with code that checks what is current user role and only change url then http://wordpress.stackexchange.com/questions/131814/if-current-user-is-admin-or-editor

    You can then use it as MU plugin https://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/

    kind regards,

    Kasia

  • Robert
    • Site Builder, Child of Zeus

    Thanks for the reply.

    The filter works for normal wordpress installs, but in multisite it causes 500 server errors.

    Also, when combining it with if( current_user_can it activates too soon causing a

    Call to undefined

    error

  • Robert
    • Site Builder, Child of Zeus
    //Change add new product link
    function add_new_post_url( $url, $path, $blog_id ) {

    if ( $path == "post-new.php?post_type=product" ) {
    $path = "edit.php?post_status=private&post_type=product";
    }

    return $path;
    }
    add_filter( 'admin_url', 'add_new_post_url', 10, 3 );

    Again, I didn’t post this in the support forum because it isn’t related to wpmudev, but the post has been moved to support where members outside of the wpmudev team can’t see it.

    Do you guys not want us to use the public forums for such things? If so, that’s fine, but I posted it in the public forum so that I can possibly hear from someone who has done this change already, not so that it can be viewed by a small team with limited time and resources.

  • Ash
    • WordPress Hacker

    Hello Robert

    I hope you are doing good.

    The simplest way to use javascript to change the url of the menu item (not redirect). Are you open to do this? If yes, you can try the following code:

    add_action( 'admin_head', function() {
    ?>
    <script type="text/javascript">
    jQuery(function($) {
    $('a[href="post-new.php?post_type=product"]').attr( 'href', 'edit.php?post_status=private&post_type=product' );
    });
    </script>
    <?php
    } );

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name ‘mu-plugins’. If there is no folder in that name, then create a folder, name it ‘mu-plugins’, create a file inside that, give any name you like and paste the code in there. You don’t need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers

    Ash

  • Rupok
    • Support Ninja

    Hi Robert

    Hope you had a wonderful day.

    I’ve modified the code Ash provided and this will change the *button* link:

    add_action( 'admin_head', function() {
    ?>
    <script type="text/javascript">
    jQuery(function($) {
    $('.wrap .add-new-h2, .wrap .add-new-h2:active, .wrap .page-title-action, .wrap .page-title-action:active').attr( 'href', 'http://www.google.com' );
    });
    </script>
    <?php
    } );

    Replace “http://www.google.com” to your desired URL.

    Can you please confirm if it works for you?

    Have a nice day. Cheers!

  • Ash
    • WordPress Hacker

    Hello Robert

    If you mean the button in screenshot, then the code you used works perfectly fine in my test site:

    function add_new_post_url( $url, $path, $blog_id ) {

    if ( $path == "post-new.php?post_type=product" ) {
    $path = "edit.php?post_status=private&post_type=product";
    }

    return $path;
    }
    add_filter( 'admin_url', 'add_new_post_url', 10, 3 );

    I have just tested in my multisite with a different cpt and worked fine for me. I am also requesting Kasia Swiderska to check the code in her multistie to double sure.

    Have an awesome day!

    Cheers

    Ash

  • Ash
    • WordPress Hacker

    Ahh that makes sense. Please try the following code:

    function add_new_post_url( $url, $path, $blog_id ) {

    if ( $path == "post-new.php?post_type=product" ) {
    return "edit.php?post_status=private&post_type=product";
    }

    return $url;
    }
    add_filter( 'admin_url', 'add_new_post_url', 10, 3 );

    Please let us know if that works.

    Have an awesome day!

    Cheers

    Ash

  • Kasia Swiderska
    • Support nomad

    Hello Robert,

    I tested this code on mu Multisite as MU plugin:

    <?php

    function add_new_post_url( $url, $path, $blog_id ) {

    if ( $path == &quot;post-new.php?post_type=product&quot; ) {
    return &quot;edit.php?post_status=private&post_type=product&quot;;
    }

    return $url;
    }
    add_filter( 'admin_url', 'add_new_post_url', 10, 3 );

    and it changes the button URL:

    (look on the bottom of screenshot – new url when I hover over the button).

    There is small change in the code – it has return $url; instead of the return $path;

    Try to clear browser cache when you add it.

    kind regards,

    Kasia

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.