WordPress.org

Support

Support » Plugins and Hacks » [Resolved] How to create pages programmatically?

[Resolved] How to create pages programmatically?

  • Hi all,
    I would like to know how to create a WordPress page programmatically. I would like to set the page title, the desc and the template to use, and create the page with PHP code. I tried to hack and directly insert rows in WordPress tables, but it failed to work properly.
    Any idea is welcomed 🙂
    Thanks in advance,
    Nicolas

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi

    The WP code that inserts the initial posts after a WP install is in this file wp-admin/includes/upgrade.php You can use it as a starting point

    Here you go,

    global $user_ID;
    
    $page['post_type']    = 'page';
    $page['post_content'] = 'Put your page content here';
    $page['post_parent']  = 0;
    $page['post_author']  = $user_ID;
    $page['post_status']  = 'publish';
    $page['post_title']   = 'Your Page Title';
    $page = apply_filters('yourplugin_add_new_page', $page, 'teams');
    $pageid = wp_insert_post ($page);
    if ($pageid == 0) { /* Add Page Failed */ }

    Shows up in the menu and everything.
    You can add other variables to the $page array as well, but this is all you need. Just take a look at the posts database table for all the options you could use.

    Keep on Keepin’ On! 😛

    Funny… Had forgotten this ticket opened 6 months ago, but had the same need a few days ago… Check it and realized you posted an answer 1 day ago 🙂

    And your solution works like a charm !!! Thanks a lot.

    @shawnkltucker – I would love to use this code but I have no idea what includes are needed to get it to work or if the data needs to be posted to a specific URL etc. Noob here 🙂

    I see leroy12 is happy so I guess he is skilled enough to know how to utilise this snippet of code.

    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] How to create pages programmatically?’ is closed to new replies.
Skip to toolbar