michaeluno / admin-page-framework

Facilitates WordPress plugin and theme development.
http://admin-page-framework.michaeluno.jp/
Other
337 stars 71 forks source link

Couldn't attached custom post type edit.php?post_type=mycpt to main menu with add_submenu() #288

Closed champsupertramp closed 3 years ago

champsupertramp commented 3 years ago

Type of issue

Prerequisites

Steps to reproduce the issue (for Bug report)

  1. I created my own CPT with register_post type()
  2. I tried adding the CPT url edit.php?post_type=mycpt to the following Root Menu:
    $this->setRootMenuPage( 'Subscriptions','dashicons-welcome-widgets-menus', 44 );
    $this->addSubMenuItem(
            array(
                'title'        => 'Dashboard',
                'page_slug'    => 'cnum_subscriptions',
                ''
            )
        );
  3. And then use the code snippets below to register the CPT URL.
    $this->addSubMenuItem(
            array(
                'title'    =>    __( 'Subscriptions', '' ),
                'href'    =>    admin_url('edit.php?post_type=cnum_subscriptions'),
                'show_page_heading_tab'    =>    false,
            )
    ); 

And the result is: it is only show as a link and doesn't seem to highlight the submenu.

I tried using the add_submenu_page to register it with the root page cnum_subscriptions but it doesn't add the CPT submenu to the Root Menu Dashboard. What could be the right action hook so that it registers the add_submenu_page

Here's the code for adding the submenu:

$rootMenu = 'cnum_subscriptions';
add_submenu_page(  $rootMenu, __( 'Subscriptions', um_subscriptions_textdomain ), __( 'Subscriptions', 'subs' ), 'manage_options', 'edit.php?post_type=cnum_subs' , false );

Thanks for this great open-source framework.

michaeluno commented 3 years ago

Hi,

Use the show_in_menu custom post type argument. For an existing admin page root menu created with Admin Page Framework, pass the admin page class name as it serves as the menu slug.

I've uploaded example plugins.

champsupertramp commented 3 years ago

Hi @michaeluno Thanks for your attention.

I tried the following code snippet but unfortunately, the submenu don't reigster. The post type is created and the root menu is there. It's just the submenu doesn't appear in the parent menu.

class APFDemo_CPTSubmenu_AdminPage extends AdminPageFramework {

    public function setUp() {

        $this->setRootMenuPage( 'CPT Demo - CPT Submenu','dashicons-welcome-widgets-menus', 100 );
        $this->addSubMenuItems(
            array(
                'title'        => 'Main',
                'page_slug'    => 'apf_demo_cpt_submenu_main',
                'order'        => 10,
            )
        );

    }

    /**
     * @callback do_{page slug}
     */
    public function do_apf_demo_cpt_submenu_main() {
        echo "<p>Hello.</p>";
    }

}

class APFDemo_CPTSubmenu_PostType_A extends AdminPageFramework_PostType {

    public function setUp() {
        $this->setArguments(
            array(
                'labels'                => array(
                    'name'                  => 'CPT Submenu A',
                    'menu_name'             => 'CPT Submenu A',
                ),
                'show_ui'               => true,
                'show_in_menu'          => 'APFDemo_CPTSubmenu_AdminPage',
                'submenu_order_manage'  => 20,
            )
        );
    }

}

new APFDemo_CPTSubmenu_AdminPage;

new APFDemo_CPTSubmenu_PostType_A('cpt_newtest', null, __FILE__);
michaeluno commented 3 years ago

In your code, extendsAdminPageFramework { will code an error. Try the examples shown on Gist linked in my previous reply. They can be downloaded as a zip file and run as a plugin. Install them on your test site with the Admin Page Framework - Loader plugin activated.

champsupertramp commented 3 years ago

Hi @michaeluno

It's a typo with my example but it's already set as extends AdminPageFramework in the plugin that i'm working on. I'm also able to create root pages, section and fields.

Regards,

michaeluno commented 3 years ago

Try the example plugins on a test site which only activates the Admin Page Framework - Loader plugin, NOT on your development plugin in order to eliminate any possible factors which cause problems.

The steps you need to take is:

  1. Create a test site with no plugins and with the default theme.
  2. Install and activate the Admin Page Framework - Loader plugin.
  3. Install and activate the example plugin.

And see what happens. If it doesn't give you a desired result, post screenshots.

champsupertramp commented 3 years ago

Hi @michaeluno

Good news! I'm able to make it work now within the development plugin. It was an issue with my class calls. I've changed it from 'show_in_menu' => 'Admin_Settings', to 'show_in_menu' => '\The_Subscription\Admin_Settings',

Thank you very much for your guidance!

Regards,

michaeluno commented 3 years ago

Okay, great.