Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have developed a custom script that uses wp_insert_post to create new posts, however i'd like to use similar code to create the same data in another wp database. Could be this done on the fly before insertion? For example instruct wordpress to point another database and then insert data? Or should I do this "manually" using raw mySQL code?

share|improve this question
    
Have you tried this ? –  mike23 Jun 21 '11 at 10:06
add comment

1 Answer 1

up vote 1 down vote accepted

You could avoid SQL altogether and use the XML-RPC API. This would also let you post to remote wordpress installs too.

( note if XML-RPC is not an option, scroll further down )

If Using XML-RPC

Here's some code from a quick google search using XML-RPC to post to a remote Wordpress blog:

http://en.forums.wordpress.com/topic/great-code-for-remote-posting?replies=5

Here's a simpler set of examples with an explanation of the XML-RPC APIs

http://life.mysiteonline.org/archives/161-Automatic-Post-Creation-with-Wordpress,-PHP,-and-XML-RPC.html

And here's an example from WP-Recipes using Curl and XML-RPC:

http://www.wprecipes.com/post-on-your-wordpress-blog-using-php

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;
?>

If afterall that XML-RPC is not for you

Perhaps a wordpress multisite might be best for you? Creating the new post would be as simple as calling switch_to_blog($blog_id); then doing your business creating the new post, before calling restore_current_blog();

If you MUST use SQL

Use this question linked to by mike23 in order to gain access via a wpdb object

share|improve this answer
    
What advanatges using this technique versus mySQL? There's quite a bit of code here, maybe little less than using mySQL... maybe this wuold prevent potential conflicts with database scheme changes over newer WP versions? –  Riccardo Jun 21 '11 at 10:32
    
Indeed, fewer conflicts, and you get all the hooks filters that plugins use, and side stuff wordpress stuff that you skip by going straight for the database. –  Tom J Nowell Jun 21 '11 at 11:23
    
You also gain the security of that API, whereas your SQL code hasnt been tested for years with years of bugfixing and security fixes –  Tom J Nowell Jun 21 '11 at 11:24
    
Your also freed from needing the SQL database on the same server ( and its more secure than having a public facing SQL server ) –  Tom J Nowell Jun 21 '11 at 11:25
    
There's also stuff like user postcounts etc to take into account –  Tom J Nowell Jun 21 '11 at 11:28
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.