WordPress XML-RPC to Zend Framework connector

WordPress XML-RPC to Zend Framework connector

Released 6 years ago , Last update 5 years ago

A Zend Framework based library to access, fetch and write data to remote Wordpress instances. Also works with other framework based and baseline PHP applications.

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

  • Connect to multiple Wordpress blogs with one connection
  • Get user list and user information from remote blogs
  • Create, Retrive, Update and Delete pages (Pages CRUD)
  • Create, Retrive, Update and Delete posts (Posts CRUD)
  • Change page status, draft, publish and so on.
  • Change posts status, draft, publish and so on.
  • Get tags form your blog instance
  • Get categories from your blog instance
  • Get comments from articles
  • Create, edit and moderate comments for articles (Comments CRUD)
  • Get users of all your blogs or any specific blog
  • Finder and Sorter magic method (see in philosophy section)
  • PHPUnit tested - more than 70% of the code under test

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.

3.5
  • 5 2
  • 4 1
  • 3 0
  • 2 0
  • 1 0
4 Reviews Read Reviews

Pricing

14 day 14-day money-back guarantee

FREE

Personal License

  • Perpetual license

  • 1 site, unlimited servers

  • No distribution (hosted use only)

  • Commercial use

Create a connection

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");

Loading Wally Components in a Zend Framework project

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_"

Loading Wally Components out of Zend Framework scope

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.

Library Design Philosophy

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.

Usage as Arrays

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

Flexible Data Containers

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

Finder Methods

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.

Sorter Methods

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.

About dynamic model fields

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

Tips

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.

Get blog pages

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.

Create and modify pages / posts

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

Post workflow

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

Get posts from your blog

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.

Get tags from your blog

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;

Working with users

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

Delete pages and posts

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

Get blog categories

Categories are fetched like other Wordpress entities (such as Posts and Pages). Example

$categories = $wp->getCategories();
$category = $categories->findOneByCategoryName("My category name");

Working with comments

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:

  • SPAM
  • APPROVED
  • UNAPPROVED
  • TRASH
  • ALL

Are available as constants in the Wally_Wordpress class for easy handling of status.

Modify an existing comment

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.

Creating new comments

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.

Anonymous Comments

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 xmlrpcallowanonymous_comments to return true.

Those parameters are the author information we set on the comment object instance as shown in the example above.

Pear Repository

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.

License » Personal License Download

Get A Quote

What do you need?
  • Custom development
  • Integration
  • Customization / Reskinning
  • Consultation
When do you need it?
  • Soon
  • Next week
  • Next month
  • Anytime

Thanks for getting in touch!

Your quote details have been received and we'll get back to you soon.


Or enter your name and Email
  • PS Peter Schultheiss 5 years ago
    Hi Wally, great work on this library and documentation! I was able to get 90% of my tasks done with ease. A few questions and comments for you... - Is there any way to grab the permalink instead of using /blog/?p=postid? - I need to grab thumbnail images per post as well. I there a way I can extend your class to get these or do I need to write a web service to grab them? This is the final 10%. - The .zip file for the latest version does not open (I used the older 5.3-only version so it didn't affect me) Thanks again for your work here...great job! Best regards, Peter
    • WM Walter Dal Mut Publisher 5 years ago
      Hi Peter, I'm so happy that you use this component. I check the download and the zip works fine. Could you re-download the latest library and check if it works? Otherwise I re-upload the code asap. I'm on Github and I support this component through this social-coding. If you want you can fork the code at your pleasure. Actually your first two question (permalink, thumbs) are not supported but I think to support this in a future and I mark as "todo" on Github account and I will to work on this task when I have the time. If you fork the repo and fix anything please send me a pull request that I fill the code and republish immediately. Regards