0

I was wondering if it is at all possible to write a php file that will take a form submission from an external server and add the submission to wordpress as a blog post. The file should return a post->ID. Thanks

asked Dec 3, 2012 at 1:15
2

1 Answer 1

1

If you want to have the form processed on the remote server, you can remotely add the submission to WordPress via XMLRPC.

An example as follows:

<?php
class XMLRPClientWordPress
{
 var $XMLRPCURL = "";
 var $UserName = "";
 var $PassWord = "";
 // constructor
 public function __construct($xmlrpcurl, $username, $password) 
 {
 $this->XMLRPCURL = $xmlrpcurl;
 $this->UserName = $username;
 $this->PassWord = $password;
 }
 function send_request($requestname, $params) 
 {
 $request = xmlrpc_encode_request($requestname, $params);
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
 curl_setopt($ch, CURLOPT_URL, $this->XMLRPCURL);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_TIMEOUT, 1);
 $results = curl_exec($ch);
 curl_close($ch);
 return $results;
 }
 function create_post($title,$body,$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,$this->UserName,$this->PassWord,$content,true);
 return $this->send_request('metaWeblog.newPost',$params);
 }
 function create_page($title,$body,$encoding='UTF-8')
 {
 $title = htmlentities($title,ENT_NOQUOTES,$encoding);
 $content = array(
 'title'=>$title,
 'description'=>$body
 );
 $params = array(0,$this->UserName,$this->PassWord,$content,true);
 return $this->send_request('wp.newPage',$params);
 }
 function display_authors()
 {
 $params = array(0,$this->UserName,$this->PassWord);
 return $this->send_request('wp.getAuthors',$params);
 }
 function sayHello()
 {
 $params = array();
 return $this->send_request('demo.sayHello',$params);
 }
}
if(trim($_POST['subject'])=='' || trim($_POST['content'])==''){
 die('All fields are required.');
}
$xmlrpc = new XMLRPClientWordPress("http://yoursite.com/xmlrpc.php" , "username" , "password");
$xmlrpc->create_post( mb_convert_encoding(stripslashes($_POST['subject']), 'HTML-ENTITIES', 'UTF-8'), mb_convert_encoding(htmlentities(stripslashes($_POST['content']), ENT_COMPAT, 'UTF-8'), 'HTML-ENTITIES', 'UTF-8'), 1);
echo 'Post successful!';
?>
answered Dec 3, 2012 at 8:08
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this file, however how do I return a post->ID?
If I'm not mistaken, $xmlrpc->create_post() should return with something containing the post ID. Try var_dump()ing it.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.