2017 © Pedro Peláez
 

library wpapi

Wordpress XML-RPC API wrapper

image

comodojo/wpapi

Wordpress XML-RPC API wrapper

  • Tuesday, August 18, 2015
  • by comodojo
  • Repository
  • 4 Watchers
  • 1 Stars
  • 24 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

comodojo/wpapi

Build Status Latest Stable Version Total Downloads License Scrutinizer Code Quality Code Coverage, (*1)

A XML-RPC Wordpress API wrapper. It uses the comodojo/rpcclient to send requests., (*2)

This lib is intended to be used as a remote Wordpress handler., (*3)

Installation

Install composer, then:, (*4)

composer require comodojo/wpapi 1.0.*, (*5)

Basic usage

Getting recent posts from a blog:, (*6)

try {

    // Create a new Wordpress instance
    $wp = new \Comodojo\WPAPI\WP( "www.example.org" );

    // Log in to the server
    if ($wp->login("awesome_user", "awesome_password")) {

        // Get last 10 posts (methods are chainable)
        $posts = $wp->getBlogByID(1)->getLatestPosts();

        // $posts is an Iterator object, so it can be used in a foreach statement
        foreach ($posts as $p) {

            echo "<h1>" . $p->getTitle()   . "</h1>";
            echo "<p>"  . $p->getContent() . "</p>";

        }

    }

} catch (\Exception $e) {

    /* something went wrong :( */

}

Retrieving profile informations:, (*7)

try {

    // Create a new Wordpress instance
    $wp = new \Comodojo\WPAPI\WP( "www.example.org" );

    // Log in to the server
    if ($wp->login("awesome_user", "awesome_password")) {

        // Get profile
        $profile = $wp->getBlogByID(1)->getProfile();

        echo "<p>Hello " . $profile->getDisplayName() . "!</p>";

        // You can edit your profile informations
        $profile->setFirstname("Arthur")->setLastname("Dent")->save();

        echo "<p>Don't forget your towel " . 
            $profile->getFirstname() . " " . $profile->getLastname() . "!</p>";

    }

} catch (\Exception $e) {

    /* something went wrong :( */

}

Adding a new post:, (*8)

try {

    // Create a new Wordpress instance
    $wp = new \Comodojo\WPAPI\WP( "www.example.org" );

    // Log in to the server
    if ($wp->login("awesome_user", "awesome_password")) {

        // Let's create a new post on the main blog
        $new_post = new \Comodojo\WPAPI\WPPost($wp->getBlogByID(1));

        // Chain like there's no tomorrow
        $new_post->setTitle("Awesome new post")
          ->setContent("This is a really awesome test post")
          ->addTag("test") // Add a few tags and a category
          ->addTag("awesomeness")
          ->addTag("wpapi")
          ->addCategory("wpapi")
          ->setStatus("publish") // By default, all posts are saved as "draft"
          ->save();

    }

} catch (\Exception $e) {

    /* something went wrong :( */

}

Adding a new page:, (*9)

try {

    // Create a new Wordpress instance
    $wp = new \Comodojo\WPAPI\WP( "www.example.org" );

    // Log in to the server
    if ($wp->login("awesome_user", "awesome_password")) {

        // Let's create a new page on the main blog
        $new_page = new \Comodojo\WPAPI\WPPost($wp->getBlogByID(1));

        // A page is basically a post with a different 'type' value
        $new_page->setTitle("Awesome new page")
          ->setContent("This is a really awesome test page")
          ->setType("page") // Yep, in order to create a page you just need to set the type
          ->setStatus("publish")
          ->save(); // Don't forget to save when you've done chaining

    }

} catch (\Exception $e) {

    /* something went wrong :( */

}

Retrieving comments for a specific post:, (*10)

try {

    // Create a new Wordpress instance
    $wp = new \Comodojo\WPAPI\WP( "www.example.org" );

    // Log in to the server
    if ($wp->login("awesome_user", "awesome_password")) {

        // Initialize a post object
        $post = new \Comodojo\WPAPI\WPPost($wp->getBlogByID(1));

        // You can load a post starting from its ID
        // and then retrieve its comments
        $comments = $post->loadFromID(42)->getComments();

        // Show how many comments are available
        echo "<h2>Total comment(s): ". $comments->getTotal() ."</h2>";

        // $comments is an Iterator object, so it can be used in a foreach statement
        foreach ($comments as $c) {

            echo "<h3>" . $c->getAuthor()  . "</h3>";
            echo "<p>"  . $c->getContent() . "</p>";

        }

    }

} catch (\Exception $e) {

    /* something went wrong :( */

}

Fetching the media library:, (*11)

try {

    // Create a new Wordpress instance
    $wp = new \Comodojo\WPAPI\WP( "www.example.org" );

    // Log in to the server
    if ($wp->login("awesome_user", "awesome_password")) {

        // Initialize a media gallery iterator
        // You can filter the results by the mime-type
        $library = $wp->getBlogByID(1)->getMediaLibrary("image/jpeg");

        /* The iterator is meant to load each object on demand during iteration.
         * This means that it won't do any query to Wordpress until you start
         * fetching through the media library. It should save time and resources,
         * but it won't allow you to know how many objects you have in the library
         * until you've fetched them all.
         */
        foreach ($library as $img) {

            echo "<img src='" . $img->getLink()  . "'/>";

        }

        echo "<p>Total images: " . $library->getFetchedItems() . "</p>";

    }

} catch (\Exception $e) {

    /* something went wrong :( */

}

Add an image to a post:, (*12)

try {

    // Create a new Wordpress instance
    $wp = new \Comodojo\WPAPI\WP( "www.example.org" );

    // Log in to the server
    if ($wp->login("awesome_user", "awesome_password")) {

        // Load a post (you can use the 'loadFromID' method or
        // set the post ID directly into the constructor)
        $post = new \Comodojo\WPAPI\WPPost($wp->getBlogByID(1), 42);

        // Create an image
        $image = new \Comodojo\WPAPI\WPMedia($wp->getBlogByID(1));

        // You can set the image object as an attachment to the post
        // This only works BEFORE calling the 'upload' method
        $image->setPostID($post->getID());

        // Upload a file directly to Wordpress
        // if you have a buffer of data, you can use the 'uploadData' method instead
        $image->upload("/path/to/file.jpg")->save();

        // You can both add the image as post thumbnail and append it to the content
        $post->setTitle( $post->getTitle() . " (with image)" )
            ->setThumbnail($image)
            ->setContent( $post->getContent() . "<p><img src='" . $image->getLink() . "'/></p>" )
            ->save();

    }

} catch (\Exception $e) {

    /* something went wrong :( */

}

Documentation

For a full documentation of the project, check the Comodojo API page., (*13)

Contributing

Contributions are welcome and will be fully credited. Please see CONTRIBUTING for details., (*14)

License

comodojo/wpapi is released under the MIT License (MIT). Please see License File for more information., (*15)

The Versions

18/08 2015

dev-master

9999999-dev https://comodojo.org

Wordpress XML-RPC API wrapper

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marco Castiello

wordpress api wrapper xmlrpc

18/08 2015

1.0.2

1.0.2.0 https://comodojo.org

Wordpress XML-RPC API wrapper

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marco Castiello

wordpress api wrapper xmlrpc

13/07 2015

1.0.1

1.0.1.0 http://comodojo.org

Wordpress XML-RPC API wrapper

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marco Castiello

wordpress api wrapper xmlrpc

11/07 2015

1.0.0

1.0.0.0 http://comodojo.org

Wordpress XML-RPC API wrapper

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marco Castiello

wordpress api wrapper xmlrpc