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 like to learn some of WP automation. The WP get inbound http request to site url and the question is how to create a publication based on post (or get) data structure? How can maintain eventhandler for this and take string from the array of $_POST? to make all of this

wp_insert_post( array(
            'post_title' => $new_post_title,
            'post_content' => $new_post_content,
            'post_status' => ,
            'post_author' => ID,
            'post_date' => $new_post_date,
            'post_category' => $new_post_category,
            'post_type' => ,
        ));  

and get a new post in blog

share|improve this question

2 Answers 2

up vote 3 down vote accepted

As a general note, there's the (you've already used it to tag your question) that has an archive.

WP HTTP API

Here's a list of "How to"-answers about the HTTP API basics

Post Data

Yes, you just use wp_insert_post() and similar API functions to do this.

Authors: Important to note is, that every post will need a user. You can try funny stuff like using -1, etc. but all this won't bring any fun stuff, instead you'll find yourself in lots of trouble. That's why I created "The SysBot"-Plugin - you can grab and use it for free. It's OS.

Meta Data: Here's a pretty short and simple answer that will you guide you around this.

Another solution

As I'm currently working on something like this myself, here's the idea: Create your own WordPress importer. Here's the basic how-to with an already prepared plugin base:

This is the bootstrap.class.php file (or if you prefer WP naming conventions, name it class-bootstrap.php). It registers the importer and loads it.

<?php
defined( 'ABSPATH' ) OR exit;
/**
 * Plugin Name: (#85043) »kaiser« HTTP remote data Importer
 * Description: Fetches remote data via the WP HTTP remote API from the CUSTOM location
 */


add_action( 'plugins_loaded', array( 'CUSTOM_importer_bootstrap', 'init' ), 1 );
class CUSTOM_importer_bootstrap
{
    protected static $instance;
    public static function init()

    {
        null === self :: $instance AND self :: $instance = new self;
        return self :: $instance;
    }

    public function __construct()
    {
        add_action( current_filter(), array( $this, 'load_files' ), 5 );
        add_action( 'admin_init', array( $this, 'register_importer' ) );
    }

    public function load_files()
    {
        include_once plugin_dir_path( __FILE__ ).'importer.class.php';
    }

    public function register_importer()
    {
        if ( ! defined( 'WP_LOAD_IMPORTERS' ) )
            return;

        $GLOBALS['wp_importer'] = new CUSTOM_Import();
        register_importer(
             'custom_importer'
            ,'CUSTOM Importer'
            ,__(
                 'Import from the CUSTOM feed.'
                ,'CUSTOM_importer'
             )
            ,array( $GLOBALS['wp_importer'], 'dispatch' )
        );
    }
}

Here we got the importer.class.php file, that resides in the same directory. It holds the importer class that extends the default WordPress importer. You simply have to fill in your post insertion and are happy to go. Note: Write a parser function as well to prepare your data before inserting. Then write another method to sanitize the data.

<?php
defined( 'ABSPATH' ) OR exit;

# Display verbose errors
! defined( 'IMPORT_DEBUG' ) AND define( 'IMPORT_DEBUG', true );

# Load Importer API
require_once ABSPATH.'wp-admin/includes/import.php';

! class_exists( 'WP_Importer' )
    AND require ABSPATH.'wp-admin/includes/class-wp-importer.php';

class CUSTOM_Import extends WP_Importer
{
    public $nonce_step_1 = 'custom-import-1';
    public $nonce_step_2 = 'custom-import-2';

    public $version;
    public $authors    = array();
    public $posts      = array();
    public $terms      = array();
    public $categories = array();
    public $tags       = array();
    public $base_url   = '';

    // mappings from old information to new
    var $processed_authors    = array();
    var $author_mapping       = array();
    var $processed_terms      = array();
    var $processed_posts      = array();
    var $post_orphans         = array();
    var $processed_menu_items = array();
    var $menu_item_orphans    = array();
    var $missing_menu_items   = array();

    var $fetch_attachments    = false;
    var $url_remap            = array();
    var $featured_images      = array();

    // Every time a core dev does this, a kitten dies
    function CUSTOM_Importer() { /* nothing... of course */ }

    /**
     * Registered callback function for the WordPress Importer
     *
     * Manages the three separate stages of the WXR import process
     */
    function dispatch()
    {
        $this->header();

        $step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step'];
        switch ( $step )
        {
            case 0:
                $this->screen_start();
                break;
            case 1:
                check_admin_referer( $this->nonce_step_1 );

                $this->screen_step_1();

                #if ( $this->handle_upload() )
                #   $this->import_options();

                break;
            case 2:
                check_admin_referer( $this->$this->nonce_step_2 );

                $this->screen_step_2();

                #$this->fetch_attachments = ! empty( $_POST['fetch_attachments'] )
                #   AND $this->allow_fetch_attachments();
                #$this->id = (int) $_POST['import_id'];
                #$file = get_attached_file( $this->id );
                #set_time_limit(0);
                #$this->import( $file );

                break;
        }

        $this->footer();
    }

# =============== MarkUp

    /**
     * Display introductory text and file upload form
     */
    public function screen_start()
    {
        printf(
             '<div class="narrow"><p>%s</p><form id="custom-import-form" method="post" action="%s">%s</form></div>'
            ,__( 'Press button to update manually', 'custom_importer' )
            ,esc_attr( wp_nonce_url( 'admin.php?import=custom_importer&amp;step=1', $this->nonce_step_1 ) )
            ,get_submit_button( __( 'import', 'custom_importer' ), 'button' )
        );
    }

    public function screen_step_1()
    {
        wp_nonce_field( $this->nonce_step_2 );

        printf(
             '<p>%s</p>'
            ,__( 'Step 1', 'custom_importer' )
        );
    }

    public function screen_step_2()
    {
        printf(
             '<p>%s</p>'
            ,__( 'Step 2', 'custom_importer' )
        );
    }

    /**
     * Display import page title
     */
    public function header()
    {
        printf(
             '<div class="wrap">%s<h2>%s</h2>'
            ,get_screen_icon()
            ,__( 'Import CUSTOM posts', 'custom_importer' )
        );
    }

    /**
     * Close the container div
     */
    public function footer()
    {
        echo '</div>';
    }
}
share|improve this answer

Here is an example how to check for one $_POST variable called 'send':

if(isset($_POST['send']) && $_POST['send']=="1"){

    // wp_insert_post stuff

}

and then you can use for example:

wp_insert_post( array(
            'post_title' => wp_kses($_POST['my_post_title']),
            'post_content' => esc_html($_POST['my_post_content']),
            'post_status' => $afip_options['default_post_status'],
            'post_author' => $current_user->ID,
            'post_date' => wp_kses($_POST['my_post_date']),
            'post_category' => intval($_POST['my_post_category']),
            'post_type' => 'post',
        ));  
share|improve this answer

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.