Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to have a menu item that will, on-click, add a page with certain arguments.

In it's simplicity, I have:

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

}

This doesn't work and even if it did, I don't believe it would take me to the Edit screen. Is there a way you can use a function to add a post and go directly to the edit screen?

Also, bonus question: If you do not specify a title, but you specify the post_status as publish, will this automatically create a slug for you? I don't want an auto-generated slug of some gibberish. Thanks in advance.

share|improve this question
    
Who the hell downvoted? Legit question, no? –  TheDeadMedic Jul 31 '12 at 20:07

1 Answer 1

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

If you do not specify a title, but you specify the post_status as publish, will this automatically create a slug for you?

It certainly will! Check the source code for wp_insert_post().

share|improve this answer
    
Wow this is excellent. I'm getting a 'You do not have sufficient permissions to access this page.' tho. –  AlxVallejo Jul 31 '12 at 20:17
    
What's the URL in the address bar? (you can strip out your domain name if you wish) –  TheDeadMedic Jul 31 '12 at 20:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.