Textbroker to Wordpress - Automatically Enable Comments, Change Post Type, Disable Rich Text Editor

It’s great having things done for you, isn’t it? I got someone round to fix a loose tile on my roof last month, because I’m terrified of heights, and what I know about roof repair could be written on the side of a USB thumb drive in giant permanent marker.

My wife will ocassionally, very occasionally make me a cup of tea. That’s pretty nice. I haven’t asked her to write any content for me. She’d likely do a much better job of it though. But then I might have to (gasp) make her a cup of tea in return. So that’s where services like Textbroker come in handy when importing Wordpress content. They don’t expect hot beverages. Just money. Keeps it simple.

That said, it’s still nice to have a little control over the flow of data. There are some modifications you can make to the Textbroker import process to alter data as it enters your Wordpress site.

Automatically enabling comments, & trackbacks for Textbroker

You might want to ensure that any posts from Textbroker are automatically marked as commentable in Wordpress using its commenting system.

Plop this little snippet into your functions.php file. Comment out the queries as required

Note: This functionality isn’t specific to Textbroker. It will work for any post sent via XMLRPC using the metaWeblog.newPost or wp.newPost channels.

add_action('wp_insert_post', function($post){

    global $HTTP_RAW_POST_DATA, $wpdb;

    if(stripos($HTTP_RAW_POST_DATA, 'metaWeblog.newPost') !== false || stripos($HTTP_RAW_POST_DATA, 'wp.newPost') !== false) {

      // Allow comments on new posts
        $query = $wpdb->prepare("UPDATE {$wpdb->prefix}posts SET comment_status = 'open' WHERE ID = %d", $post);
        $wpdb->query($query);

        // Allow pingbacks/trackbacks on new posts
        $query = $wpdb->prepare("UPDATE {$wpdb->prefix}posts SET ping_status = 'open' WHERE ID = %d", $post);
        $wpdb->query($query);

        // Set the trackback URL if required
        $pingUrl = "http://www.example.com/yourpingurl.php";
        $query = $wpdb->prepare("UPDATE {$wpdb->prefix}posts SET to_ping = %s WHERE ID = %d", $pingUrl, $post);
        $wpdb->query($query);

        // Add a meta value to mark this post as having come from textbroker. Comes in handy later...
        update_post_meta($post, 'source_xmlrpc', '1');

    }

});

Set a custom post type for Textbroker posts

You might want to change the content from Textbroker to something other than a page or a post in Wordpress.

As long as you have a custom post type already defined in your Wordpress blog, you can do so with a slight modification to the earlier code, using the wp_insert_post hook.

You can either use this code directly, or merge it with the previous chunk of code.

add_action('wp_insert_post', function($post){

    global $HTTP_RAW_POST_DATA, $wpdb;

    if(stripos($HTTP_RAW_POST_DATA, 'metaWeblog.newPost') !== false || stripos($HTTP_RAW_POST_DATA, 'wp.newPost') !== false) {

        // Change the post type of the generated post
        $yourPostType = 'article';
        $query = $wpdb->prepare("UPDATE {$wpdb->prefix}posts SET post_type = '%s' WHERE ID = %d", $yourPostType, $post);
        $wpdb->query($query);

    }

});

Disable the rich text editor for Textbroker posts

You might want to set up your Wordpress site to automatically show Textbroker posts in text mode, and potentially disable the WYSIWYG mode altogether, as if you’re recieving your content with special formatting, Wordpress’ editor might interfere with that.

This is just one example of how you could apply different rules for Textbroker posts than for others. This makes use of the meta value, source_textbroker we set previously.

add_filter('user_can_richedit', 'disable_wyswyg_for_xmlrpc_post');
function disable_wyswyg_for_xmlrpc_post( $default ){
    global $post;
    $fromXMLRPC = get_post_meta($post->ID, 'source_xmlrpc', true);
    if($fromXMLRPC):
        return false;
    endif;
    return $default;
}

Other useful modifications to Textbroker posts

You might want to try other modifications to the incoming Textbroker data according to your requirements. There’s a whole range of possibilities. If you’d like any assistance in setting up functionality like this, please do feel free to get in touch