How we can create custom post type plugin.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

How we can create custom post type plugin:

In this post you will learn how to create a custom post type plugin in WordPress.

 

What is Custom Post Type Plugin?
If you want create own custom posts in your site, you will use custom post type plugin. In this plugin you can create own custom post types and call them whatever you want. The easiest way to create custom post in WordPress is by using a plugin. It is safe and very easy method. In this plugin you will store any type of information. This plugin helps for managing your regular posts.

 

Steps for Create Custom Post type Plugin.

1. Firstly you have to create new directory in WordPress plugin directory as named Cooking. After that open the directory and create the PHP file named as cooking.php and in this file on the top of header add this code:

 

PHP code

<?php

Plugin Name: Cooking
Description: Declares a plugin that will create a custom post type displaying diifrent dish.

?>
 

2.when this code add top of the header file, before closing the Php command you will add this code .Please check this code given below. This code will help for execute the custom function.

 

PHP code

<?php
function create_cooking_review() {
register_post_type( ‘Cooking’
array(
‘labels’ => array(
‘name’ => ‘Cooking’,
‘add_new’=> ‘Add New’,
‘edit’ => ‘Edit’,
‘view’ => ‘View’,
‘not_found’=> ‘No Cooking Reviews found’,

),

‘public’=> true,
‘menu_position’=> 10,
‘supports’ => array( ‘title’, ‘editor’,‘comments’, ‘thumbnail’, ‘custom-fields’ ),

)
);
}
?>

 

Usage for this code…
a. Register_post_type () : This function is used for custom fields, post thumbnails, post comments or Meta box.
b. labels: An array of labels for this post type. In this array we use different kind of features which is help for visit the icon and functionality for plugin in admin.
c. Name: It defines the name of custom post type which you create.e.g’Cooking’.
d. Add New: If you will add new post in your site, you can click on add new button.
e. Edit and View: You will edit and view of post respectively.
f. Public: this specifies the visibility of plugin in the backend and frontend.
g. Menu-Position: Specify your plugin menu position in admin.
h. Supports: Specify features for plugin which you want display.

 

3. After that add this code check your WordPress admin dashboard and activate your plugin.
For activation plugin:
a. Go to the WordPress admin dashboard.
b. Click the menu “Plugins” from sidebar.

 

4. After that check your own create custom post type plugin then activate plugin as Shown below…….

image1

 

5. If your plugin is activating, you will see this type of left sidebar menu, as shown below.

image2

 

6. If we can add post, you will click on the Add New option to go to the custom post type editor.

image3

 

7. Which provide a title of cooking, a review and you can also set the feature images for your dish .you will add any kind of content for feature images. As show below.

image4

 

If you will create a Custom Template for Custom Post Types, you will see this code given below:

 

What is custom template for custom post type?
When you create your own custom post type, you can use single.php file for the contents which is display on the front-end of the custom posts. It means your custom types will display with only their title, images and content.

 

Create the custom template….
PHP code

<?php
add_filter( ‘template_include’, ‘template_function’, 1 );
functiontemplate_function ( $template_path ) {
if ( get_post_type() == ‘Cooking’ ) {
if ( is_single() ) {
if ( $theme_file = locate_template( array ( ‘cooking.php’ ) ) ) {
$template_path = $theme_file;
} else {
$template_path = plugin_dir_path( __FILE__ ) . ‘/cooking.php’;
}}}
return $template_path;
}
?>
 

Usage for this code…….
If your post type is cooking which you are define in own code after that if your site page is single.php template then load the single page from your theme file.Othervise page load from plugin directory. This code is use only in cooking.php file before close the Php tag.

 

After that you will use short code for show post on front page. This code will help. Shown below:
PHP code

<?php
function HelloShortcode() {
$html = ”;
$mypost = array ‘post_type’ => ‘cooking’ );
$loop = new WP_Query($mypost );
while ( $loop->have_posts() ) : $loop-> the_post() ;
$html .=‘</div>’;
$html .= the_title() ;
$html .= ‘</div>’;
$html .= ‘</div></div class=”entry-content”>’. the_content().'</div></div>’;
endwhile;
return $html;
}
add_shortcode(‘helloworld’,‘HelloShortcode’);
?>

In this code you will use short code for visit the post on front page. You can create a short code name whatever you want Like [helloworld] after that use function name which you create .You will use html with the help of any variable name like’$html’ and concate this variable with html structure which is used for front page design. In this code firstly set the title of post and after that set the content of post. As shown given below….

 

vgnjdrhfesgujresigfptres
Download Code