是否可以处理$_POST/入站http请求以自动创建WordPress帖子?

时间:2013-02-08 作者:Yurij73

我喜欢学习一些WP自动化。WP get入站http请求到站点url,问题是如何基于post(或get)数据结构创建发布?如何为此维护eventhandler并从$\\u POST数组中获取字符串?要做到这一切

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\' => ,
        ));  
并在博客中获得新帖子

1 个回复
最合适的回答,由SO网友:kaiser 整理而成

总的来说 (您已经使用它来标记您的问题)具有存档。

WP HTTP API以下是关于HTTP API基础知识的“操作”答案列表

发布数据

是的,您只需使用wp_insert_post() 和类似的API函数来实现这一点。

Authors: 需要注意的是,每个帖子都需要一个用户。你可以尝试一些有趣的东西,比如-1, 但这一切都不会带来任何有趣的东西,相反,你会发现自己有很多麻烦。这就是我创建"The SysBot"-Plugin - 您可以免费获取和使用它。这是操作系统。

Meta Data: Here\'s a pretty short and simple answer 这将指导你解决这个问题。

另一个解决方案是:创建自己的WordPress导入器,因为我目前正在研究类似的东西。以下是已经准备好的插件库的基本操作方法:

这是bootstrap.class.php 文件(或者如果您喜欢WP命名约定,请命名它class-bootstrap.php). 它注册并加载导入程序。

<?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\' )
        );
    }
}
我们得到了importer.class.php 文件,该文件位于同一目录中。它包含扩展默认WordPress导入器的导入器类。您只需填写插入后的内容,就可以很高兴地离开。注意:还要编写一个解析器函数,以便在插入之前准备数据。然后编写另一个方法来清理数据。

<?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>\';
    }
}

结束

相关推荐

How deactivate the http-api

为它提供一个tipp或解决方案来停用WP\\U Http\\U Streams类中的方法request()?我也在脱机服务器上使用WordPress,并让wp\\U debug true用于开发和测试。但是我从函数中得到了很多关于使用http类的警告;例如,在仪表板中读取提要的函数。目前我已经停用了更新主题、插件、核心和cron的所有挂钩;请参阅我的小插件:https://github.com/bueltge/WP-Offline谢谢你的回复