DEV Community

Fronttt
Fronttt

Posted on • Originally published at frontendli.com

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() {
    register_post_type('questions',
        array(
            '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: http://frontendli.com
Description: Plugin to registers Questions 
Version: 1.0
Author: Tiranga Sheetal Beck
Author URI: http://frontendli.com
Textdomain: Frontendli
License: GPLv2
*/
Enter fullscreen mode Exit fullscreen mode

Here is the full code with registering categories & taxonomies.[https://frontendli.com/create-separate-plugin-for-custom-post-type-wordpress/ ]

Top comments (0)