- 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 – 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
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 that 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:
You must be logged in and have an active premium membership to view the rest of this content. Register or login from the sidebar.


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.
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