This entry is part 6 of 14 in the Plugin Development 101 Series
- Introduction to WordPress Plugin Development 101
- Plugin Development 101 – What Makes a Plugin?
- Plugin Development 101 – General Best Practices
- Plugin Development 101 – An Intro to Filters
- Plugin Development 101 – Intro to Actions
- Plugin Development 101 – Registering a Custom Post Type
- Plugin Development 101 – Intro to Short Codes
- Plugin Development 101 – Intro to Loading Scripts and Styles
- Plugin Development 101 – Introduction to Adding Dashboard Menus
- Plugin Development 101 – Separating Your Plugin into Multiple Files
- Plugin Development 101 – Your First OOP Plugin
- Plugin Development 101 – Dissecting the Featured Comments Plugin
- Plugin Development 101 – Digging Into WordPress Core to Solve a Problem
- Plugin Development 101 – Introduction to extending classes
This part of the series is the first one where we look at creating a complete sample plugin. It’s a very simple plugin, but a complete plugin nonetheless. For this plugin we are registering a new customer post type called “Books”. Registering custom post types is something that we do a lot in plugin development, so having an understanding of how it works is very important.
More importantly than understanding how custom post types are created, however, is understanding the process of “hooking” your plugin’s functions into WordPress, and that’s really what this tutorial helps to illustrate.
The complete code written in the tutorial is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <?php /* Plugin Name: Plugin Development 101 Plugin URI: https://pippinsplugins.com/series/plugin-development-101/ Description: An introductory course on plugin development Author: Pippin Williamson Version: 1.0 Author URI: https://pippinsplugins.com */ function pd101_register_book_post_type() { $labels = array( 'name' => 'Books', 'singular_name' => 'Book', 'add_new' => 'Add New', 'add_new_item' => 'Add New Book', 'edit_item' => 'Edit Book', 'new_item' => 'New Book', 'all_items' => 'All Books', 'view_item' => 'View Book', 'search_items' => 'Search Books', 'not_found' => 'No books found', 'not_found_in_trash' => 'No books found in Trash', 'parent_item_colon' => '', 'menu_name' => 'Books' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'book' ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ) ); register_post_type( 'book', $args ); } add_action( 'init', 'pd101_register_book_post_type' ); |
Please see the WordPress codex for more info on the register_post_type() function.
Hey Pippin!
But how to extend this plugin so that if you activate it and it greates cpt + some default pages? Maybe some suggestions?
That’s a topic that goes beyond this part of the series, but we can definitely talk about it later on.
I’d also like to see more advanced discussion regarding this.
Can’t get this work correctly with any permalink structure beside http://localhost/pd/wordpress/?p=123, if I change the pemalink structure to http://localhost/pd/wordpress/sample-post/, I get 404 errors once the custom post type is published. Strange thing is that when the post is still in draft mode I can view and everything is fine, but once I publish the post, everything stops working and I get 404 errors.
Any ideas on how to fix this?
This is usually caused by a plugin or theme flushing the rewrite rules (permalinks) on every page load: http://codex.wordpress.org/Function_Reference/flush_rewrite_rules
I would try deactivating your other plugins and switching to one of the default themes. Once you’ve done that, refresh your permalink structure and test. If it works then, reactivate each of your plugins (and your theme) one by one until you find the one causing the problem.
Thanks Pippin, although apparently it was a webserver issues.
Speaking of flushing the rewrite rules… is this a good way to flush them?
function pd101_rewrite_flush() {
flush_rewrite_rules();
}
register_activation_hook( __FILE__, ‘pd101_rewrite_flush’ );
That’s the correct way.
Hi Pippin
Catching up to the publications.
How about actually saying what the Sublime formatting “plugin” you almost mentioned is? Any tool of that nature would make one more efficient, logically speaking. Compliant code, efficiently constructed represent fundamental foundations for the happy expressions of your students, don’t you think?
Listen around 5:15.
Thanks
Oh yes, of course 🙂
It’s TidyPHP: https://github.com/welovewordpress/SublimePhpTidy
Hey pipin I m scratching my to get this work, I have created a custom post type for news(not plugin but in my theme) and its was working great on my localhost and my test website however when I uploaded my theme to the client’s website the news pages dont show up(link goes to 404 page). I m able to create new ‘news pages’ but when I click on ‘Preview’ page (in admin), it again takes me to 404 page.
Again its working great locally and on my test website but not on my client’s server.
My client’s site: http://www.redneckyachtclubokc.com/
My test site; http://mstoicthemes.com/
Any idea why its not working my client’s website?
This is code I wrote
add_action( ‘init’, ‘create_post_type’ );
function create_post_type() {
register_post_type( ‘news’,
array(
‘labels’ => array(
‘name’ => __( ‘News’ ),
‘singular_name’ => __( ‘News’ )
),
‘public’ => true,
‘has_archive’ => true,
)
);
}
Have you flushed the permalinks?
Sorry got some spelling mistakes.
Didn’t do it before but tried it just now, and guess what, now its not working even on my localhost either .This is how I flushed it
add_action(‘init’, ‘custom_taxonomy_flush_rewrite’);
function custom_taxonomy_flush_rewrite() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
Hey thanks a lot Pippin, I just wrote this after the custom post type registration function and it fixed the problem, I dont know why it wasn’t working when I wrote it before the funciton.
All thanks to you.
Do not ever flush rewrite rules on “init”. That will cause the rules to be flushed on every single page load. The best thing to do is flush on plugin/theme activation: http://codex.wordpress.org/Function_Reference/register_post_type#Flushing_Rewrite_on_Activation
Sure thanks for telling will certainly keep that in mind
Looking forward to reading more .. I’m a subscription holder and am seeing the ” You must be logged in and have an active premium membership to view the rest of this content ” message. Totally logged in!
Can you tell me your username so I can look up your account?
Hi is the video is working ?
I got an error can’t find media source ( maybe it because I’m using a iPad )
It’s working for me. Have you been able to try it from a computer?
From a computer it’s working great.
Guessing that the mobile version is the problem
Custom post-types is such a common need for CMS customization I surprised this isn’t part of the “core” of WP – Luckily we have this fantastic plugin!
Note for Future Users: Spent 20min trying to figure out why all my info was displaying as one giant block of text at the top.
Solution: You need to put <?php at the top of the file you cannot just name the file .php and expect it to work.
Hi Pippin,
Thank you for awesome tutorials for paid members. I love the way you teach. so simple and easy to understand.
I started developing a plugin following your tuts. But I am stuck up with “meta boxes”. I have created a custom post type “Hot Deals” which has a Title, Description, Featured Image — included in “support”. But in addition to this I want to add custom fields — “Call to Action Text”, “Call to Action Link”, “Call to Action button BG color”.
Is there any tuts in your existing list which teaches how to accomplish above (creating custom fields and displaying them on the front end) — I want to code this in my plugin “Hot Deals” without using any custom post types plugins.
Kindly advice.
I don’t have one that I can think of off the top of my head, sorry.
Hi Pippin,
Great content.
I have a question.
Let’s say that the Custom Post Type is as you made it, how I could add a custom field? Let’s say I want to add a Rating custom field.
Br,
John
I’d recommend checking out CMB2: https://github.com/WebDevStudios/CMB2
Hey,
Do you have a video on how to add a custom taxonomy (hierarchical and non-hierarchical )