ADD_MENU_PAGE回调函数:跳过页面内容?

时间:2012-07-31 作者:AlxVallejo

我想有一个菜单项,点击后会添加一个带有特定参数的页面。

简单来说,我有:

function av_subscribe_create_menu(){
// Create top-level menu
add_menu_page( \'Add Comment Feed\', \'Add Comment Feed\', \'manage_options\', __FILE__, 
    \'av_subscribe_create_feed_page\', \'\' );
}
add_action( \'admin_menu\', \'av_subscribe_create_menu\' );

function av_subscribe_create_feed_page(){

    $page = array(
      \'post_type\' => \'page\',
      \'post_status\' => \'publish\',
      \'post_parent\' => 13570
    );

    $new_page_id = wp_insert_post($page, false);

}
这不起作用,即使它起作用了,我也不相信它会把我带到编辑屏幕。是否有一种方法可以使用功能添加帖子并直接转到编辑屏幕?

另外,还有一个额外的问题:如果您没有指定标题,但将post\\u状态指定为publish,这会自动为您创建一个slug吗?我不想听到自动生成的胡言乱语。提前谢谢。

1 个回复
SO网友:TheDeadMedic

add_action( \'admin_menu\', \'av_subscribe_create_menu\' );

function av_subscribe_create_menu()
{
    $hook = add_menu_page(
        \'Add Comment Feed\',
        \'Add Comment Feed\',
        \'manage_options\',
        \'av-create-feed\', // Don\'t use __FILE__ as the slug, keep it short \'n sweet!
        \'__return_true\' // We need a callback otherwise WP won\'t properly handle the page, though it\'s never seen
    );


    if ( $hook ) // Current user has the right caps
        add_action( "load-$hook", \'av_subscribe_create_feed_page\' ); // This runs before header output 
}

function av_subscribe_create_feed_page()
{
    $page_id = wp_insert_post( array(
        \'post_status\' => \'publish\',
        \'post_parent\' => 13570,
        \'post_title\' => \'My Title\',
        \'post_type\' => \'page\',
    ));

    if ( $edit_url = get_edit_post_link( $page_id, \'raw\' ) )
        wp_redirect( $edit_url );
    else
        wp_redirect( admin_url( "edit.php?post_type=page" ) ); // Fallback

    exit;
}
如果未指定标题,但将post\\u状态指定为publish,这会自动为您创建slug吗?

当然会的!检查源代码wp_insert_post().

结束

相关推荐