4
votes

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:

/**
     * 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" />
1
without seeing your code, kinda hard to tell. The page is just like any other page, you can set the form to submit to the same page and var_dump ($_POST) to see what you are passing. <form action="" method="POST" enctype="multipart/form-data"> . If you can't get it working, post your display_admin_page function, ill have a look at that tomorrow.David
Hi @David, thank you for your reply. I had a look at the form and it is posting to "<form action="options.php" method="post" class="wpbp-form" enctype="multipart/form-data">". I will change it to "edit.php" and see if this fixes the problem.Jason
@David I have amended the question and added the results of the hidden form fields and var_dump to the initial question.Jason
The issue seems to be that I have created the options page under the page URL of "edit.php" and not "options.php". This needs to be done to be able to nest the link under the custom post type. If I change the form action to "options.php" I get the attribute &settings-updated=true added to the URL (But the settings are still not updating in the database). If I post the form back to itself it doesn't update the db or add anything to the URl or display and update messages.Jason
whats your $_post function? theres not too much that go wrong with update_option() but if you put a $var equal to it will update false if it fails (or if the same value already exists, so no action needed)David

1 Answers

0
votes

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'
);
}