Set the Default Post Type for the Posts from Micropub Client

close up shot of a typewriter

Background Knowledge

Introduction to Micropub

Micropub is an open API standard used for creating posts on websites (such as WordPress sites) using third-party clients. Web applications and native apps (e.g., iPhone, Android) can use Micropub to publish short posts, photos, event information (RSVP), or other types of posts to their websites, similar to how a Twitter client publishes to Twitter.com. It requires the IndieAuth plugin for authentication.

Need for a New Post Type

If there is a need to separate the content sent via Micropub from the traditional long articles typically found on WordPress, it is worth considering creating a new post type to specifically put Micropub content.

How to Create a New Post Type

To create a new post type, you can consider installing plugins like IndieBlocks or Shortnotes that are specifically designed and optimized for this type of short content;

You can also install plugins like MB Custom Post Types & Custom Taxonomies or Post Types Unlimited that allow you to add post types through a web-based graphical interface;

Or you can hand-code and add it into function.php (or add to a code deployment plugin like Snippets):

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'my_custom_post',
        array(
            'labels' => array(
                'name' => __( 'My Custom Posts' ),
                'singular_name' => __( 'My Custom Post' )
            ),
            'public' => true,
            'has_archive' => true,
        )
    );
}

(This code creates a new post type during WordPress initialization—the ‘init’ hook. The name of this post type is ‘my_custom_post’, its label name is ‘My Custom Posts’, and it is public with archives. It’s worth noting that the ‘register_post_type()’ function has many available parameters that can be used to customize the post type. For specific parameter details and usage, you can refer to the WordPress official documentation.)

Main Topic

Now to the main point. Once a custom post type is in place, you can consider setting a default post type for content sent by the Micropub client.

Firstly, if you want to post content via Micropub client on your WordPress site, the Micropub plugin is a easy choice:

The plugin provides a series of filters and hooks, allowing users to extend and customize the functionality of the Micropub plugin.

According to the official documentation, the relevant Micropub filter is:

micropub_post_type( $post_type = 'post', $input )

This filter is invoked in the process of creating a Micropub post. By default, this filter sets the new post’s type to ‘post’, but it also allows for the Micropub posts to be set as a custom post type.

To use this filter, you can add similar code to the following in the ‘functions.php’ file (or a code deployment plugin like Snippets):

function your_function_name($post_type, $input) {
    // Here you can modify the value of $post_type based on $input
    $post_type = 'post_type_name';

    return $post_type;
}
add_filter('micropub_post_type', 'your_function_name', 10, 2);

‘your_function_name’ is the function name and can be changed to any name you prefer (for instance, you could call it ‘set_custom_micropub_post_type’). ’10’ is the priority of this function when executing the filter, and ‘2’ is the number of parameters the function accepts. In this way, each time a Micropub post is created, it will set the post type to ‘post_type_name’ (modify this name according to your situation).

This is just a simple implementation. The internal logic of the ‘your_function_name’ function also needs to be modified according to actual requirements.

Discover more from Sang-nguok

Subscribe now to keep reading and get access to the full archive.

Continue reading