Full refund within 14 days of purchase date.
This is a Wordpress XMLRPC connector for Zend Framework based projects, other PHP frameworks based applications and baseline PHP applications.
This library allows you to connect and perform operations on remote Wordpress installations from other applications. You can add pages, posts, work with authors and use many other Wordpress features remotely.
Features
PEAR Packages available see appropriate section.
This library use the ZF1 autoloader, and it is written for working on PHP 5.x or later.
You can fork, download and see new features (zf2 porting) on github.
To connect to your Wordpress installation you need to enable XML-RPC API access. Enter the Wordpress administration area and under settings->writing enable the XML-RPC connector.
After that you can create a new connection from your project in this way:
$wp = new Wally_Wordpress("http://www.your-site-name.com", "username", "password");
If you have a Zend Framework based project you have only to put the Wally folder that you find in the package at the same folder as the Zend library. After that, add the autoload namespace in the application.ini file:
autoloadernamespaces.Wally = "Wally_"
If you have a native project you have to put the Wally library and the Zend library into a directory and add the path to the php include_path, after that start the Zend Autoloader functionality into your bootstrap procedure.
<?php
set_include_path(implode(PATH_SEPARATOR, array(
realpath(dirname(__FILE__) . '/../src'),
get_include_path(),
)));
//Zend autoloader
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace("Wally_");
$autoloader->registerNamespace("Zend_");
This allows Wally classes to be loaded magically as well as use the required Zend Framework components for negotiating with XML-RPC.
This library is designed for flexibility and readability. All components contain "magic" methods for getting specific information without an heavy weight usage of Wordpress Web Service methods.
For example: If you need a single page, you can use getPages() and then run findOneBy a particular variable that you need. See example code below -
//Get all pages
$pages = $wp->getPages();
$page = $pages->findOneByUsername("walter");
The system replies with a single post written by user that have "walter" as username.
Methods like getPages, getPosts and others return iteratable objects that can be treated as arrays with added functionality.
Example usage:
$pages = $wp->getPages();
foreach ($pages as $page) {
echo $page->title . PHP_EOL;
}
Result sets contain objects that act as data containers for Wordpress properties.
$pages->getPages();
foreach ($pages as $page) {
//We can use the $page as an object
echo $page->title . " " . $page->dateCreated;
}
Variables name in the final objects are the same of Wordpress documentation but converted to the Zend coding standard and Pear coding standard, see http://codex.wordpress.org/XML-RPC_wp
We can treat sets such as pages and posts as arrays like in previous examples, but we can also search the sets using the find magic methods. Some examples:
$pages = $wp->getPages();
$singlePostWrittenByWalter = $pages->findOneByWpAuthor("Walter Dal Mut");
$allPostsWrittenByWalter = $pages->findAllByWpAuthor("Walter Dal Mut");
$oldPages =
$pages->findAllLessThanDateCreated(Zend_Date::now()->subDay(20));
$oldGreaterPages =
$pages->findAllGreaterThanDateCreated(Zend_Date::now()->subDay(20));
Starting from 1.0.1 you can use the finder chain strategy for obtain resultsets.
$post = $posts->findAllByUserid("walter")->findOneGreaterThanDateCreated(Zend_Date::now()->subDay(10));
In this example we want to find all posts written by "walter" and search for one on that result set which have date created greater than 10 days ago.
We can treat sets such as pages and posts as arrays like in previous examples, but we can also sort the sets using the sort magic methods. Some examples:
$pages->sortByTitleOrderAsc();
$pages->sortByUserIdOrderDesc();
Pay attention that the sort method is a mutable method and for that reason it sort the actual result set.
If you want to get the names of object properties you can get any of sets (pages, posts etc) and use the var_dump function to see what they contain. Alternatively, property names are the same as described in the Wordpress API: http://codex.wordpress.org/XML-RPC_Support
All methods get the blogId, it is the blog identification number. You can omit after the first call since the library will remember it, but if you have multiple blogs you will want to specify the identifier when making each request.
$pages = $wp->getPages(2); //get pages of blog 2
$posts = $wp->getPosts(10, 2); //get 10 posts of blog 2
$categories = $wp->getCategories(3); //get categories of blog 3
You can check blog instances using the getUsersBlogs method, see the authors section.
To get blog pages you only have to create a new Wordpress Connection using the Wally_Wordpress component and request pages using the getPages method.
$wp = new Wally_Wordpress("http://your_site.com/xmlrpc.php", "username", "password");
$pages = $wp->getPages();
foreach ($pages as $page) {
echo "{$page->title}
";
}
That's all, very simple.
If you want perform specific searches you can use the find magic method, eg. findOneByTitle where the part after findOneBy is the name of the property you want to search by.
If you want to create a new page you have to create a Wally_Wordpress_Model_Page object and save it using your Wally Wordpress instance.
Example:
$page = new Wally_Wordpress_Model_Page();
$page->title = "XML-RPC";
$page->wpSlug = "";
$page->wpPageOrder = -10;
$page->customFields = array();
$page->description = "This is the content";
$page->mtAllowComments = 0;
$page->mtAllowPings = 0;
$page->pageStatus = "publish";
$wp->save($page);
After that your new page is created on the blog.
If you want to update an existing page you can find it, modify it and save it using a similar method. See example below for getting pages, locating a specific page by identification number and modifying the title of the page:
$page = $wp->getPages()->findOneByPageId(555);
$page->title = "Wally from XML-RPC";
$page->pageStatus = "publish";
$wp->save($page);
Posts and pages are treated in a similar fashion, but we use the appropriately named Posts and Post models instead of Pages and Page.
If you want to create a new post you can construct it and use the save method, as shown previously for pages:
$post = new Wally_Wordpress_Model_Post();
$post->title = "My Title";
$post->category = "1,2"; //Put it into category with id 1 and 2
$post->content = "This is the body of my post!";
$wp->save($post);
Post creation is very simple and powerful. If you need to modify a post, find it and modify it, then save it as before:
$post = $wp->getPosts()->findOneByTitle("My Title");
$post->title = "My new title";
$wp->save($post);
If you want to get latest articles from your blog you can call the getPosts method. This method works like the getPages method and all features are available for both methods.
$posts = $wp->getPosts();
$post = $posts->findOneByPostid(12);
Pay attention on getPosts results, infact this method use the blogger prefix for working and variables are not in caplocks, for example the post identification number is written directly postid and the find method for that reason is findOneByPostid. The same particularity exists for userid and other fields, use var_dump function for show array keys and use the find magic method properly.
If you want to get all your tags you can use the getTags method. This method works similarly to getPages. Example:
$tags = $wp->getTags();
$tag = $tags->findOneByName("wordpress");
echo $tag->name;
If you need to access to your users programmatically, you need to use two different methods.
The first one is the getUsersBlogs, that is used to get the blogs of the users registered on the Wordpress Web Service that you are connecting to and the second one is the getAuthors method.
Returned set objects use finder methods for filtering the set. Example:
$blogs = $wp->getUsersBlogs();
$admins = $blogs->findAllByIsAdmin(1); //Get blogs where I am an admin
Another example:
$users = $wp->getAuthors();
$user = $users->findOneByUserId(1); //Get one user with a specific identification number
If you want to change the blog you are fetching from, specify the blog identification number in the request method:
$users = $wp->getAuthors(3); //Get users of blog with id 3
If you want to remove pages programmatically you need to use the page identification number. If you do not have yet, you can find it using the getPages method and search the result set to find the page you want to delete. Example:
$pages = $wp->getPages();
$page = $pages->findOneByTitle("This is the title");
$wp->delete($page);
If you have the identification number, you can create the page model manually and call the delete method. Example:
$page = new Wally_Wordpress_Model_Page();
$page->pageId = 124;
$wp->delete($page);
In this example the page exists on the blog and we know the identification number. We can thus directly delete the page without requesting the page list.
The same process is used for deleting posts, just with differently named models (Posts instead if Pages). Example:
$posts = $wp->getPosts();
$post = $wp->findOneByTitle("Goodbye cruel world");
$wp->delete($post);
If you want remove directly, use a process similar to what was discussed above:
$post = new Wally_Wordpress_Model_Post();
$post->postid = 15;
$wp->delete($post);
Categories are fetched like other Wordpress entities (such as Posts and Pages). Example
$categories = $wp->getCategories();
$category = $categories->findOneByCategoryName("My category name");
To get comments for a specific post, you find by filtering a Posts set using some criteria and calling the getComment method on the post found. There might be a lot of comments, so you might want to attach some filters when calling the getComment method. Example:
$post = $wp->getPosts()->findOneByTitle("Example");
$filters = new Wally_Wordpress_Model_CommentFilter();
$filters->setOffset(0); //No initial offset
$filters->setLimit(10); //No more than ten
$filters->setStatus(Wally_Wordpress::COMMENT_TRASH); //Get comments from trash
$post->setCommentsFilter($filters);
$comments = $wp->getComments($post);
Successful return results is an iteratable set of Comment objects. Note that status properties which include:
Are available as constants in the Wally_Wordpress class for easy handling of status.
If you want to modify an existing comment, use the save method on a comment model instance. Example:
$comment = $comments->findOneByAuthor("walter");
$comment->content = "I change your content completely";
$comment->status = Wally_Wordpress::TRASH; //Move comment to trash
$wp->save($comment);
You can move comments from trash to spam, spam to approved status and so forth using this method as well.
If you want to create a new comment, create a comment model instance, fill-in the relevant properties and run the save() method:
$comment = new Wally_Wordpress_Model_Comment();
$comment->commentParent = 0;
$comment->content = "This comment was created by xmlrpc";
$comment->author = "";
$comment->author_url = "";
$comment->author_email = "";
$comment->setPost($post);
$ret = $wp->save($comment);
Consider that the comment author will be set to the API connection user profile. If you want to publish with a different username, set the user and password before saving the comment.
You can see how to post anonymous comments on the Wordpress API section at : http://codex.wordpress.org/XML-RPC_wp#wp.newComment
If you want to send anonymous comments, leave the second and third parameter blank and install a filter to xmlrpc_allow_anonymous_comments to return true.
Those parameters are the author information we set on the comment object instance as shown in the example above.
You can use the pear repository using channel pear.walterdalmut.com
PAY ATTENTION This PEAR packages actually doesn't have any support, choose this solution only if you want and you are able with PEAR packages.
provided me a great time to work
This is a big help it works really great.
In my downloading, it became obvious that I would not be able to use this. Simply because I saw two Zip file errors upon downloading. If this gets fixed, I would love to try to use this system in the future, until then, I suppose it's on to building my own.
Questions & Comments