Learn How To Create a WordPress plugin From Scratch

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:

12345678910111213141516171819202122232425262728293031323334353637383940function 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:

12345678910111213141516171819function 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 activatedregister_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:

12345678910111213<?php/*Template Name: WP Blog*/get_header();query_posts(array(   ‘post_type’ => ‘music_review’)); ?><?phpwhile (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.

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号