• Hi,

    I have decided to create my first plugin(Foxware Recipe), iv already learnt a little bit from working with WordPress for about a year now and i have the basic understandings of different things in WordPress and what they are for (or have become familiar with looking it up in the codex) and the structure of the DB.

    I’m using boilerplate, and i have successfully registered my first custom post type, but now i can’t figure out how to register a second post type.

    The plugin im trying to create is a recipe costing program where a user can create A recipe and add ingredients to it, they can also add a supplier and place a cost for each ingredient in the supplier_meta table.

    So i need to create 3 custom post types, Recipe, Ingredient and Supplier. I prefer to use a full post type for the ingredient instead of a taxonomy because the ingredient will have data attached to it like unit measure, qty, alternative ingredients etc…

    So my question is, how do i create 3 custom post type? I have placed the code below into my plugin-name.php file in my plugin folder

    // If this file is called directly, abort.
    if ( ! defined( 'WPINC' ) ) {
    	die;
    }
    
    /**
     * The code that runs during plugin activation.
     */
    require_once plugin_dir_path( __FILE__ ) . 'includes/class-foxware_recipe-activator.php';
    
    /**
     * The code that runs during plugin deactivation.
     */
    require_once plugin_dir_path( __FILE__ ) . 'includes/class-foxware_recipe-deactivator.php';
    
    /** This action is documented in includes/class-foxware_recipe-activator.php */
    register_activation_hook( __FILE__, array( 'foxware_recipe_Activator', 'activate' ) );
    
    /** This action is documented in includes/class-foxware_recipe-deactivator.php */
    register_deactivation_hook( __FILE__, array( 'foxware_recipe_Deactivator', 'deactivate' ) );
    
    /**
     * The core plugin class that is used to define internationalization,
     * dashboard-specific hooks, and public-facing site hooks.
     */
    require_once plugin_dir_path( __FILE__ ) . 'includes/class-foxware_recipe.php';
    
    /**
     * Begins execution of the plugin.
     *
     * Since everything within the plugin is registered via hooks,
     * then kicking off the plugin from this point in the file does
     * not affect the page life cycle.
     *
     * @since    1.0.0
     */
    function run_foxware_recipe() {
    
    	$plugin = new foxware_recipe();
    	$plugin->run();
    
    }
    run_foxware_recipe();
    
    add_action( 'init', 'create_foxware_recipe' );
    function create_foxware_recipe() {
        register_post_type( 'foxware_recipe',
            array(
                'labels' => array(
                    'name' => 'Recipes',
                    'singular_name' => 'Recipe',
                    'add_new' => 'Add New',
                    'add_new_item' => 'Add New Recipe',
                    'edit' => 'Edit',
                    'edit_item' => 'Edit Recipe',
                    'new_item' => 'New Recipe',
                    'view' => 'View',
                    'view_item' => 'View Recipe',
                    'search_items' => 'Search Recipes',
                    'not_found' => 'No Recipes found',
                    'not_found_in_trash' => 'No Recipes found in Trash',
                    'parent' => 'Parent Recipe'
                ),
    
                'public' => true,
                'menu_position' => 15,
                'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' ),
                'taxonomies' => array( '' ),
                'menu_icon' => plugins_url( 'images/foxware_icon.png', __FILE__ ),
                'has_archive' => true
            )
        );
    }

    Any help would be greatly appreciated, and please let me know if i’m doing this incorrectly, i get the feeling that boilerplate requires me to register a custom post type somewhere else, but im not seeing it in the comments and i dont think boilerplate has documentations set up.

