SOLARISE
DEV

Customizing Textbroker WordPress Imports: Auto-Features & Editor Control

Streamline Your Content Workflow with These PHP Snippets for WordPress

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_id){ // Changed $post to $post_id for clarity as it's an ID

    global $HTTP_RAW_POST_DATA, $wpdb;

    // It's better to check if $HTTP_RAW_POST_DATA is set before using stripos
    if (isset($HTTP_RAW_POST_DATA) && (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_id);
        $wpdb->query($query);

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

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

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

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_id){ // Changed $post to $post_id

    global $HTTP_RAW_POST_DATA, $wpdb;

    if (isset($HTTP_RAW_POST_DATA) && (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'; // Example post type
        $query = $wpdb->prepare("UPDATE {$wpdb->prefix}posts SET post_type = %s WHERE ID = %d", $yourPostType, $post_id);
        $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_xmlrpc we set previously (corrected from source_textbroker for consistency with the first snippet).


add_filter('user_can_richedit', 'disable_wyswyg_for_xmlrpc_post');
function disable_wyswyg_for_xmlrpc_post( $default ){
    global $post; // $post should be available in this context

    // Ensure $post is an object and has an ID
    if (is_object($post) && isset($post->ID)) {
        $fromXMLRPC = get_post_meta($post->ID, 'source_xmlrpc', true);
        if($fromXMLRPC){ // No need for explicit '== 1' if it's a boolean or '1'/'0' string
            return false;
        }
    }
    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.

Robin Metcalfe

About the Author: Robin Metcalfe

Robin is a freelance web strategist and developer based in Edinburgh, with over 15 years of experience helping businesses build effective and engaging online platforms using technologies like Laravel and WordPress.

Get in Touch