removing dashboard submenu in custom posts

152 views
Skip to first unread message

Angel Utset

unread,
Nov 17, 2012, 3:38:58 AM11/17/12
to manchester-word...@googlegroups.com

Hi

 

I have made a plugin for a custom post type. The registered user (custom role) introduces the custom-post features through a form appearing in his admin dashboard, contained in a specific dashboard widget. I have customized the dashboard admin bar y menu, according to the role.

I gave “read” capabilities to the role. Hence, the user can edit his custom posts, as well as read the other users’ posts.

Everything is fine, but the “Add new” submenu. User can create a new custom post from such submenu, whilst I want them to introduce the data in the dashboard widget I created, instead. Both “Add” and “Edit” submenus are created when user has “read” capabilities.

I need to remove the “Add new” submenu, whilst keeping the rest unchanged.

I have reviewed several options, but none seems to fit my case, since I have a custom post.

 

I will appreciate any help!!!

 

Cheers,

 

Angel

 

 

 

Mike Little

unread,
Nov 26, 2012, 12:23:31 PM11/26/12
to manchester-word...@googlegroups.com
Try something like this:

// remove the menu
add_action( 'admin_menu', 'cpt_hide_add_new_cpt_menu' );
function cpt_hide_add_new_cpt_menu() {
    global $submenu;
if ( is_admin() && isset( $submenu['edit.php?post_type=cpt'][10] ) ) {
unset( $submenu['edit.php?post_type=cpt'][10] );
}
}

// remove the add-new link at the top of the list and edit/new pages
add_action('admin_head', 'cpt_hide_add_new_cpt_link');
function cpt_hide_add_new_cpt_link() {
    global $pagenow, $post_type;
if ( ( $pagenow == 'edit.php' ) || ( $pagenow == 'post.php' ) && isset( $post_type ) && ( 'cpt' == $post_type ) ) {
echo "
<style type=\"text/css\">
.add-new-h2 { display: none; }
</style>
";
}
}

// remove from admin bar
add_action( 'admin_bar_menu', 'cpt_admin_bar_menu', 75 );
function cpt_admin_bar_menu( $wp_admin_bar) {
$wp_admin_bar->remove_node('new-cpt');
}


Just replace 'cpt' with the slug of your custom post type

Mike
--
Mike Little
http://zed1.com/

Angel Utset

unread,
Nov 26, 2012, 2:35:20 PM11/26/12
to manchester-word...@googlegroups.com

Thanks!!. I will review your code.

Actually I had found a solution, removing completely the edit-add menu for the custom post and creating another menu for the edit only. It works. I hided the add-new h2 button also.

 

The code I wrote is (sorry for the Spanish!):

 

add_action( 'admin_menu', 'remove_menu_ofertas' ); // Quita todo el menu ofertas

 

            function remove_menu_ofertas() {

                        remove_menu_page('edit.php?post_type=oferta');       

                       

            }

 

add_action('admin_menu', 'edit_oferta_menu_page'); // Añade un menu que permite editar la oferta

 

function edit_oferta_menu_page() {

   add_menu_page('edit oferta menu title', 'Edit oferta', 'edit_ofertas', 'edit.php?post_type=oferta&author=6', '',   0);

}

 

function hide_buttons() // Esconde la opcion de addnew en Editar oferta

{

  global $current_screen;

 

  echo '<style>.add-new-h2{display: none;}</style>'; 

 

}

add_action('admin_head','hide_buttons');

 

This solution seems to work properly. Thanks again!

 

The dashboard language is personalized according to each’ user preferences, as I wrote previously, using a plugin I made.

 

Angel


--
See the group blog at http://mwug.info
 
You received this message because you are subscribed to the Google
Groups "Manchester WordPress User Group" group.
To post to this group, send email to
manchester-word...@googlegroups.com
To unsubscribe from this group, send email to
manchester-wordpress-...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/manchester-wordpress-user-group
 
 

Angel Utset

unread,
Nov 28, 2012, 4:27:44 AM11/28/12
to manchester-word...@googlegroups.com

Hi Mike

 

I tried to follow your code, but after removing the “Add new” menu I can’t reach the edit menu: “You do not have sufficient permissions to access this page”

 


De: manchester-word...@googlegroups.com [mailto:manchester-word...@googlegroups.com] En nombre de Mike Little
Enviado el: 26 November 2012 17:24
Para: manchester-word...@googlegroups.com
Asunto: Re: [MWUG] removing dashboard submenu in custom posts

 

On 17 November 2012 08:38, Angel Utset <angel...@gmail.com> wrote:

--

Mike Little

unread,
Nov 28, 2012, 6:08:38 AM11/28/12
to manchester-word...@googlegroups.com
On 28 November 2012 09:27, Angel Utset <angel...@gmail.com> wrote:

Hi Mike

 

I tried to follow your code, but after removing the “Add new” menu I can’t reach the edit menu: “You do not have sufficient permissions to access this page”



That must be something to do with the custom roles you have created and the way the capabilities map to the custom post type.

This example CPT here: https://pastebin.com/Ap5E7fyV works fine. I tried logging in as an author and as an editor and things worked as expected.


Mike
Reply all
Reply to author
Forward
0 new messages