DEV Community


Posted on • Originally published at

Create A Separate Plugin For Custom Post Type In WordPress

Creating Custom Post Type of WordPress is pretty easy.

Register post type in your functions.php and you're ready to go.

function custom_post_type_question() {
            'labels'      => array(
                'name'          => __( 'Questions', 'textdomain' ),
                'singular_name' => __( 'Question', 'textdomain' ),
            'public'      => true,
            'show_in_graphql' => true,
            'hierarchical' => true,
            'graphql_single_name' => 'question',
            'graphql_plural_name' => 'questions',
            'has_archive' => true,
            'rewrite'     => array( 'slug' => 'faq' ), // my custom slug

add_action('init', 'custom_post_type_question');
Enter fullscreen mode Exit fullscreen mode

Why you should create separate plugin for custom post type

Because when you change the theme, the Custom Post Type code which was in functions.php will be gone.

So for creating a plugin write this declaration in your php file.

Plugin Name: Frontendli Questions
Plugin URI:
Description: Plugin to registers Questions 
Version: 1.0
Author: Tiranga Sheetal Beck
Author URI:
Textdomain: Frontendli
License: GPLv2
Enter fullscreen mode Exit fullscreen mode

Here is the full code with registering categories & taxonomies.[ ]

Discussion (0)