18.9 C
New York
Tuesday, May 24, 2022

How to Create Custom Post Type in WordPress without Plugin

- Advertisement -

If you’re stepping into a world greater than a simple blogging platform, then WordPress is all you need to help satiate your needs as it acts as an ideal content management system with which you can create any kind of website. For such a shift, you will need to look into the custom post types as they will assist you in creating different types of content.

First Off, What are Custom Post Types in WordPress?

The term Post Types is used to refer to the various kinds of content displayed in a WordPress site. Usually they refer to posts and pages of a website. However, over a period of time WordPress shifted from a basic blogging platform to a robust CMS, and ever since then the term post just happened to stick to it. Whether you choose to use a WordPress plugin or not

Below are the post types that come with WordPress by default:

  • Post
  • Page
  • Revision
  • Nav menu
  • Attachment

How to Create a Custom Post Type Manually – Without Using a Plugin

As easy as it may seem to use a WordPress plugin to create your custom post types, the major problem you will face is that they will disappear each time the plugin is deactivated. Which means the data will be safe and stored within the plugin, but the custom post type will be unregistered and you will not be able to access it from the admin panel.

So, if you are working on a site for a client and do not wish to install another plugin to get the job done, then you can create your custom post type manually by using codes in your theme’s functions.php file or in the site-specific plugin.

We are going to demonstrate how you can create a custom post type without using a WordPress plugin by creating a “News” section with the help of some simple coding steps. Take a look at the example given below. It should make understanding the workings of it a lot easier.

For this you need to open the folder of your theme, and add the code given below in Appearance > Theme Editor > Functions.php

1    // Our custom post type function
2   function create_posttype() {
3
4          register_post_type( ‘news’,
5          // CPT Options
6                 array(
7                         ‘labels’ => array(
8                               ‘name’ => __( ‘News’ ),
9                               ‘singular_name’ => __( ‘News’ )
10                      ),
11                       ‘public’ => true,
12                       ‘has_archive’ => true,
13                       ‘rewrite’ => array(‘slug’ => ‘News’),
14                       ‘show_in_rest’ => true,
15
16              )
17        );
18    }
19    // Hooking up our function to theme setup
20   add_action( ‘in_it’, ‘create_posttype’ );

So basically, without a WordPress plugin; you have this code and what it will do is register a post type “News” with an array of arguments. And then consider the arguments as the options you have for your custom post type.

In particular, this array has two parts. The first part is labeled as an array, while the second part consists of multiple other arguments such as public visibility, has archive, slug, and show_in_rest block editor support.

Now we’ll show you a detailed example of a code that adds more options to your custom post type.

1    /*
2    * Creating a function to create our CPT
3    */
4
5    function custom_post_type() {
6
7    // Set UI labels for Custom Post Type
8          $labels = array(
9                  ‘name’ => _x( ‘News’, ‘Post Type General Name’,
10    ‘twentytwenty’ ),
11                    ‘singular_name’ => _x( ‘News’, ‘Post Type Singular Name’,
12    ‘twentytwenty’ ),
13                   ‘menu_name’ => __( ‘News’, ‘twentytwenty’ ),
14                   ‘parent_item_colon’ => __( ‘Parent News’, ‘twentytwenty’ ),
15                   ‘all_items’ => __( ‘All News’, ‘twentytwenty’ ),
16                   ‘view_item’ => __( ‘View News’, ‘twentytwenty’ ),
17                   ‘add_new_item’ => __( ‘Add New News’, ‘twentytwenty’ ),
18                   ‘add_new’ => __( ‘Add New’, ‘twentytwenty’ ),
19                   ‘edit_item’ => __( ‘Edit News’, ‘twentytwenty’ ),
20                  ‘update_item’ => __( ‘Update News’, ‘twentytwenty’ ),
21                   ‘search_items’ => __( ‘Search News’, ‘twentytwenty’ ),
22                  ‘not_found’ => __( ‘Not Found’, ‘twentytwenty’ ),
23                  ‘not_found_in_trash’ => __( ‘Not found in Trash’, ‘twentytwenty’ 
24    ),
25          );
26

27    // Set other options for Custom Post Type
28
29          $args = array(
30                  ‘label’ => __( ‘news’, ‘twentytwenty’ ),
31                   ‘description’ => __( ‘News and reviews’, ‘twentytwenty’ ),
32                  ‘labels’ => $labels,
33                  // Features this CPT supports in Post Editor
34                  ‘supports’ => array( ‘title’, ‘editor’, ‘excerpt’,
35    ‘author’, ‘thumbnail’, ‘comments’, ‘revisions’, ‘custom-fields’, ),
36                 // You can associate this CPT with a taxonomy or custom taxonomy.
37                 ‘taxonomies’ => array( ‘genres’ ),
38                 /* A hierarchical CPT is like Pages and can have
39                * Parent and child items. A non-hierarchical CPT
40                * is like Posts.
41                 */
42                 ‘hierarchical’ => false,
43                 ‘public’ => true,
44                 ‘show_ui’ => true,
45                 ‘show_in_menu’ => true,
46                 ‘show_in_nav_menus’ => true,
47                 ‘show_in_admin_bar’ => true,
48                 ‘menu_position’ => 5,
49                 ‘can_export’ => true,
50                 ‘has_archive’ => true,
51                  ‘exclude_from_search’ => false,
52                 ‘publicly_queryable’ => true,
53                 ‘capability_type’ => ‘post’,
54                 ‘show_in_rest’ => true,
55
56        );
57
58        // Registering your Custom Post Type
59        register_post_type( ‘news’, $args );
60
61    }
62
63    /* Hook into the ‘init’ action so that the function
64    * Containing our post type registration is not
         * unnecessarily executed.
         */

         add_action( ‘init’, ‘custom_post_type’, 0 );

With this example it is quite visible how we’ve added multiple options to the custom post type. It adds several more features like support for revisions, feature images, custom fields, etc.

So for each part of the code, we have designed things according to our preference. Which means, if you wish to have your custom post type to behave like pages then you can set the hierarchical value to be true, instead of false as shown above in the example.

If the theme you are using happens to be translation ready, you could also have your custom post types translated. For this all you will need is to use a text domain; which is basically the repeated usage of a string. This is the same as the string ‘twenty-twenty’ used in the example above. Your theme’s text domain can be found in the theme directory in the ‘style.css’ file.

Conclusion

Even today if there’s one thing that didn’t change, that would be the value of one the best WordPress features out there; Custom Post Types. Whether you choose to use a WordPress Plugin or manage to modify with the use of some simple codes, you gain the ability to create something unique and exclusive just for your site. If you are new to this and are willing to try it out for the first time – hopefully this tutorial will suffice to guide you from the start and take you straight to the finish line. Good luck and happy coding!

ournethelps
OurNetHelps is a web-based information portal, we produce unique step by step articles on different subjects. Check out our all articles Here.

Related Articles

Latest Articles