See the Snippets! You can get productive in minutes or waste days and debug huge amounts of free and crappy code to get to 5% of elWpAPI's functionality.

WordPress Remote Control

WordPress XMLRPC Remote Publishing API (and beyond) by Easy-to-use PHP Objects

••• Hey there! Click here to buy, download or update the WpAPI library ONLY after you read the #3 Testimonials •••

Basic Snippets

Basic Snippets are in no way basic as in simple but they work on any WordPress with XMLRPC enabled. They don't require the rFC-rSQL MUplugin to work. A different page will show examples with Advanced Snippets that require an additional plugin that facilitates special unrestricted access to WordPress.

1 | Initializing elWpAPI

This code depends on your actual file locations so treat it as an example. Make sure you fix the paths to point to your WPAPI files.

<?php
// Include WPAPI files first
require_once(dirname(__FILE__).'/wpapi/WpAPI.php');
require_once(dirname(__FILE__).'/wpapi/WpAPIpro.php');
require_once(dirname(__FILE__).'/wpapi/WpAPIwrap.php');
// Using the basic Object (enter proper blog URL and Credentials)
$wp = new elWpAPI('http://.../xmlrpc.php', 'admin', 'password');
// ... or the Advanced Object (that requires rFC/rSQL MUplugin installed)
$wpro = new elWpAPIpro('http://.../xmlrpc.php', 'admin', 'password');
?>

2 | List Blog Authors

<?php
$blogs = $wp->getBlogs(); // List blogs
$author = $wp->getUserInfo(); // Current user details
$authors = $wp->getAuthors(); // Author listing ... 'light' on details
$authorsEx = $wp->getAuthorsEx(); // Author listing ... 'advanced' details
?>

3 | Manage Options

<?php
// Returns Options as array with advanced details
$options = $wp->getOptions($options = null, $blogID = 0);
// Returns Options grouped by Access (Read/Write)
$optionsLGrouped = $wp->getOptionsGrouped($blogID = 0);
// Returns Options as an array of name and Values
$optionsLight = $wp->getOptionsLight($blogID = 0);
// Set options values as array(Name => Value)
$result = $wp->setOptions($options, $blogID = 0);
?>

4 | Listing Statuses

<?php
$pageStatuses = $wp->getPageStatusList($blogID = 0); // Possible page statuses
$postStatuses = $wp->getPostStatusList($blogID = 0); // Possible post statuses
$commentStatuses = $wp->getCommentStatusList($blogID = 0); // Possible comment statuses
?>

5 | Listing Tags & Categories

<?php
// Get categories as array(ID => Name)
$categories = $wp->getCategories($blogID = 0, $shortVersion = 1);
// Get categories with advanced details
$categoriesEx = $wp->getCategoriesEx($blogID = 0);
// Get categories with basic details
$categoryList = $wp->getCategoryList($blogID = 0);
// Get tags as array(ID => Name)
$tags = $wp->getTags($blogID = 0, $shortVersion = 1);
// Get tags with advanced details
$tagsEx = $wp->getTagsEx($blogID = 0);
?>

6 | Manage Categories

<?php
// Create a new category with optional full details, returns an ID
$categoryID = $wp->newCategory($name, $parent = 0, $slug = null,
     
$description = null, $blogID = 0);
$dropResult = $wp->dropCategory($categoryID); // Drop the newly created category
// Create multiple categories in one call, returns array (Name => ID)

$categoryIDs = $wp->newCategories($names, $parent = 0, $blogID = 0);
?>

7 | Listing Pages

<?php
// Get the last 10 pages from the blog (advanced details versions, as arrays)
$last10 = $wp->getPages($limit = 10, $blogID = 0);
// Get the all the from the blog (advanced details versions, as arrays)
$all = $wp->getAllPages($blogID = 0);
// Get the last 10 pages from the blog (advanced details versions, as elWpPage objects)
$last10Obj = $wp->getPagesObj($limit = 10, $blogID = 0);
// Get the all the from the blog (advanced details versions, as elWpPage objects)
$allObj = $wp->getAllPagesObj($blogID = 0);
// Gets a 'light' list and is recommended but has no limit (returns all in one call, objects)
$list = $wp->getPageList($blogID = 0);
// Get all posts as an array(ID => Title); uses ->getPostsList()
$pageIDs = $wp->getPageIDs($blogID = 0);
?>

