Action

WordPress - New Post

Posted by agiletortoise, Last update about 6 years ago

Create a new post on a WordPress blog using XML-RPC API through Drafts scripting. Additional options can be configured using parameters documented in Wordpress XML-RPC api docs. This example will create the post as a draft.

Steps

  • script

    // setup values to use in post
    let title = draft.processTemplate("[[title]]");
    let body = draft.processTemplate("[[body]]");
    
    // create credentials for site
    let cred = Credential.createWithHostUsernamePassword("WordPress", "WordPress credentials. Include full URL (with http://) of the home page of your WordPress site in the host field.");
    cred.authorize();
    
    // create WordPress object and make request
    let wp = WordPress.create(cred.getValue("host"), 1, "", "");
    let method = "wp.newPost"
    let params = [
    	1, // blog_id, in most cases just use 1
    	cred.getValue("username"), 
    	cred.getValue("password"),
    	{
    		"post_title": title,
    		"post_content": body
    	}
    ];
    
    let response = wp.runMethod(method, params);
    if (response.success) {
    	console.log("Response: " + JSON.stringify(response.params));
    	console.log("Error: " + response.error);
    	console.log("Create WordPress post id: " + response.params[0]);
    }
    else {
    	console.log("HTTP Status: " + response.statusCode);
    	console.log("XML-RPC Fault: " + response.faultCode + ", " + response.error);
    	context.fail();
    }
    

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.