Search This Blog
Popular Posts
-
Elegant Themes has been developing WordPress themes for a long time. It has developed lots of popular themes including Divi, Nexus, Fa...
-
Flickr/Laura D'Alessandro See Also I took Harvard Business School's new pre-MBA course online — and it is definitely w...
-
Hello there! My blog post 'Spikes' was published on Sept. 22nd; however, the post before it called 'Flow' was published on...
-
I will publish an article only when I have something important to say. That's what I reminded myself every time the egocentric ...
-
Hi there! There isn't a true e-commerce solution here at WordPress.com. You can, however get a PayPal button. If you get a PayPal bu...
-
Good news for the secure web: WordPress will now encrypt the traffic for over a million more websites that are hosted on its servers. Wo...
-
How to start a blog or website in 5 minutes with WordPress. After publishing the post on how I started blogging full-time, I'v...
-
KOZHIKODE: E A Jabbar, a retired teacher and an activist of Malappuram based Yukthi vadi Sangham, has filed a complaint before chief minis...
-
At the ripe young age of 32, back in 2009, Uber CEO Travis Kalanick apparently launched a Wordpress blog called Swooshing, and for some ...
-
Wednesday the latest version of WordPress 4.7.1 was released by the WordPress Team, it is classified as a security release for all pre...
Blog Archive
- December (18)
- November (29)
- October (27)
- September (29)
- August (31)
- July (30)
- June (29)
- May (29)
- April (30)
- March (31)
- February (28)
- January (31)
- December (31)
- November (30)
- October (31)
- September (30)
- August (43)
- July (42)
- June (33)
- May (43)
- April (36)
- March (37)
- February (31)
- January (4)
- December (1)
- November (1)
- October (24)
- September (24)
- August (25)
- July (28)
- June (18)
- September (1)
Total Pageviews
Blogroll
WordPress Custom Post Types â Notices and Taxonomies
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 NoticesAre 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 TaxonomiesIn 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.
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.
ConclusionCustom 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.
Source: WordPress Custom Post Types – Notices and Taxonomies
0 comments:
Post a Comment