Viewing 1 replies (of 1 total)
  • Thread Starter Julian Fox (greataussiepie)

    (@greataussiepie)

    Finally managed to do it ?? i was browsing many tutorials online but finally when i just copied and pasted the code directly from the codex and then just copied the add_action instead of the whole function it worked better, but it only seemed to work when i used the code from the codex.

    I think this is resolved but if anyone can help me improve what im trying to do that would be awesome. I now have to add meta boxs to the custom post types and then create a relationship between recipes and ingredients. I want to list the ingredients on the add new or edit recipe page. I think i mentioned that i decided to use a custom post type relationship instead of taxonomy because i want to add lots more data to ingredient post type later on so i can perform calculations.

    My code now looks like this:

    add_action( 'init', 'create_foxware_recipe_post_types' );
    /**
     * Register a post type.
     *
     * @link https://codex.www.ads-software.com/Function_Reference/register_post_type
     */
    function create_foxware_recipe_post_types() {
    	$labels = array(
    		'name'               => _x( 'Recipes', 'post type general name', 'your-plugin-textdomain' ),
    		'singular_name'      => _x( 'Recipe', 'post type singular name', 'your-plugin-textdomain' ),
    		'menu_name'          => _x( 'Recipes', 'admin menu', 'your-plugin-textdomain' ),
    		'name_admin_bar'     => _x( 'Recipe', 'add new on admin bar', 'your-plugin-textdomain' ),
    		'add_new'            => _x( 'Add New', 'Recipe', 'your-plugin-textdomain' ),
    		'add_new_item'       => __( 'Add New Recipe', 'your-plugin-textdomain' ),
    		'new_item'           => __( 'New Recipe', 'your-plugin-textdomain' ),
    		'edit_item'          => __( 'Edit Recipe', 'your-plugin-textdomain' ),
    		'view_item'          => __( 'View Recipe', 'your-plugin-textdomain' ),
    		'all_items'          => __( 'All Recipes', 'your-plugin-textdomain' ),
    		'search_items'       => __( 'Search Recipes', 'your-plugin-textdomain' ),
    		'parent_item_colon'  => __( 'Parent Recipes:', 'your-plugin-textdomain' ),
    		'not_found'          => __( 'No Recipes found.', 'your-plugin-textdomain' ),
    		'not_found_in_trash' => __( 'No Recipes found in Trash.', 'your-plugin-textdomain' )
    	);
    
    	$args = array(
    		'labels'             => $labels,
    		'public'             => true,
    		'publicly_queryable' => true,
    		'show_ui'            => true,
    		'show_in_menu'       => true,
    		'query_var'          => true,
    		'rewrite'            => array( 'slug' => 'recipe' ),
    		'capability_type'    => 'post',
    		'has_archive'        => true,
    		'hierarchical'       => true,
    		'menu_position'      => 5,
    		'menu_icon' => plugins_url( 'images/foxware_icon.png', __FILE__ ),
    		'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt' )
    	);
    
    	register_post_type( 'foxware_recipe', $args );
    
    	$labels = array(
    		'name'               => _x( 'Ingredients', 'post type general name', 'your-plugin-textdomain' ),
    		'singular_name'      => _x( 'Ingredient', 'post type singular name', 'your-plugin-textdomain' ),
    		'menu_name'          => _x( 'Ingredients', 'admin menu', 'your-plugin-textdomain' ),
    		'name_admin_bar'     => _x( 'Ingredient', 'add new on admin bar', 'your-plugin-textdomain' ),
    		'add_new'            => _x( 'Add New', 'Ingredient', 'your-plugin-textdomain' ),
    		'add_new_item'       => __( 'Add New Ingredient', 'your-plugin-textdomain' ),
    		'new_item'           => __( 'New Ingredient', 'your-plugin-textdomain' ),
    		'edit_item'          => __( 'Edit Ingredient', 'your-plugin-textdomain' ),
    		'view_item'          => __( 'View Ingredient', 'your-plugin-textdomain' ),
    		'all_items'          => __( 'All Ingredients', 'your-plugin-textdomain' ),
    		'search_items'       => __( 'Search Ingredients', 'your-plugin-textdomain' ),
    		'parent_item_colon'  => __( 'Parent Ingredients:', 'your-plugin-textdomain' ),
    		'not_found'          => __( 'No Ingredients found.', 'your-plugin-textdomain' ),
    		'not_found_in_trash' => __( 'No Ingredients found in Trash.', 'your-plugin-textdomain' )
    	);
    
    	$args = array(
    		'labels'             => $labels,
    		'public'             => true,
    		'publicly_queryable' => true,
    		'show_ui'            => true,
    		'show_in_menu'       => true,
    		'query_var'          => true,
    		'rewrite'            => array( 'slug' => 'ingredient' ),
    		'capability_type'    => 'post',
    		'has_archive'        => true,
    		'hierarchical'       => true,
    		'menu_position'      => 5,
    		'menu_icon' => plugins_url( 'images/foxware_icon.png', __FILE__ ),
    		'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt' )
    	);
    
    	register_post_type( 'foxware_ingredient', $args );
    
    		$labels = array(
    		'name'               => _x( 'Suppliers', 'post type general name', 'your-plugin-textdomain' ),
    		'singular_name'      => _x( 'Supplier', 'post type singular name', 'your-plugin-textdomain' ),
    		'menu_name'          => _x( 'Suppliers', 'admin menu', 'your-plugin-textdomain' ),
    		'name_admin_bar'     => _x( 'Supplier', 'add new on admin bar', 'your-plugin-textdomain' ),
    		'add_new'            => _x( 'Add New', 'Supplier', 'your-plugin-textdomain' ),
    		'add_new_item'       => __( 'Add New Supplier', 'your-plugin-textdomain' ),
    		'new_item'           => __( 'New Supplier', 'your-plugin-textdomain' ),
    		'edit_item'          => __( 'Edit Supplier', 'your-plugin-textdomain' ),
    		'view_item'          => __( 'View Supplier', 'your-plugin-textdomain' ),
    		'all_items'          => __( 'All Suppliers', 'your-plugin-textdomain' ),
    		'search_items'       => __( 'Search Suppliers', 'your-plugin-textdomain' ),
    		'parent_item_colon'  => __( 'Parent Suppliers:', 'your-plugin-textdomain' ),
    		'not_found'          => __( 'No Suppliers found.', 'your-plugin-textdomain' ),
    		'not_found_in_trash' => __( 'No Suppliers found in Trash.', 'your-plugin-textdomain' )
    	);
    
    	$args = array(
    		'labels'             => $labels,
    		'public'             => true,
    		'publicly_queryable' => true,
    		'show_ui'            => true,
    		'show_in_menu'       => true,
    		'query_var'          => true,
    		'rewrite'            => array( 'slug' => 'supplier' ),
    		'capability_type'    => 'post',
    		'has_archive'        => true,
    		'hierarchical'       => true,
    		'menu_position'      => 5,
    		'menu_icon' => plugins_url( 'images/foxware_icon.png', __FILE__ ),
    		'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt' )
    	);
    
    	register_post_type( 'foxware_supplier', $args );
    
    }
Viewing 1 replies (of 1 total)
  • The topic ‘How do i register multiple custom post types’ is closed to new replies.