Create Custom Settings Page for WordPress Custom Post Type

I have a WordPress Custom Post Type that I have created. The CPT created fine and was added successfully to the WordPress menu.

I created the CPT with this code:

Read More
/**
     * Registers the WPBP custom post types
     */
    function register_wpbp_post_type(){
        register_post_type($this->cpt,
        array(
            'labels' => array(
                'name'                  => __('Backgrounds', $this->prefix),
                'singular_name'         => __('Background', $this->prefix),
                'menu_name'             => __('WP BG Pro', $this->prefix),
                'add_new'               => __('Add New', $this->prefix),
                'add_new_item'          => __('Add New Background', $this->prefix),
                'edit_item'             => __('Edit Background', $this->prefix),
                'new_item'              => __('New Background', $this->prefix),
                'view_item'             => __('View Background', $this->prefix),
                'search_items'          => __('Search Background', $this->prefix),
                'not_found'             => __('No Background Found', $this->prefix),
                'not_found_in_trash'    => __('No Background Found In Trash', $this->prefix),
                'parent_item_colon'     => ''
            ),
            'public'                => false,
            'publicly_queryable'    => false,
            'hierarchial'           => false,
            '_builtin'              => false,
            '_edit_link'            => 'post.php?post=%d',
            'show_ui'               => true,
            'exclude_from_search'   => true,
            'show_in_nav_menus'     => false,
            'capability_type'       => 'post',
            'can_export'            => true,
            'has_archive'           => false,
            'supports'              => array('title'),
            'menu_icon'             => '',
            )
        );
    }

What I am trying to achieve is to create a custom Settings page for the CPT that is nested under the CPT menu item as screenshot-ed below.

The image demonstrates the custom settings page that I want to create for the CPT.

enter image description here

I was able to add the custom “Settings” link to my CPT with this code.

/**
* Add a link to the WordPress menu
*/
public function add_option_page() {

    add_submenu_page('edit.php?post_type=wpbp-backgrounds', 'WP Backgrounds Pro', __('Settings', $this->hook), $this->accesslvl, 'wpbp_options', array(&$this, 'display_admin_page'));

}

The Issue

If you click on the “Settings” link it takes you to this URL

edit.php?post_type=wpbp-backgrounds&page=wpbp_options

What happens when you try and save the settings for this page they never actually save to the database. I get this URL amended to the page URL:

edit.php?post_type=wpbp-backgrounds&page=wpbp_options&settings-updated=true

but I never see the dialogue/message box that tells you that the settings have been saved.

I hope this makes sense and someone could offer some helping advise.
Thanks

EDIT – Addition of var_dump and hidden form fields.

I am posting the admin form to:

<form action="" method="post" class="wpbp-form" enctype="multipart/form-data">

Here are the results of the var_dump if added to the form.

array(6) {
  ["option_page"]=> string(12) "wpbp_options"
  ["action"]=> string(6) "update"
  ["_wpnonce"]=> string(10) "2584accf5f"
  ["_wp_http_referer"]=> string(77) "/wordpress-dev/wp-admin/edit.php?post_type=wpbp-backgrounds&page=wpbp_options"
  ["wpbp_options"]=>
  array(3) {
    ["default_background"]=> string(1) "0"
    ["in_types"]=> string(5) "Posts"
    ["in_taxonomies"]=> string(8) "Category"
  }
  ["Submit"]=> string(12) "Save Changes"
}

Results of form hidden fields

These are the hidden fields that are automatically added to the form through the WP Settings API.

<input type='hidden' name='option_page' value='wpbp_options' />
<input type="hidden" name="action" value="update" />
<input type="hidden" id="_wpnonce" name="_wpnonce" value="2584accf5f" />
<input type="hidden" name="_wp_http_referer" value="/wordpress-dev/wp-admin/edit.php?post_type=wpbp-backgrounds&amp;page=wpbp_options" />

Related posts

Leave a Reply

1 comment

  1. Do you want to add custom post type link under the options page menu?

    I’m using this code for my themes.

    add_action('admin_menu', 'nivo_register_my_custom_submenu_page');
    function nivo_register_my_custom_submenu_page() {
    add_submenu_page( 
        'nivothemes-settings', //options page url
        __('Taksit Tablosu İçeriği', 'nivothemes'),
        __('Taksit Tablosu', 'nivothemes'),
        'manage_options', 
        'edit.php?post_type=installement'
    );
    }