Search code examples
phpwordpresspostremote-accessxml-rpc

Remotely post to a Wordpress Blog with php (best solution)


I am searching for the best solution to post to a wordpress blog from a php script on a different server. Is there any good php script already developed? I think it would not work with a cookie auth model like the WP REST API?

Thank you very much

Regards

memme


Solution

  • Think best solution is to use the XML-RPC of wordpress with this PHP client: https://github.com/letrunghieu/wordpress-xmlrpc-client

    Here is the code to add a new Wordpress Blog Post and retrieve the URL:

        require_once 'WordpressClient.php';
        require_once('.\Exception\NetworkException.php');
        require_once('.\Exception\XmlrpcException.php');
    
        $endpoint = "http://www.example.com/xmlrpc.php";
        $wpUser = 'username';
        $wpPass = 'password';
        $YourCategoryID = 5;
        $wpClient = new \HieuLe\WordpressXmlrpcClient\WordpressClient();
        $wpClient->setCredentials($endpoint, $wpUser, $wpPass);
        $title="Your Blog Post Title";
        $title = htmlentities( $title, ENT_NOQUOTES, 'UTF-8' );
        $body='Your HTML coded article';
        $content = array(
        'post_category' => array( $YourCategoryID ), // my category id
        'post_type' => 'post',
        'post_status' => 'published',
        'post_title' => $title,
        'post_content' => $body,
        'ping_status' => 'closed',
        'comment_status' => 'closed',
    );
    
        $result=$wpClient->newPost($title,$body,$content);
        $postname=$wpClient->getPost($result);
        $new_post_url_slug=$postname['post_name'];