Web Development by Solarise

The Solarise.dev Blog

Textbroker to WordPress – Automatically Enable Comments, Change Post Type, Disable Rich Text Editor

Blog archive Robin Metcalfe 13th January 2018

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

Author Image

About the author

Robin is the dedicated developer behind Solarise.dev. With years of experience in web development, he's committed to delivering high-quality solutions and ensuring websites run smoothly. Always eager to learn and adapt to the ever-changing digital landscape, Robin believes in the power of collaboration and sharing knowledge. Outside of coding, he enjoys diving into tech news and exploring new tools in the industry.

If you'd like to get in touch to discuss a project, or if you'd just like to ask a question, fill in the form below.

Get in touch

Send me a message and I'll get back to you as soon as possible. Ask me about anything - web development, project proposals or just say hi!

Please enable JavaScript in your browser to complete this form.

Solarise.dev Services

Maintenance Retainers

Min 3 hours per month, no contract

Project Planning

Get in touch to discuss your project details

Consultancy

Face-to-face chats. Solve your problems!