Creating Custom Post Types in WordPress Without Using Plugins

how to create custom post type in wordpress without plugin
how to create custom post type in WordPress without plugin

In the world of WordPress, custom post types provide a powerful way to extend the functionality of your website. They allow you to organize and display content in a manner that goes beyond the traditional posts and pages. Custom post types enable you to create unique content structures tailored to your specific needs, such as portfolios, testimonials, events, products, and more. In this article, we will learn how to create custom post types in WordPress without the need for third-party plugins.

Code Snippet for Custom Post Type

To create a custom post type in WordPress, you’ll need to add code to your theme’s functions.php file or create a custom plugin. Below is a code snippet that defines a custom post type called “Products.” You can use this as a template for creating your own custom post types.

// Register Custom Post Type
function custom_post_type_products() {
    $labels = array(
        'name'                  => _x( 'Products', 'Post Type General Name', 'text_domain' ),
        'singular_name'         => _x( 'Product', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'             => __( 'Products', 'text_domain' ),
        'name_admin_bar'        => __( 'Product', 'text_domain' ),
        'archives'              => __( 'Product Archives', 'text_domain' ),
        'attributes'            => __( 'Product Attributes', 'text_domain' ),
        'parent_item_colon'     => __( 'Parent Product:', 'text_domain' ),
        'all_items'             => __( 'All Products', 'text_domain' ),
        'add_new_item'          => __( 'Add New Product', 'text_domain' ),
        'add_new'               => __( 'Add New', 'text_domain' ),
        'new_item'              => __( 'New Product', 'text_domain' ),
        'edit_item'             => __( 'Edit Product', 'text_domain' ),
        'update_item'           => __( 'Update Product', 'text_domain' ),
        'view_item'             => __( 'View Product', 'text_domain' ),
        'view_items'            => __( 'View Products', 'text_domain' ),
        'search_items'          => __( 'Search Product', 'text_domain' ),
        'not_found'             => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
        'featured_image'        => __( 'Featured Image', 'text_domain' ),
        'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
        'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
        'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
        'insert_into_item'      => __( 'Insert into Product', 'text_domain' ),
        'uploaded_to_this_item' => __( 'Uploaded to this Product', 'text_domain' ),
        'items_list'            => __( 'Products list', 'text_domain' ),
        'items_list_navigation' => __( 'Products list navigation', 'text_domain' ),
        'filter_items_list'     => __( 'Filter Products list', 'text_domain' ),
    );
    $args = array(
        'label'                 => __( 'Product', 'text_domain' ),
        'description'           => __( 'Post Type Description', 'text_domain' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields' ),
        'taxonomies'            => array( 'category', 'tag' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-cart',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'post',
        'rewrite'               => array( 'slug' => 'products' ),
    );
    register_post_type( 'products', $args );
}
add_action( 'init', 'custom_post_type_products', 0 );

Important Parts of the Code:

  1. register_post_type('products', $args): This function registers a custom post type named “products” with the specified arguments.
  2. $labels: These labels define the various text elements related to your custom post type, like its name, singular name, and menu names.
  3. $args: This array contains a variety of settings for your custom post type, including support for features such as title, editor, thumbnail, excerpt, and custom fields. You can customize these options based on your specific requirements.
  4. menu_icon: You can set a custom icon for your custom post type using the dashicons library. In this example, ‘dashicons-cart’ is used.

Guidelines to Use the Code:

  1. Copy the provided code snippet and paste it into your theme’s functions.php file or create a custom plugin.
  2. Modify the code to match your specific requirements. You can change the custom post type name, labels, supported features, taxonomies, and other settings as needed.
  3. Save the changes, and your custom post type will be registered on your WordPress site.
  4. To start using your custom post type, navigate to the WordPress dashboard, and you will find a new menu item with the name you specified for your post type (in this case, “Products”). You can now create, edit, and manage your custom post-type content just like regular posts and pages.

Conclusion

Creating custom post types in WordPress without relying on third-party plugins gives you more control and flexibility over your website’s content structure. By following the code snippet and guidelines provided in this article, you can tailor your WordPress site to better suit your specific needs and offer a more customized user experience. Whether you’re building a portfolio, showcasing products, or managing testimonials, custom post types empower you to shape your website according to your vision.

Available For New Project

Abdullah Al Imran

I'm Abdullah Al Imran, a Full Stack WordPress Developer ready to take your website to the next level. Whether you're looking for WordPress Theme Development, adding new features, or fixing errors in your existing site, I've got you covered. Don't hesitate to reach out if you need assistance with WordPress, PHP, or JavaScript-related tasks. I'm available for new projects and excited to help you enhance your online presence. Let's chat and bring your website dreams to life!

Leave a Comment

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