How to create submenu and option page of custom post type?

To add the submenu item under any custom post type page, First you will need to create a submenu item under custom post type menu section and then need to create a option page.

To add a new Sub-menu to WordPress Administration, use the add_submenu_page() function.

add_submenu_page( string $parent_slug, string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '', int $position = null )

add_submenu_page() function takes a capability which will be used to determine whether or not a page is included in the menu.

Parameters #Parameters

$parent_slug
(string) (Required) The slug name for the parent menu (or the file name of a standard WordPress admin page).

$page_title
(string) (Required) The text to be displayed in the title tags of the page when the menu is selected.

$menu_title
(string) (Required) The text to be used for the menu.

$capability
(string) (Required) The capability required for this menu to be displayed to the user.

$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().

$function
(callable) (Optional) The function to be called to output the content for this page.

Default value: ”

$position
(int) (Optional) The position in the menu order this item should appear.

Default value: null


Example:

For a example, we have already created a gallery custom post type and now we are going to create settings page of gallery.

To add the submenu item under custom post type menu, we have need to add given below code in your theme function.php file

/*  Create new submenu item */
add_action('admin_menu', 'my_post_type_submenu');

function my_post_type_submenu()
{
add_submenu_page(
        'edit.php?post_type=ve_gallery',
        __( 'Settings', 'virtualemployee' ),
        __( 'Settings', 'virtualemployee' ),
        'manage_options',
        'gallery-options',
        'gallery_settings_page_callback'
    );

}

//lets register settings option of the gallery page

add_action( 'admin_init', 'register_image_gallery_settings' );
function register_image_gallery_settings()
{

	register_setting( 'gallery-settings', 'new_option_name' );
	register_setting( 'gallery-settings', 'some_other_option' );
	register_setting( 'gallery-settings', 'option_etc' );
}

function gallery_settings_page_callback(){
// settings page

?>
<div class="wrap">
<h1>Settings Page</h1>

<form method="post" action="options.php">
    <?php settings_fields( 'gallery-settings' ); ?>
    <?php do_settings_sections( 'gallery-settings' ); ?>
    <table class="form-table">
        <tr valign="top">
        <th scope="row">New Option Name</th>
        <td><input type="text" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td>
        </tr>
         
        <tr valign="top">
        <th scope="row">Some Other Option</th>
        <td><input type="text" name="some_other_option" value="<?php echo esc_attr( get_option('some_other_option') ); ?>" /></td>
        </tr>
        
        <tr valign="top">
        <th scope="row">Options, Etc.</th>
        <td><input type="text" name="option_etc" value="<?php echo esc_attr( get_option('option_etc') ); ?>" /></td>
        </tr>
    </table>
    
    <?php submit_button(); ?>

</form>
</div>
<?php
}

All the best

Leave a Reply

Your email address will not be published. Required fields are marked *