Help a newbie with a PHP Function
I want to use this function:
http://www.frankmacdonald.co.uk/php/post-to-wordpress-with-php.ht开发者_StackOverflow社区ml
Its used to post to Wordpress using XMLRPC, can anyone give me the basics for using this function and maybe a brief over view?
I want to learn how functions work and how to use them.
EDIT:
The below guideance works like a dream.
I want to use a foreach
loop to cycle through a number of entries and post them all to WP, wonder if anyone could advise?
If you want to post several items, there are only a few things that change for every post:
- The title of the post.
- The contents of the post
- The category of the post
- The keywords (optional, but I guess you are using it).
The RPC URL, username, password and encoding is standard because I also suppose you're posting it onto the same website. So we only have to store the 4 named items in an array that we can run through. I store the items in another array, so we have an array of arrays.
You could easily write something like this:
// We create a post array that contains an array per post.
// I put in the data index based. First item is title, second is content body, third is category, fourth is keywords.
$posts = array(
array('Title','Contents','category','keywords'),
array('Another post','More content','another category ID','etc.'),
// You can add more items if you want.
);
// These are just general settings that are the same for each post.
$rpcurl = 'http://www.yourwordpressblog.com/xmlrpc.php';
$username = 'myusername';
$password = 'mypassword';
$encoding ='UTF-8';
foreach($posts AS $Post)
{
// From the foreach we get an array with post data each cycle.
// To keep things a bit clear, I will extract the data to seperate variables..
$title = $Post[0];
$body = $Post[1];
$category = $Post[2];
$keywords = $Post[3];
wpPostXMLRPC($title,$body,$rpcurl,$username, $password,$category,$keywords,$encoding);
}
Nothing much to do here, it's pretty much ready to run already.
Just replace the example values given in the first lines:
$title = 'This is the post title';
$body = 'this is the post content';
$rpcurl = 'http://www.yourwordpressblog.com/xmlrpc.php';
$username = 'myusername';
$password = 'mypassword';
$category = ''; //default is 1, enter a number here.
$keywords = 'one,two,three';//keywords comma seperated.
$encoding ='UTF-8';//utf8 recommended
with some actual data. (The link to xmlrpc.com should be www.yourdomain.com/xmlrpc.php
if your blog resides in the root directory.)
Put the whole thing into a PHP file, and run it. If you're lucky, everything will run fine on the first go. If it doesn't, come back and edit your question.
精彩评论