WordPress Extended API Sample Code

Home » Blog » WordPress Extended API Sample Code
Feb 28, 13 • Blog, Featured, WordPress Plugins2 Comments

I’ve been getting semi-frequent requests for sample code for working with my WordPress Extended API plugin. So I figured I’d go ahead and post a sample to help out. 

The following sample uses Zend Framework’s XmlRpc library.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
error_reporting(E_ALL);
 ini_set('display_errors', '1');

$path = '/PATH/TO/ZEND/FRAMEWORK/';
 set_include_path(get_include_path() . PATH_SEPARATOR . $path);

require_once('Zend/Loader/Autoloader.php');
 Zend_Loader_Autoloader::getInstance();

$params = array(
 'username' => 'USERNAME-HERE',
 'password' => 'PASSWORD-GOES-HERE',
 'method' => 'add_post_meta',
 'args' => array(
 'post_id' => 1,
 'meta_key' => 'Michael',
 'meta_value' => 'Rocks',
 'unique' => false)
 );

$client = new Zend_XmlRpc_Client('http://YOUR-WORDPRESS-URL/xmlrpc.php');
 $response = $client->call('extapi'. ".callWpMethod",$params);

If you’re working with another library or another language, try using the above to along with this to generate a sample of what your requests and responses should look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 echo "

<pre>"
;
echo "Request:

"
;

$request = $client-&gt;getLastRequest();

$dom = new DOMDocument;
$dom-&gt;preserveWhiteSpace = FALSE;
$dom-&gt;loadXML($request);
$dom-&gt;formatOutput = TRUE;
$request_xml = $dom-&gt;saveXml();
echo nl2br(str_replace('&lt;', '&lt;', $request_xml));
echo "
"
;
echo "Response:

"
;

$response = $client-&gt;getLastResponse();

$dom = new DOMDocument;
$dom-&gt;preserveWhiteSpace = FALSE;
$dom-&gt;loadXML($response);
$dom-&gt;formatOutput = TRUE;
$response_xml = $dom-&gt;saveXml();
echo nl2br(str_replace('&lt;', '&lt;', $response_xml));
echo "</pre>
"
;

That should give you a good starting point, but feel free to contact me if you have further questions!

2 Responses to WordPress Extended API Sample Code

  1. Hussain says:

    So this is Zend Framework specific example. What about CodeIgniter?

    • michaelgrosser says:

      This is a Zend Framework example, but you should be able to understand what it’s doing and apply it to other frameworks. Unfortunately, I don’t have time to provide examples for every framework, especially ones I don’t work with often.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>