1

I created a custom post type and I hide it

using register_post_type()

and a menu page using

add_menu_page() and add_submenu_page()

the link for the custom post type is page=edit.php?post_type=survey

and for the menu page is admin.php?page=my_survey

I hide the custom post type because I don't want to show it, I just want to have one menu but with a link to the sub menu page that goes to the custom post type

the problem is that all the links in the menu have the prefix admin.php?page= when I add the link in the

menu_slug => 'edit.php?post_type=survey'

it adds admin.php?page=edit.php?post_type=survey

is it anyway that I can remove that prefix from just one sub menu ?

I'm working on a OOP this is how I add the submenu

  public function setSubPages()
  { 
    $this->subpages = [
      [
        'parent_slug'  => 'survey',
        'page_title'  => 'Survey Plugin',
        'menu_title'  => 'Survey',
        'capability'  => 'manage_options',
        'menu_slug'   => 'edit.php?post_type=survey',
        'callback'    =>   [$this->callbacks, 'adminDashboard'],
      ];
  }

and my custom post type

  public function activate()
  {
    $labels =  [
      'name' => 'survey',
      'singular_name' => 'survey',

    ];

    $args = [
      'labels' => $labels,
      'public' => true,
      'has_archive' => false,
      'menu_icon' => 'dashicons-email-alt',
      'supports' => false,
      'exclude_from_search' => true,
      'publicly_queryable' => false,
      'show_in_menu'       => false
    ];

    register_post_type( 'survey', $args );

  }

if I wasn't clear enough please let me know thanks

Blackbam
  • 17,496
  • 26
  • 97
  • 150
Marcogomesr
  • 2,614
  • 3
  • 30
  • 41

2 Answers2

2

This is how I solve the problem, for those are having the same problem hope that helps

custom post type

      public function activate()
      {
        $labels =  [
          'name' => 'Email List',
          'singular_name' => 'Email List',

        ];

        $args = [
          'labels' => $labels,
          'public' => false,
          'has_archive' => false,
          'menu_icon' => 'dashicons-email-alt',
          'supports' => false,
          'exclude_from_search' => true,
          'publicly_queryable' => false,
          'show_in_menu'       => 'survey_plugin'  // <----- admin page menu_slug
        ];

        register_post_type( 'surveyemail', $args );

      }

admin survey page

public function setPages()
  { 
    $this->pages = [
      [
        'page_title'  => 'Survey Plugin',
        'menu_title'  => 'survey',
        'capability'  => 'manage_options',
        'menu_slug'   => 'survey_plugin', // same as show_in_menu custom post type
        'callback'    =>   [$this->callbacks, 'adminDashboard'],
        'icon_url'    => 'dashicons-format-aside',
        'position'    => 25
      ]
    ];

  }

  public function setSubPages()
  { 
    $this->subpages = [
      [
        'parent_slug'  => 'survey_plugin', // match menu_slug $this->page
        'page_title'  => 'Welcome',
        'menu_title'  => 'Welcome page',
        'capability'  => 'manage_options',
        'menu_slug'   => 'Welcome',
        'callback'    =>   [$this->callbacks, 'adminDashboard'],
      ],
      [
        'parent_slug'  => 'survey_plugin',// match menu_slug $this->page
        'page_title'  => 'feedback',
        'menu_title'  => 'feedback',
        'capability'  => 'manage_options',
        'menu_slug'   => 'feedback',
        'callback'    =>   [$this->callbacks, 'adminDashboard'],
      ]


    ];
  }

enter image description here

Marcogomesr
  • 2,614
  • 3
  • 30
  • 41
0

First of all there is no sense in having public set to true if you unset each of its effects in the parameters afterwards (https://codex.wordpress.org/Function_Reference/register_post_type).

Second thing: You can not use the menu slug in this way because it is defined as follows:

$menu_slug

(string) (Required) The slug name to refer to this menu by. Should be unique for this menu and only include lowercase alphanumeric, dashes, and underscores characters to be compatible with sanitize_key().

However there is a way to create custom submenu URLs. Please check the following thread on SO WordPress for further information: https://wordpress.stackexchange.com/questions/83768/add-menu-and-submenu-in-admin-with-a-url-instead-of-slug?answertab=votes#tab-top

Be careful what you do in WP-Admin though, if you want to have a post type editable in WP-Admin it is usually the best way to have it editable by parameters.

Blackbam
  • 17,496
  • 26
  • 97
  • 150