WordPress
Article

WordPress Custom Post Types – Notices and Taxonomies

By Agbonghama Collins

In my previous post, I introduced Custom Post Types (CPT) and how to create one for your WordPress powered website.

We also took a look at how to customize the various UI labels of a custom post type to make it distinct from the native post and page post types. However, we didn’t cover how to customize the admin notices generated by them.

In this tutorial, I will be covering how to customize these notices and also how to register new taxonomies to a custom post type.

Customizing CPT Admin Notices

Are you familiar with the alert message that is displayed near the top of admin pages for example when a post is saved as draft, published or even when you save the settings of a plugin? This message is what is referred to as an admin notice.

By default, the admin notices displayed when working on a custom post assumes you are dealing with a post post type and therefore, when for example a book post type is updated, the following notice is displayed: Post updated. View post.

You can change the text of these messages easily by using the post_updated_messages hook like so:

add_filter( 'post_updated_messages', 'book_cpt_messages' );


/**
 * Book CPT updates messages.
 *
 * @param array $messages Existing post update messages.
 *
 * @return array Amended book CPT notices
 */
function book_cpt_messages( $messages ) {
    $post             = get_post();
    $post_type        = get_post_type( $post );
    $post_type_object = get_post_type_object( $post_type );

    $messages['book'] = array(
        0  => '', // Unused. Messages start at index 1.
        1  => __( 'Book updated.', 'textdomain' ),
        2  => __( 'Custom field updated.', 'textdomain' ),
        3  => __( 'Custom field deleted.', 'textdomain' ),
        4  => __( 'Book updated.', 'textdomain' ),
        5  => isset( $_GET['revision'] ) ? sprintf( __( 'Book restored to revision from %s', 'textdomain' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
        6  => __( 'Book published.', 'textdomain' ),
        7  => __( 'Book saved.', 'textdomain' ),
        8  => __( 'Book submitted.', 'textdomain' ),
        9  => sprintf(
            __( 'Book scheduled for: <strong>%1$s</strong>.', 'textdomain' ),
            date_i18n( __( 'M j, Y @ G:i', 'textdomain' ), strtotime( $post->post_date ) )
        ),
        10 => __( 'Book draft updated.', 'textdomain' )
    );

    if ( $post_type_object->publicly_queryable ) {
        $permalink = get_permalink( $post->ID );

        $view_link = sprintf( ' <a href="%s">%s</a>', esc_url( $permalink ), __( 'View book', 'textdomain' ) );
        $messages[ $post_type ][1] .= $view_link;
        $messages[ $post_type ][6] .= $view_link;
        $messages[ $post_type ][9] .= $view_link;

        $preview_permalink = add_query_arg( 'preview', 'true', $permalink );
        $preview_link      = sprintf( ' <a target="_blank" href="%s">%s</a>', esc_url( $preview_permalink ), __( 'Preview book', 'textdomain' ) );
        $messages[ $post_type ][8] .= $preview_link;
        $messages[ $post_type ][10] .= $preview_link;
    }

    return $messages;
}

Code Explanation: The code above customizes admin notices generated by a book custom post type.

The $messages multi-dimensional array controls the admin notices displayed by any post type.

To customize the messages of a book custom post type, create an index array containing the various messages as the value of $messages['book'].

The if statement checks if the custom post type is publicly query-able. That is, whether the public argument is set to true while registering the custom post type.

If true, a link to view a post belonging to the CPT is added to the admin notice displayed when it is updated, published or scheduled for publication while a link to preview the post is added when it is submitted for review or a draft is updated.

Custom Taxonomies

In WordPress, a taxonomy is mechanism for grouping posts of any type.

Examples of taxonomies include Category for grouping posts that are related to a given category and Tag which is pretty similar to categories but is more free form. More information on taxonomies is available over at the WordPress Codex.

That being said, we are going to cover how to create custom taxonomies. Let us take the example of creating a book post type, categorizing the book entries using the same categories used for blog posts isn’t ideal.

A real life example is the Easy Digital Downloads plugin that uses a download custom post type for digital product entries with a download_category taxonomy for product categorization.

To create a custom taxonomy, use the register_taxonomy() function and hook it to the init Action like so:

add_action( 'init', 'book_category_taxonomy' );

function book_category_taxonomy() {
    register_taxonomy(
        'book_category',
        'book',
        array(
            'label'        => __( 'Book Categories' ),
            'rewrite'      => array( 'slug' => 'book_category' ),
            'hierarchical' => true,
        )
    );
}

If you have a book custom post type already, you should see the category taxonomy added to the admin menu and post edit screen.

Books Custom Post Type

Category taxonomy in post edit screen

You can also use register_post_type() for registering custom post types, the register_taxonomy() function also accepts an array of arguments for customizing labels and configuration of a custom taxonomy.

I won’t be explaining the arguments because they are pretty much the same as that of register_post_type() . A list of the arguments and descriptions is available here.

Conclusion

Custom Post Types are a powerful feature of WordPress and useful in grouping data or post entries that don’t fit into a post and page type. The icing on the cake is the ability to further categorize the posts of a custom post type by registering a custom taxonomy.

Do you have a question or contribution? Please use the comments to let us know.

Meet the author
Collins is a web developer and freelance writer. Creator of the popular ProfilePress and OmniPay WordPress plugins. When not wrangling with code, you can find him writing at his personal blog or on Twitter.

No Reader comments

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in WordPress, once a week, for free.