Skip to content

Instantly share code, notes, and snippets.

@kheniparth
Last active August 29, 2015 14:18
Show Gist options
  • Save kheniparth/57af7ca50a91410b1096 to your computer and use it in GitHub Desktop.
Save kheniparth/57af7ca50a91410b1096 to your computer and use it in GitHub Desktop.
PHP Function to publish a post in WordPress
function publishPost($title,$description,$category,$keywords,$encoding,$excerpt)
{
$content = array(
'title'=>$title,
'description'=>$description,
'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),
'mt_excerpt' => $excerpt
);
// Create the client object
$client = new IXR_Client('http://www.yourdomain.com/xmlrpc.php');
$username = "username"; //this user must have writting permissions.
$password = "password";
$params = array(0,$username,$password,$content,true); // Last parameter is 'true' which means post immediately, to save as draft set it as 'false'
// Run a query for PHP
if (!$client->query('metaWeblog.newPost', $params)) {
die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
} else {
echo "Article Posted Successfully";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment