How to create a simple WordPress plugin – Learn WordPress from a to z

Tutorials 0 lượt xem

WordPress is a free, open source content management system (CMS). And the best part of WordPress is that it’s pretty extensible. Thanks to plugins, you can extend more functionality than the original. Plugins allow you to add additional functions like SEO, eCommerce, security, etc.

How to create a simple WordPress plugin

And today, I want to show you how to create a simple WordPress plugin. If you develop a plugin of your own, let’s get started!

Basic knowledge

Before you start, make sure you have some knowledge of PHP. This will help a lot if you plan to write more plugins in the future.

In addition, you also need to have more knowledge of CSS and JS. Plus you need to be familiar with WordPress programming standards .

Rest assured, it’s a long road. We don’t get into that complicated stuff today. I’ll show you how to write a simple plugin and share some tools and resources that will make your job easy.

How to write a basic plugin

Every WordPress plugin has key files that you have to create manually. However, to save time, I will guide you to create a plugin directory automatically using Pluginplate.

Create WordPress Plugins with Pluginplate

First, go to the Pluginplate , click on the “Create Your Plugin” button.

Next, fill in your plugin information as shown below. Towards the bottom of the page, you will see a “Modules” section that allows you to add additional features to your plugin. Then press the “Generate Plugin” button.

Then click the “Download” button to download it to your computer.

Now, we have all the basic files, including the main file. The next thing is that we have to add code to execute the functions when the plugin is activated. Based on my example, the main file of my plugin is .php . This is the file that I will edit in the next part.

Add functionality to the plugin

First, unzip the folder you downloaded to your computer from the Pluginplate. Inside the folder you will see the main file, the main file of your plugin is .php .

Inside the plugin folder you can see a bunch of other files but we don’t need those at the moment.

Now I will open the file .php up.

<?php /**
 * hocwordpress
 *
 * @package       HOCWORDPRE
 * @author        Huy Kira
 * @license       gplv2
 * @version       1.0.0
 *
 * @wordpress-plugin
 * Plugin Name:   hocwordpress
 * Plugin URI:    https://hocwordpress.vn/
 * Description:   This is some demo short description...
 * Version:       1.0.0
 * Author:        Huy Kira
 * Author URI:    huykira@gmail.com
 * Text Domain:   hocwordpress
 * Domain Path:   /languages
 * License:       GPLv2
 * License URI:   https://www.gnu.org/licenses/gpl-2.0.html
 *
 * You should have received a copy of the GNU General Public License
 * along with hocwordpress. If not, see .
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * HELPER COMMENT START
 * 
 * This file contains the main information about the plugin.
 * It is used to register all components necessary to run the plugin.
 * 
 * The comment above contains all information about the plugin 
 * that are used by WordPress to differenciate the plugin and register it properly.
 * It also contains further PHPDocs parameter for a better documentation
 * 
 * The function HOCWORDPRE() is the main function that you will be able to 
 * use throughout your plugin to extend the logic. Further information
 * about that is available within the sub classes.
 * 
 * HELPER COMMENT END
 */

// Plugin name
define( 'HOCWORDPRE_NAME', 'hocwordpress' );

// Plugin version
define( 'HOCWORDPRE_VERSION', '1.0.0' );

// Plugin Root File
define( 'HOCWORDPRE_PLUGIN_FILE', __FILE__ );

// Plugin base
define( 'HOCWORDPRE_PLUGIN_BASE', plugin_basename( HOCWORDPRE_PLUGIN_FILE ) );

// Plugin Folder Path
define( 'HOCWORDPRE_PLUGIN_DIR', plugin_dir_path( HOCWORDPRE_PLUGIN_FILE ) );

// Plugin Folder URL
define( 'HOCWORDPRE_PLUGIN_URL', plugin_dir_url( HOCWORDPRE_PLUGIN_FILE ) );

/**
 * Load the main class for the core functionality
 */
require_once HOCWORDPRE_PLUGIN_DIR . 'core/class-hocwordpress.php';

/**
 * The main function to load the only instance
 * of our master class.
 *
 * @author  Huy Kira
 * @since   1.0.0
 * @return  object|Hocwordpress
 */
function HOCWORDPRE() {
	return Hocwordpress::instance();
}

HOCWORDPRE();
<div><span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span><span>19</span><span>20</span><span>21</span><span>22</span><span>23</span><span>24</span><span>25</span><span>26</span><span>27</span><span>28</span><span>29</span><span>30</span><span>31</span><span>32</span><span>33</span><span>34</span><span>35</span><span>36</span><span>37</span><span>38</span><span>39</span><span>40</span><span>41</span><span>42</span><span>43</span><span>44</span><span>45</span><span>46</span><span>47</span><span>48</span><span>49</span><span>50</span><span>51</span><span>52</span><span>53</span><span>54</span><span>55</span><span>56</span><span>57</span><span>58</span><span>59</span><span>60</span><span>61</span><span>62</span><span>63</span><span>64</span><span>65</span><span>66</span><span>67</span><span>68</span><span>69</span><span>70</span><span>71</span><span>72</span><span>73</span><span>74</span><span>75</span><span>76</span><span>77</span><span>78</span><span>79</span><span>80</span><span>81</span>

The above code will tell WordPress the name of the plugin as well as its version, author, license, and other details.

Just below the above code, or add the following code:

// Register Custom Post Type
function custom_post_type() {

	$labels = array(
		'name'                  =&gt; _x( 'Khoá học', 'Post Type General Name', 'hocwordpress' ),
		'singular_name'         =&gt; _x( 'Khoá học', 'Post Type Singular Name', 'hocwordpress' ),
		'menu_name'             =&gt; __( 'Khoá học', 'hocwordpress' ),
		'name_admin_bar'        =&gt; __( 'Khoá học', 'hocwordpress' ),
		'archives'              =&gt; __( 'Item Archives', 'hocwordpress' ),
		'attributes'            =&gt; __( 'Item Attributes', 'hocwordpress' ),
		'parent_item_colon'     =&gt; __( 'Parent Item:', 'hocwordpress' ),
		'all_items'             =&gt; __( 'Tất cả khoá học', 'hocwordpress' ),
		'add_new_item'          =&gt; __( 'Thêm khoá học', 'hocwordpress' ),
		'add_new'               =&gt; __( 'Add New', 'hocwordpress' ),
		'new_item'              =&gt; __( 'New Item', 'hocwordpress' ),
		'edit_item'             =&gt; __( 'Edit Item', 'hocwordpress' ),
		'update_item'           =&gt; __( 'Update Item', 'hocwordpress' ),
		'view_item'             =&gt; __( 'View Item', 'hocwordpress' ),
		'view_items'            =&gt; __( 'View Items', 'hocwordpress' ),
		'search_items'          =&gt; __( 'Search Item', 'hocwordpress' ),
		'not_found'             =&gt; __( 'Not found', 'hocwordpress' ),
		'not_found_in_trash'    =&gt; __( 'Not found in Trash', 'hocwordpress' ),
		'featured_image'        =&gt; __( 'Featured Image', 'hocwordpress' ),
		'set_featured_image'    =&gt; __( 'Set featured image', 'hocwordpress' ),
		'remove_featured_image' =&gt; __( 'Remove featured image', 'hocwordpress' ),
		'use_featured_image'    =&gt; __( 'Use as featured image', 'hocwordpress' ),
		'insert_into_item'      =&gt; __( 'Insert into item', 'hocwordpress' ),
		'uploaded_to_this_item' =&gt; __( 'Uploaded to this item', 'hocwordpress' ),
		'items_list'            =&gt; __( 'Items list', 'hocwordpress' ),
		'items_list_navigation' =&gt; __( 'Items list navigation', 'hocwordpress' ),
		'filter_items_list'     =&gt; __( 'Filter items list', 'hocwordpress' ),
	);
	$args = array(
		'label'                 =&gt; __( 'Khoá học', 'hocwordpress' ),
		'description'           =&gt; __( 'Quản lý khoá học', 'hocwordpress' ),
		'labels'                =&gt; $labels,
		'supports'              =&gt; array( 'title', 'editor', 'thumbnail', 'comments', 'revisions' ),
		'hierarchical'          =&gt; false,
		'public'                =&gt; true,
		'show_ui'               =&gt; true,
		'show_in_menu'          =&gt; true,
		'menu_position'         =&gt; 5,
		'menu_icon'             =&gt; 'dashicons-welcome-learn-more',
		'show_in_admin_bar'     =&gt; true,
		'show_in_nav_menus'     =&gt; true,
		'can_export'            =&gt; true,
		'has_archive'           =&gt; true,
		'exclude_from_search'   =&gt; false,
		'publicly_queryable'    =&gt; true,
		'capability_type'       =&gt; 'page',
	);
	register_post_type( 'khoa_hoc', $args );

}
add_action( 'init', 'custom_post_type', 0 );
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span><span>19</span><span>20</span><span>21</span><span>22</span><span>23</span><span>24</span><span>25</span><span>26</span><span>27</span><span>28</span><span>29</span><span>30</span><span>31</span><span>32</span><span>33</span><span>34</span><span>35</span><span>36</span><span>37</span><span>38</span><span>39</span><span>40</span><span>41</span><span>42</span><span>43</span><span>44</span><span>45</span><span>46</span><span>47</span><span>48</span><span>49</span><span>50</span><span>51</span><span>52</span><span>53</span><span>54</span><span>55</span>
</div>

The above code simply creates a custom post type named “Course”. And this post type will support features like quotes, avatars, comments, custom fields, etc. These are the features you will see inside the post editor when adding a new course.

Install the plugin on your WordPress site

Save all your changes. Then compress your plugin folder into a .ZIP file. Next go to your WordPress site and install it like any other plugin.

Now, if you check the WordPress admin menu, you will see that the new post type is “Course”.

Congratulations on successfully creating your first WordPress plugin! Alternatively, you can study the code of other plugins yourself (all WordPress plugins are open source).

Also, read the WordPress Handbook Plugin documentation. This document will contain all you need regarding creating a WordPress plugin.

Epilogue

Programming WordPress plugins can seem intimidating at first, especially as a beginner. But with the right tools and some learning resources, you can develop plugins in no time. With just basic programming knowledge and determination, you’re halfway there.

I hope this article will serve as a stepping stone for you to develop more complex WordPress plugins in the future. Take a look at the resources I recommended above to improve your knowledge of WordPress plugin development.

If you find it interesting, you can follow the  WordPress Tips section  and follow the fanpage to know more new knowledge.

Bài viết liên quan