为WordPress自定义文章类型创建自定义设置页面

4
我有一个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中。
/**
* 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-backgrounds&page=wpbp_options
当您尝试保存此页面的设置时,它们实际上永远不会保存到数据库中。我会将此URL修改为页面URL:
edit.php?post_type=wpbp-backgrounds&page=wpbp_options&settings-updated=true 但我从未看到告诉您已保存设置的对话框/消息框。
希望这有意义,有人可以提供一些帮助和建议。 谢谢
编辑-添加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
没有看到你的代码,很难说。这个页面就像其他页面一样,你可以将表单设置为提交到同一个页面,并使用var_dump($_POST)来查看你传递了什么。 <form action="" method="POST" enctype="multipart/form-data"> 如果你无法让它工作,请发布你的display_admin_page函数,我明天会看一下。 - David
嗨@David,感谢您的回复。我查看了表单,它正在发布到“<form action="options.php" method="post" class="wpbp-form" enctype="multipart/form-data">”。我将把它改为“edit.php”,看看是否可以解决问题。 - Jason
你的$_post函数是什么?update_option() 函数不会出现太多问题,但如果你将一个$var赋值给它,那么如果更新失败(或者相同的值已经存在,因此不需要采取任何操作),它将更新为false。 - David
嗨@David,我不太确定你上面的话是什么意思。我还没有能够让它正常工作。 - Jason
抱歉,如果您编辑问题以显示处理帖子信息的代码,我会为您查看!您是否使用update_option将选项保存到数据库中? - David
显示剩余3条评论
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接