8 | Manage Pages

Basic Page Creation

<?php
// Create A Page ... the 'light' way
$pageID = $wp->newPage('title', 'text', 'more', 'slug', true);
?>

Advanced Page Creation

<?php
// Let's create a Page ... the advanced way
$newPage = new elWpAPIpage();
// We now set the important details
$newPage->set(
'Advanced Title', // Post title
'Advanced Text', // Post text
'Advanced More', // Optional more text
'Advanced Excerpt' // Optional excerpt
);
$newPage->setStatus('publish'); // Publish directly
$newPage->setSlug('advanced-slug'); // Set slug
// Add a bunch of custom fields

$newPage->addCustomField('cf', 'value1');
$newPage->addCustomField('cf', 'value2');
$newPage->addCustomField('cf1', 'value11');
$newPage->addCustomField('cf2', 'value22');
$newPage->setAuthor(1); // Set the author ID
$newPage->setParent(0); // No parent for the page
// Create the Page and get the ID

$pageIDEx = $wp->newPageEx($newPage);
?>

Get Page

<?php
// Get the page complete informations as array
$page = $wp->getPage($pageID);
// Get the page complete informations as elWpPage object
$pageEx = $wp->getPageObj($pageID);
?>

Edit Page

Variant 1

<?php
// Working with the array-ish version
$page = $wp->getPage($pageID = 31); // Get the page as an array of properties
$page['title'] = 'Advanced Title #2'; // Change an array property
$wp->setPage($pageID, $page); // Update page
?>

Variant 2

<?php
// Working with the object-ish (elWpPage) version [more secure]
$page = $wp->getPageObj($pageID = 31); // Get the page as an object
$page->setTitle('Advanced Title #2'); // Change title
$wp->setPage($pageID, $page); // Update page
?>

Delete Page

<?php
$success = $wp->dropPage($pageID); // Delete the page
?>

9 | Listing Posts

<?php
// Get a limited number of recent posts with full details (array of arrays)
$recentsEx = $wp->getRecentPosts($limit = 10, $blogID = 0);
// Get all posts with full details (array of arrays)
$allEx = $wp->getPosts($blogID = 0);
// Get a limited number of recent posts with full details (array of elWpAPIpost)
$recentsObj = $wp->getRecentPostsObj($limit = 10, $blogID = 0);
// Get all posts with full details (array of elWpAPIpost)
$allObj = $wp->getPostsObj($blogID = 0);
// Get a limited number of recent posts with light details (recommended)
$recentsLight = $wp->getRecentPostsList($limit = 10, $blogID = 0);
// Get all posts with light details (recommended)
$postsLight = $wp->getPostsList($blogID = 0);
// Get a limited number of recent posts as an array(ID => Title) uses ->getPostsList()
$recentPostsIDs = $wp->getRecentPostIDs($limit = 10, $blogID = 0);
// Get all posts as an array(ID => Title); uses ->getPostsList()
$postsIDs = $wp->getPostIDs($blogID = 0);
?>

10 | Manage Posts

Basic Post Creation

<?php
// Create a new 'simple' post
$postID = $wp->newPostEz('Simple Post Title', 'Simple Post Text');
// And get the Post basic informations
$post = $wp->getPostEz($postID);
?>

Advanced Post Creation

