How To Create A WordPress Plugin
Here is how WordPress Codex defines WordPress Plugin:
A WordPress Plugin is a program, or a set of one or more functions, written in the PHP scripting language, that adds a specific set of features or services to the WordPress weblog, which can be seamlessly integrated with the weblog using access points and methods provided by the WordPress Plugin Application Program Interface (API).
This article will take you through a WordPress plugin development guide. I hope that by the end of this guide, you will be able to create a WordPress plugin.
Starting a Plugin File
The first step in creating a WordPress plugin is starting a plugin file. A plugin must contain a file with meta information which tells WordPress what it is and how to handle the plugin within your website. Plugins can be installed, deleted, activated, and inactivated.
Open the folder wp-content/plugins/wpblog/ and create a new file inside it called wpblog-reviews.php
The path to the file should now be wp-content/plugins/wpblog/wpblog-reviews.php. The information should be formatted as follows:
/**
* Plugin Name: Custom WP Blog Reviews
* Description: This is simple plugin which deal with custom post type.
* Version: 1.0
**/
With this information added, save your file and navigate to your WordPress admin area.
Open the file wpblog-reviews.php. Add the following code to it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
function register_wpblog_review() { $labels = array( 'name' => _x('wpblog Reviews', 'wpblog_review'), 'singular_name' => _x('wpblog Review', 'wpblog_review'), 'add_new' => _x('Add New', 'wpblog_review'), 'add_new_item' => _x('Add New wpblog Review', 'wpblog_review'), 'edit_item' => _x('Edit wpblog Review', 'wpblog_review'), 'new_item' => _x('New wpblog Review', 'wpblog_review'), 'view_item' => _x('View wpblog Review', 'wpblog_review'), 'search_items' => _x('Search wpblog Reviews', 'wpblog_review'), 'not_found' => _x('No wpblog reviews found', 'wpblog_review'), 'not_found_in_trash' => _x('No wpblog reviews found in Trash', 'wpblog_review'), 'parent_item_colon' => _x('Parent wpblog Review:', 'wpblog_review'), 'menu_name' => _x('wpblog Reviews', 'wpblog_review'), ); $args = array( 'labels' => $labels, 'hierarchical' => true, 'description' => 'wpblog reviews filterable by genre', 'supports' => array('title', 'editor', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes'), 'taxonomies' => array('genres'), 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, 'menu_icon' => 'dashicons-format-audio', 'show_in_nav_menus' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'has_archive' => true, 'query_var' => true, 'can_export' => true, 'rewrite' => true, 'capability_type' => 'post' ); register_post_type('wpblog_review', $args); } add_action('init', 'register_wpblog_review'); |
At this point, visit the WordPress admin area. If everything is on track, you will see a new custom post type:
Below, I will add a new music review.
Our code now works. I will now create a page that will use this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function create_wpblog_review_pages() { //post status and options $post = array( 'comment_status' => 'open', 'ping_status' => 'closed' , 'post_date' => date('Y-m-d H:i:s'), 'post_name' => 'wpblog_review', 'post_status' => 'publish' , 'post_title' => 'wpblog Reviews', 'post_type' => 'page', ); //insert page and save the id $newvalue = wp_insert_post( $post, false ); //save the id in the database update_option( 'mrpage', $newvalue ); } // // Activates function if plugin is activated register_activation_hook( __FILE__, 'create_wpblog_review_pages'); |
Finally, I will create the WP Blog Reviews page once the plugin has been activated.
Fetch Content On the FrontEnd
Start by creating a template file template-wpblogreview.php, located in the theme folder. Open the file and add the following code to it:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php /*Template Name: WP Blog*/ get_header(); query_posts(array( 'post_type' => 'music_review' )); ?> <?php while (have_posts()) : the_post(); ?> <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> <p><?php the_excerpt(); ?></p> <?php endwhile; get_footer(); ?> |
Now, in the admin area, go to Pages, edit the page WP Blog Reviews and select the template of wpblog, as shown in the screencap below:
Now, view the page. You can see the post of your custom post type.
Conclusion
This is a relatively simple plugin. You can extend it so much further by including several predefined templates. However, remember that creating a plugin is no easy task. You should go through careful planning before even starting the WordPress plugin development process. Proper planning will ensure that the development process goes through without problems and you create a WordPress without many hurdles.
Create Faster WordPress Websites!
Free eBook on WordPress Performance right in your inbox.