0

I would like to completely reorganize, stylize a custom post type. By default register_post_type() function add Title, editor, category and tags into add custom post from $supports argument.

Can I prevent the creation of these elements? Or can I overwrite the generated page for custom Post type ?

Same question for custom Post list. Currently I use add_menu_page and add_submenu_page but they can be linked to a custom post list.

I try adding only these parameters, but it doesn't work:

function atv_post_type(){
  $args = array(
  //'labels' => $labels,
  //'supports' => array(),
  'has_archive' => true,
  'public' => true,
  'hierarchical' => false,
  'menu_position' => 7,
  'menu_icon' => 'dashicons-admin-multisite',
  'rewrite'   => array( 'slug' => 'test-post' ),
  'show_in_rest' => true,
  'taxonomies' => array( 'post_tag' ),
);
register_post_type( 'test-post', $args );
}
add_action( 'init', 'atv_post_type' );
6
  • The solution you've proposed cannot be implemented, however the problem you have can still be solved, e.g. removing the menus and redirecting people who go there directly to the add new page screen is possible. The result is the same but the actions taken are very different.
    – Tom J Nowell
    Commented Nov 6, 2023 at 16:28
  • I managed to delete default Title and editor using 'supports' => array(''), but I think it's really not good thing. Can you point me in the direction of the method you have in mind?
    – imagIne
    Commented Nov 6, 2023 at 16:44
  • I don't understand "removing the menus" ? are you referring to that: Currently I use add_menu_page and add_submenu_page but they can be linked to a custom post list.? I'm trying to change my method
    – imagIne
    Commented Nov 6, 2023 at 16:52
  • you can ask that there be no admin UI when you register the post type, then create your own menu entries, or you can use the counterparts of add_menu_page, aka remove_menu_page
    – Tom J Nowell
    Commented Nov 6, 2023 at 20:13
  • Ok, but which parameter of add_menu_page refer to Custom post type with register_post_type()? As I say in my first question I used these function and I already had custom pages. But now I don’t find how to link register_post_type() with add_menu_page().
    – imagIne
    Commented Nov 7, 2023 at 11:21

2 Answers 2

0

Hiding the admin UI that WP generates is trivial and well documented in the official WP Developer docs for register_post_type:

https://developer.wordpress.org/reference/functions/register_post_type/

show_ui bool

Whether to generate and allow a UI for managing this post type in the admin. Default is value of $public.

and

show_in_menu bool|string

Where to show the post type in the admin menu. To work, $show_ui must be true. If true, the post type is shown in its own top level menu. If false, no menu is shown. If a string of an existing top level menu ('tools.php' or 'edit.php?post_type=page', for example), the post type will be placed as a sub-menu of that.

Default is value of $show_ui.

Using these two mechanisms you can disable all UI, or just remove the menu items core adds. Then you can construct your own pages.


But now I don’t find how to link register_post_type() with add_menu_page()

You don't, the link does not and never did exist. When you call register_post_type it checks those parameters I just mentioned and adds the menus using the very same functions that you were using for your custom pages.

After removing them nothing prevents you from inserting your own.

Importantly you will want to note down the URL of the add new cpt page, and you'll need the edit_link function to generate URLs to edit the posts. With that you can build a brand new page to replace it. There is no special linkage that needs to be done to tell WP that your new page is the posts page, both are just pages in the admin area.

0

Tom, thanks. I was partially successful, but I'm not sure I used all the necessary functions to make it perfect.

I've also understood that it's not the register_post_type that is being modified, but this is this last one that is assigned to add_menu_page() and add_submenu_page().

With the below script, if I use only add_submenu_page() without add_menu_page, it works almost perfectly (display add-org and list-org) except that default submenu items "all post" and "new post" always show up.

The two first in the image.

Organization post type menu

I read here that a custom css can hide default items (edit-post and add-post). Is it a good solution ?

function atv_organization_post_type(){
    $labels = array(
        'name'               => 'Organizations',
        'singular_name'      => 'Organization',
        'add_new'            => 'Add Organization',
        'add_new_item'       => 'Add Organization',
        'edit_item'          => 'Modify Organization',
        'new_item'           => 'New',
        'all_items'          => 'All',
        'view_item'          => 'Show Organization',
        'search_items'       => 'Search',
        'not_found'          => 'No Organization',
        'not_found_in_trash' => 'Nothing',
        'parent_item_colon'  => '',
        'menu_name'          => 'All Organization',
    );
$args = array(
      'labels' => $labels,
      'public' => true,
      'menu_icon' => 'dashicons-admin-multisite',
      'has_archive' => true
  );
  register_post_type( 'organization-post', $args );
}
add_action('init','atv_organization_post_type');


add_action( 'admin_menu', 'atvam_cp_plugin','manage_options');

function atvam_cp_plugin() {
    add_submenu_page( '/edit.php?post_type=org_post_type',  'All Organization', 'All Organization', 'manage_options', 'org-list','atv_org_list');
    add_submenu_page( '/edit.php?post_type=org_post_type',  'Add Organization', 'Add Organization', 'manage_options', 'add_org','atv_org_add');
}

function atv_org_list(){
    include plugin_dir_path( __FILE__ ) . 'adminpages/org-list.php'; 
}

function atv_org_add(){
      include plugin_dir_path( __FILE__ ) . 'adminpages/add-org.php';
}

--------------- UPDATE -----------

So I complete my answer with one solution . (If someone has a best solution I'll take it.)

  1. add show_in_menu in $args to not show clone items/links (list and add post) :

    $args = array( 'show_in_menu' => false );

  2. add add_menu_page() to replace menu generated by register_post_type():

    add_menu_page('All organizations','All organizations', 'manage_options','edit.php?post_type=organization-post','atv_org_list', 'dashicons-admin-multisite', 6);

  3. Hide the submenu item generated by add_menu_page(), because the link used is weird * and impossible to adapt while the menu link is correct.

  • It displayed this link = https://website.org/wp-admin/edit.php?post_type=org_post_type&page=edit.php?post_type=org_post_type

To hide this generated submenu I target id of top level menu class adding :

li#toplevel_page_edit-post_type-org_post_type ul.wp-submenu li.wp-first-item {
    display: none !important;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.