Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Plugin Development 101 – Registering a Custom Post Type

Posted on February 7, 2013 by Pippin in Beginner, Custom Post Types, Member Restricted, Subscriber Only, Tutorials, Writing Plugins 15 Comments
Home» Tutorials » Beginner » Plugin Development 101 – Registering a Custom Post Type
wordpress-plugin-development_101
Tweet
Love It - 3
This entry is part 5 of 8 in the Plugin Development 101 Series
← Plugin Development 101 – An Intro to FiltersPlugin Development 101 – Intro to Short Codes →
  • 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.

Tweet Follow @pippinsplugins
register_post_type

15 comments on “Plugin Development 101 – Registering a Custom Post Type”

  1. Kristjan Koppel says:
    March 6, 2013 at 7:06 am

    Hey Pippin!

    But how to extend this plugin so that if you activate it and it greates cpt + some default pages? Maybe some suggestions?

    Reply
    • Pippin says:
      March 10, 2013 at 12:12 pm

      That’s a topic that goes beyond this part of the series, but we can definitely talk about it later on.

  2. David Jeters says:
    March 12, 2013 at 2:28 pm

    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?

    Reply
    • Pippin says:
      March 12, 2013 at 4:05 pm

      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.

  3. David Jeters says:
    March 12, 2013 at 7:00 pm

    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’ );

    Reply
    • Pippin says:
      March 13, 2013 at 1:18 pm

      That’s the correct way.

  4. Blake LeBlanc says:
    April 14, 2013 at 1:04 am

    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

    Reply
    • Pippin says:
      April 14, 2013 at 1:56 pm

      Oh yes, of course :)

      It’s TidyPHP: https://github.com/welovewordpress/SublimePhpTidy

  5. sayed taqui says:
    June 2, 2013 at 7:39 am

    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,
    )
    );
    }

    Reply
    • Pippin says:
      June 2, 2013 at 2:58 pm

      Have you flushed the permalinks?

  6. sayed taqui says:
    June 2, 2013 at 7:41 am

    Sorry got some spelling mistakes.

    Reply
  7. sayed taqui says:
    June 2, 2013 at 3:13 pm

    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();
    }

    Reply
  8. sayed taqui says:
    June 2, 2013 at 3:20 pm

    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.

    Reply
    • Pippin says:
      June 2, 2013 at 3:36 pm

      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

  9. sayed taqui says:
    June 2, 2013 at 3:41 pm

    Sure thanks for telling will certainly keep that in mind

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

Sorry, no related items found.

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes

Latest Tutorials

  • Storing Session Data in WordPress without $_SESSION (19)

    The term Session in web development refers to...

  • Test Your Plugins with RTL (1)

    Right-To-Left languages are those that...

  • Submitting Your First Pull Request to a WordPress Plugin on Github (5)

    Github is an extremely popular tool for managing WordPress plugins, and one...

Enter your email to receive automated updates when new posts are published

WP Core Contributions

  • [24316]

View the ticket on Trac.

WP Codex Contributions

  • Function: shortcode exists
  • Function: has shortcode
  • Function: shortcode exists
  • Function: shortcode exists
  • Function: has shortcode

View all 41 changes in the Codex.

Latest Tweets

  • Could not fetch Twitter RSS feed.

Topics

campaign monitor get_user_meta the_content Tom McFarlin add_shortcode shortcodes contextual help register_setting Sugar Event Calendar attachments meta box hook wp_enqueue_script Related posts login authors attachment image plugin forms do_action short codes mail chimp recent posts post types apply_filters comments short code bbpress taxonomies custom post type images gallery Ajax Stripe taxonomy jquery widgets users add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2013 Pippin's Plugins