{"id":19405,"date":"2012-08-12T18:30:05","date_gmt":"2012-08-12T18:30:05","guid":{"rendered":"https:\/\/wordpress.org\/plugins-wp\/go-cptfactory\/"},"modified":"2012-08-13T02:58:07","modified_gmt":"2012-08-13T02:58:07","slug":"go-cptfactory","status":"publish","type":"plugin","link":"https:\/\/wordpress.org\/plugins\/go-cptfactory\/","author":72725,"comment_status":"closed","ping_status":"closed","template":"","meta":{"version":"a1","stable_tag":"trunk","tested":"3.4.2","requires":"3.3","requires_php":"","requires_plugins":"","header_name":"GigaOM Custom Post Type Factory Hello World Example","header_author":"Casey Bisson","header_description":"","assets_banners_color":"","last_updated":"2012-08-13 02:58:07","external_support_url":"","external_repository_url":"","donate_link":"","header_plugin_uri":"http:\/\/GigaOM.com\/","header_author_uri":"http:\/\/GigaOM.com\/","rating":0,"author_block_rating":0,"active_installs":0,"downloads":1454,"num_ratings":0,"support_threads":0,"support_threads_resolved":0,"author_block_count":0,"sections":["description","installation","faq"],"tags":[],"upgrade_notice":[],"ratings":{"1":0,"2":0,"3":0,"4":0,"5":0},"assets_icons":[],"assets_banners":[],"assets_blueprints":{},"all_blocks":[],"tagged_versions":[],"block_files":[],"assets_screenshots":[],"screenshots":[]},"plugin_section":[],"plugin_tags":[1487,12260,3772,2278,166],"plugin_category":[],"plugin_contributors":[],"plugin_business_model":[],"class_list":["post-19405","plugin","type-plugin","status-publish","hentry","plugin_tags-custom-post-types","plugin_tags-custom-posts","plugin_tags-developers","plugin_tags-post-types","plugin_tags-posts","plugin_committers-misterbisson"],"banners":[],"icons":{"svg":false,"icon":"https:\/\/s.w.org\/plugins\/geopattern-icon\/go-cptfactory.svg","icon_2x":false,"generated":true},"screenshots":[],"raw_content":"<!--section=description-->\n<p>This is a convenience class and collection of methods to make creating new custom post types with their own metaboxes easy.<\/p>\n <p>The general idea is to define a function that's hooked to the <code>register_post_types<\/code> action. The function defines a class for your post type that extends the <code>GO_Cpt<\/code> class. This follows a design pattern established by the modern widgets API (as introduced in WP 2.8) http:\/\/codex.wordpress.org\/Widgets_API#Developing_Widgets .<\/p>\n <p>The result is a new custom post type and much easier creation and updating of custom meta associated with that post type. You'll also have a convenient object you can use to access the metadata associated with each post.<\/p>\n <pre><code>function my_customposttype() {     class My_CustomPostType extends GO_Cpt     {         function __construct()         {             \/\/ execute the parent constructor with the name \n            \/\/ and definition for the custom post type             parent::__construct( $post_type_name , $post_type_definition );         }          function metabox( $post , $meta )         {             \/\/ print out the form here             \/\/ \n            \/\/ this method is optional, only use it if you want to \n            \/\/ add a metabox to your custom post type         }          function update( $new_meta , $old_meta )         {             \/\/ sanitize and validate the metadata             \/\/ then return $meta so it can be saved             \/\/             \/\/ this method is required if a \n            \/\/ metabox() method is included          }     }      global $my_customposttype;     $my_customposttype = new My_CustomPostType; }  \/\/ hook that function to the register_post_types action add_action( 'register_post_types' , 'my_customposttype' ); <\/code><\/pre>\n <p>Backstory: defining the post type class inside the function avoids issues with loading order and fatal errors resulting from attempting to extend a class that might not exist yet. Isn't PHP awesome?<\/p>\n <p>See the included hello world example: http:\/\/plugins.trac.wordpress.org\/browser\/go-cptfactory\/trunk\/example-helloworld-cpt.php<\/p>\n <p>To access the meta associated with a post, you can call <code>$my_customposttype-&gt;get_meta( $post_id );<\/code><\/p>\n <p>You can easily add a method and hook to filter <code>the_content<\/code> or <code>the_excerpt<\/code> to insert custom values that may have been entered in the post meta. Simply add <code>add_filter( 'the_content' , array( $this , 'the_content' ));<\/code> in the <code>__construct()<\/code> and then define another method in the <code>My_CustomPostType<\/code> class such as:`<\/p>\n <pre><code>    function the_content( $content )     { \n        $meta_print = print_r( $this-&gt;get_meta( get_the_ID() ) , TRUE ); \n        return $meta_print . $content \n    } <\/code><\/pre>\n <p>`<\/p>\n\n<!--section=installation-->\n<ol>\n<li>Place the plugin folder in your <code>wp-content\/plugins\/<\/code> directory and activate it.<\/li>\n<li>To actually use it, you'll have to define a custom post type class. See more: http:\/\/wordpress.org\/extend\/plugins\/go-cptfactory\/<\/li>\n<\/ol>\n\n<!--section=faq-->\n<h4>Q: Code is complex, is there an easier way to do this?<\/h4>\n <p>A: You should consinder Pods: http:\/\/podsframework.org<\/p>\n <h4>Q: If Pods is out there, why did you bother with this?<\/h4>\n <p>A: Pods is cool, but this keeps the problem in code, without any dependency on configuration information stored in the database. This also fits the architecture I like where the code related to a post type is associated with the post type definition. In my use, custom post type classes actually have many more methods to collect, validate, and display their data.<\/p>","raw_excerpt":"Makes defining custom post types as easy as creating a widget. Includes example code to get you started.","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin\/19405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin"}],"about":[{"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/types\/plugin"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/comments?post=19405"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wporg\/v1\/users\/misterbisson"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/media?parent=19405"}],"wp:term":[{"taxonomy":"plugin_section","embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_section?post=19405"},{"taxonomy":"plugin_tags","embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_tags?post=19405"},{"taxonomy":"plugin_category","embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_category?post=19405"},{"taxonomy":"plugin_contributors","embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_contributors?post=19405"},{"taxonomy":"plugin_business_model","embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_business_model?post=19405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}