<?php
// Let's create a Post ... the advanced way
$newPost = new elWpAPIpost('post');
$newPost->set(
'Advanced Title', // Post title
'Advanced Text', // Post text
'Advanced More', // Optional more text
'Advanced Excerpt' // Optional excerpt
);
// Posts with a future date change status to future by themselves
$newPost->setStatus('publish');
// Set post slug: nice permalink thingy
$newPost->setSlug('post-slug');
// Create categories before assigning them
$wp->newCategories(array('Category3', 'Category4'));
// Add categories next
$newPost->addCategories('Category3', 'Category4');
// Add post tags
$newPost->addKeywords('tag1', 'tag2');
// Add custom fields
$newPost->addCustomField('cf', 'value1');
$newPost->addCustomField('cf', 'value2');
$newPost->addCustomField('cf1', 'value11');
$newPost->addCustomField('cf2', 'value22');
// Enable both comments and pings
$newPost->enableFeedback(true, true);
// Scheduled 1 Day from now ;) ... use 0 for current time
$newPost->setTime(time() + (24 * 3600));
// Create the Post and get the ID
$postIDEx = $wp->newPostEx($newPost);
// And get the Post complete informations
$postEx = (array)$wp->getPostObj($postIDEx);
?>

Edit Post

Variant 1

<?php
// Working with the array-ish version
$post = $wp->getPost($postID); // Get the page as an array of properties
$post['title'] = 'Advanced Title #2'; // Change an array property
$wp->setPost($postID, $post); // Update page
?>

Variant 2

<?php
// Working with the object-ish (elWpAPIpost) version [more secure]
$post = $wp->getPostObj($postID); // Get the page as an object
$post->title = 'Advanced Title #2'; // Change title
$wp->setPost($postID, $post); // Update page
?>

Manage Post Categories

<?php
// Get post categories as array(ID => Name)
$catIDs = $wp->getPostCategories($postID, $shortVersion = 1);
// Remember, previous categories are removed and these ones are set
// Please use an array of Category IDs; use Strings at your own risk

$success = $wp->setPostCategories($postID, array(10));
// Get post categories as array of objects with advanced details
$cats = $wp->getPostCategoriesEx($postID);
?>

Delete Post

<?php
// Deleting a post
$success = $wp->dropPost($postID);
?>

11 | Listing Comments

<?php
// Returns an object with details about comments count
$commentsCount = $wp->getCommentCount($postID = 0, $blogID = 0);
// Get post comments as an array of objects
$postComments = $wp->getPostComments($postID, $blogID = 0);
// Get post Pending comments as an array of objects
$pendingComments = $wp->getPendingComments($postID = 0, $blogID = 0);
// Get paginated comments with advanced parameters (param combo below returns all)
$allComments = $wp->getComments($post_id = 0, $status = null,
     
$offset = 0, $number = 0, $blogID = 0);
?>

12 | Manage Comments

<?php
// Must create a user comment first, custom name/URL comments can't be added directly
$commentID = $wp->newUserComment($postID, 'Comment - '.md5(time()));
// Get the created comment before changing it
$comment = $wp->getComment($commentID);
// Then edit it to change name, URL and so on...
$success = $wp->editComment(
$commentID, // Edit commentID
'Comment Changed - '.md5(time()), // New comment text
'Author', // Author name
'http://www.somedomain.com/', // Author URL
'author@wp.mu', // Author EMail
'approve', // Comment status
time() // Timestamp
);
// Drop the just added comment
$wp->dropComment($commentID);
?>

13 | Upload Media

<?php
// newMediaObject and uploadFile are exactly the same and return an object with a url property
$newObj1 = $wp->newMediaObject('image.png', file_get_contents('image.png'),
     
'image/png', false);
$newObj2 = $wp->uploadFile('image.png', file_get_contents('image.png'), 'image/png', false);
// uploadLocalFile calls uploadFile but it uses a path instead of name and data
$newObj3 = $wp->uploadLocalFile('image.png', 'image/png', false);
?>

14 | Issuing Raw Commands

This snippet will have no utility for most of you. But I'm publishing it just so you know what goes on beneath the hood.

<?php
// This adds username and password as first two params
$results_up1 = $wp->doRequestUP('method', 'param1', 'param2');
// This needs params as an array and injects username and password as above
$results_up2 = $wp->doRequestArgsUP('method', array('param1', 'param2'));
// Do a raw request ... params are not changed in any way; no username or password added
$results_1 = $wp->doRequest('method', 'param1', 'param2');
// Same as above just that params are provided as an array and not direct arguments
$results_2 = $wp->doRequestArgs('method', array('param1', 'param2'));
?>