Create Post in Wordpress with PHP code

1

Someone knows how to create a post in Wordpress by means of some PHP script, I need to create semi-automatic posts in a web periodically, my idea is to create a php code in the same web with which by means of a cron it is activated Every once in a while and a clear post is created, sending it the title content featured image and tags if someone can help me please with something that will guide me I would really appreciate it

    
asked by Jose Rodriguez 10.03.2017 в 04:36
source

1 answer

3

There are several possibilities:

A. USING THE WORDPRESS FUNCTIONS

You could not only create, but also update. See:

  • $ post
  • wp_insert_post ()
  •   

    Some or some include are necessary

    Sample code

    if ( ! function_exists( 'PostCreator' ) ) {
    
        function PostCreator(
            $name      = 'AUTO POST',
            $type      = 'post',
            $content   = 'DUMMY CONTENT',
            $category  = array(1,2),
            $template  = NULL,
            $author_id = '1',
            $status    = 'publish'
        ) {
    
            define( POST_NAME, $name );
            define( POST_TYPE, $type );
            define( POST_CONTENT, $content );
            define( POST_CATEGORY, $category );
            define( POST_TEMPLATE, '' );
            define( POST_AUTH_ID, $author_id );
            define( POST_STATUS, $status );
    
            if ( $type == 'page' ) {
                $post      = get_page_by_title( POST_NAME, 'OBJECT', $type );
                $post_id   = $post->ID;
                $post_data = get_page( $post_id );
                define( POST_TEMPLATE, $template );
            } else {
                $post      = get_page_by_title( POST_NAME, 'OBJECT', $type );
                $post_id   = $post->ID;
                $post_data = get_post( $post_id );
            }
    
            function hbt_create_post() {
                $post_data = array(
                    'post_title'    => wp_strip_all_tags( POST_NAME ),
                    'post_content'  => POST_CONTENT,
                    'post_status'   => POST_STATUS,
                    'post_type'     => POST_TYPE,
                    'post_author'   => POST_AUTH_ID,
                    'post_category' => POST_CATEGORY,
                    'page_template' => POST_TEMPLATE
                );
                wp_insert_post( $post_data, $error_obj );
            }
    
            if ( ! isset( $post ) ) {
                add_action( 'admin_init', 'hbt_create_post' );
                return $error_obj;
            }
    
        }
    }
    
    /* All available options for PostCreator()
    
    PostCreator( 'TITLE' , 'POST TYPE' , 'POST CONTENT' , 'POST CATEGORY' , 'TEMPLATE FILE NAME' , 'AUTHOR ID NUMBER' , 'POST STATUS');
    
    TITLE - HTML Stripped Out. Simple String.
    POST TYPE - Post type slug. Eg 'post' or 'page'. Custom Post Types are supported.
    POST CONTENT - Content of the Post/Page. HTML allowed.
    POST CATEGORY - An array of the integer ID's of the category/categories you want to link to your post
    TEMPLATE FILE NAME - File name of the template. Only for Pages. In the format 'file_name.php'.
    AUTHOR ID NUMBER - Integer value. Default is 1.
    POST STATUS - Available options; [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | custom registered status ]
    
    If successful, PostCreator() returns nothing.
    If there is an error PostCreator() returns a WP_error object.
    
    */
    
    PostCreator( 'My Lorem Ipsum', 'page', 'With a sizable serving of Dolor. This was created using Harri Bell-Thomas\'s PostCreator function.' );
    

    Source of this code

    B. USING THE (QUESTIONED) API XML-RPC

    In WordPress, the XMLRPC protocol is a kind of interface that acts as an API for external applications and that allows us to interact with a WordPress installation using external applications or services. When functioning as an external interface it is almost like a "gateway" which can be consequently easily attacked from the outside causing a high consumption of resources when executing the authentication process again and again.

    In many places this API is put into question and when it is not used it is advisable to deactivate it ( see for example ).

    Anyway, here's an example:

    function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$category,$keywords='',$encoding='UTF-8') {
        $title = htmlentities($title,ENT_NOQUOTES,$encoding);
        $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);
    
        $content = array(
            'title'=>$title,
            'description'=>$body,
            'mt_allow_comments'=>0,  // 1 to allow comments
            'mt_allow_pings'=>0,  // 1 to allow trackbacks
            'post_type'=>'post',
            'mt_keywords'=>$keywords,
            'categories'=>array($category)
        );
        $params = array(0,$username,$password,$content,true);
        $request = xmlrpc_encode_request('metaWeblog.newPost',$params);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
        curl_setopt($ch, CURLOPT_URL, $rpcurl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);
        $results = curl_exec($ch);
        curl_close($ch);
        return $results;
    ?>
    

    Source of this code

    C. INSERTING THE MYSQL DATABASE

    The WP entries are stored in a MySQL Database . It will not be difficult to create a script that connects to the BD and create a post by inserting the data you send in the corresponding tables.

        
    answered by 10.03.2017 / 05:11
    source