Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using Wordpress XMLRPC support.

The content fetched through an XMLRPC client does not have the automatically added <p> tags, whereas pages rendered on the blog of the Wordpress install do.

Is there an easy way to get the <p> tags coming through the XMLRPC content as well?

share|improve this question
    
are you posting via email via a custom interface you wrote / downloaded ? –  Sagive SEO Mar 10 '12 at 3:53
    
No, we're posting on the Wordpress install. As I said, going to the posts through the Wordpress site, you can see the p tags coming through even though they're not explicitly put in the posts. I've read that wp-autop delivers this magic. However, when the same posts come through the XMLRPC interface the automatically inserted p tags are absent. –  quickshiftin Mar 10 '12 at 4:37
add comment

1 Answer

up vote 4 down vote accepted
+100

So, the default xmlrpc get_post function does not have any nice filters for you to use. The solution: roll your own XML-RPC callback!

Hook into xmlrpc_methods and add a custom method, in this case called post_autop. The array key will be the method name, and the value the method callback.

<?php
add_filter( 'xmlrpc_methods', 'wpse44849_xmlrpc_methods' );
/**
 * Filters the XMLRPC method to include our own custom method
 */
function wpse44849_xmlrpc_methods( $method )
{
    $methods['post_autop'] = 'wpse44849_autop_callback';
    return $methods;
}

Then we have our callback function, which will receive an array of $args. Which will do a few simple things: log the user in (validate username/password), then fetch the post we want, replace the text with an autop'd version, and return the post.

<?php
function wpse44849_autop_callback( $args )
{

    $post_ID     = absint( $args[0] );
    $username    = $args[1];
    $password    = $args[2];

    $user = wp_authenticate( $username, $password );

    // not a valid user name/password?  bail.
    if( ! $user || is_wp_error( $user ) )
    {
        return false;
    }

    $post = get_posts( array( 'p' => $post_ID ) );

    // no posts?  bail.
    if( empty( $post ) )
    {
        return false;
    }

    $post = $post[0];

    // the magic happens here
    $post->post_content = wpautop( $post->post_content );

    return (array) $post;
}

You can, of course, do any customization you want to the post before returning the value. Here is the above as a plugin.

I used a bit of Python to test this.

>>> import xmlrpclib as xmlrpc
>>> s = xmlrpc.ServerProxy('http://localhost/xmlrpc.php')
>>> post = s.post_autop(1, 'admin', 'password')
>>> post
# content of the post here as a Python dict
share|improve this answer
    
@ChristoperDavis I've just installed this as a plugin and it appears to be working like a charm; thanks for your help amigo! –  quickshiftin Mar 10 '12 at 17:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.