Publishing Extend of WordPress

Not always you want to solve things like WordPress provides and you seek for a different solution. Sometimes you don’t need various applications of the backend to publish posts.
A simple example would be a form, no matter where, where the contents of the form should automatically published on your WordPress blog. Alternatively, you could send it with a different status, and therefore only publish them if the admin approves it.
For a good start, here is the function, which is pretty powerful and for all processes of the publication responsible.

wp_insert_post($postarr = array(), $wp_error = false)

You can find a lot information in the Codex of the function.

Possible parameters $postarr

comment_status    [ 'closed' | 'open' ] // 'closed' means no comments.
ID                [ <post id> ] //Are you updating an existing post?
menu_order        [ <order> ] //If new post is a page, sets the order should it appear in the tabs, Default is 0
page_template     [ <template file> ] //Sets the template for the page.
ping_status       [ value of default_ping_status ] //Ping status?, Default is empty string
pinged            [ empty ] //?
post_author       [ $user_ID ] //The user ID number of the author.
post_category     [ array(<category id>, <...>) ] //Add some categories.
post_content      [ <the text of the post> ] //The full text of the post.
post_date         [ Y-m-d H:i:s ] //The time post was made.
post_date_gmt     [ Y-m-d H:i:s ] //The time post was made, in GMT.
post_excerpt      [ <an excerpt> ] //For all your post excerpt needs.
post_parent       [ <post ID> ] //Sets the parent of the new post, Default is 0
post_password     [ empty ] //password for post?, Default is empty string
post_status       [ 'draft' | 'publish' | 'pending' ] //Set the status of the new post., Default is 'draft'
post_title        [ <the title> ] //The title of your post.
post_type         [ 'post' | 'page' ] //Sometimes you want to post a page, Default is 'post'.
tags_input        [ '<tag>, <tag>, <...>' ] //For tags.
to_ping           [ whetever ] //Whether to ping.


In the simplest case, you give the contents of the form to the array and it uses this function, which takes care of the processes in WordPress.

To have this function also outside of WordPress available, the functionality of WordPress have to be fetched in this area. If the form is not realized within a template in WordPress, but outside of WP, then you have to implement the file wp-load.php, for more information read the article „Embed WordPress Functions Outside WordPress“.

require( '../my_wordpress_install_root/wp-load.php' );

// Create post object
$my_post = array();
$my_post['post_title']    = 'My post';
$my_post['post_content']  = 'This is my post.';
$my_post['post_status']   = 'publish';
$my_post['post_author']   = 1;
$my_post['post_category'] = array(0);

// Insert the post into the database
wp_insert_post( $my_post );"

This makes a lot of scenarios available and maybe it helps to realize one or other idea.

Comments are closed.


  1. Christopher Ross

    Hey Frank, great summary!

    I use this functionality weekly along with an RSS grab to automatically publish a list of the top iTune applications from Apple. It’s a pretty straight forward way to make sure the post is generated from the source and lets me then edit it before putting it live.

  2. NFL Rumors

    I might have to try this out on my WordPress Blog. I’m not very good with code but with a little help and shouldn’t be too difficult.

  3. Michael

    @Christopher: I don’t know why, but you was the second time marked as spam.

  4. Peter

    I was under the impression that even if you included wp-load.php, you’d also have to include wp-admin/admin-functions.php to get wp_insert_post(). Is that not the case? I’ll have to give it a try…

  5. Peter

    I just ran into it on a project I’m working on – wp_insert_post() is in fact not on the admin side (it’s in wp-includes/post.php), so just including wp-load.php should be plenty. I never should have second guessed you:)

  6. Alex

    @Peter, now you know 😉

  7. TJ Singleton

    It’s great for populating post from csv files.

  8. Francesco Gallarotti

    Let’s say I have a page that uses this method to post articles on my WP installation (I tried and it works). How would I go to protect that page so that I have to be an admin of that installation of WP in order to be able to access it?

  9. Francesco Gallarotti

    Nevermind, I found it:

    global $current_user;
    echo 'User email: ' . $current_user->user_email . "\n";
    echo 'User level: ' . $current_user->user_level . "\n";
    echo 'User first name: ' . $current_user->user_firstname . "\n";
    echo 'User last name: ' . $current_user->user_lastname . "\n";
    echo 'User display name: ' . $current_user->display_name . "\n";
    echo 'User ID: ' . $current_user->ID . "\n";

One pingback

  1. wp_insert_post(), la insercción de posts en Wordpress : Blogografia