为WordPress自定义帖子类型创建自定义设置页面

时间:2014-06-27 00:20:27

标签: wordpress menu settings admin custom-post-type

我有一个我创建的WordPress自定义帖子类型。 CPT创建正常,并成功添加到WordPress菜单。

我使用以下代码创建了CPT:

/**
     * 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'             => '',
            )
        );
    }

我想要实现的是为CPT创建一个自定义设置页面,该页面嵌套在CPT菜单项下,如下面的屏幕截图所示。

该图片演示了我想为CPT创建的自定义设置页面。

enter image description here

我能够添加自定义"设置"使用此代码链接到我的CPT。

/**
* 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'));

}

问题

如果您点击"设置"链接它会将您带到此URL

  

edit.php post_type = wpbp-背景&安培;页= wpbp_options

当您尝试保存此页面的设置时,会发生什么,而这些设置从未实际保存到数据库中。我将此URL修改为页面URL:

  

edit.php post_type = wpbp-背景&安培;页= wpbp_options&安培;设置更新的=真

但我从未看到对话/消息框告诉您设置已保存。

我希望这是有道理的,有人可以提供一些帮助建议。 感谢

编辑 - 添加var_dump和隐藏的表单字段。

我将管理表单发布到:

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

如果添加到表单中, var_dump 的结果如下。

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"
}

表单隐藏字段的结果

这些是通过WP设置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 个答案:

答案 0 :(得分:0)

是否要在选项页面菜单下添加自定义帖子类型链接?

我正在使用此代码作为我的主题。

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'
);
}
相关问题