How to Create A WordPress Plugin

Creating a plugin for WordPress is easy. It will only become harder if you want to make something complicated. Adding content to the article body, and adding some scripts or styles are some easy cases.

A plugin is a folder that contains a PHP file at the bare minimum. It will be placed under wp-content/plugins/ for WordPress to recognize it.

Writing the Plugin

Firstly, create a folder called first_plugin under wp-content/plugins/.

Secondly, create first_plugin.php in the created folder. Then add the necessary code to your main plugin file. Start with the header section, which contains the plugin’s name, description, author, version, and other details.

The file’s content:

<?php
/*
Plugin Name: First Plugin
Plugin URI: https://www.www.tldevtech.com/
Description: Just our test
Author: TL Dev Tech
Version: 1.0
Author URI: https://www.www.tldevtech.com/
*/

You will also need to define the plugin’s activation and deactivation hooks, which are functions that are called when the plugin is activated or deactivated.

Once you have defined the hooks, you can start adding the functionality of your plugin. Be sure to use proper coding practices, such as commenting on your code and using descriptive variable and function names.

You can try adding a feature like adding content to the article’s body.

An example of a plugin that adds a shortcode that can display recent posts based on categories and tags.

<?php
/*
Plugin Name: Recent Posts Shortcode
Description: Display recent posts via shortcode.
Version: 1.0
Author: Your Name
*/

function recent_posts_shortcode($atts) {
    $atts = shortcode_atts( [
        'count' => 5,
        'category' => '',
        'tag' => '',
        'post_type' => 'post',
        'orderby' => 'date',
        'order' => 'DESC'
    ], $atts );

    $query_args = [
        'post_type' => $atts['post_type'],
        'posts_per_page' => $atts['count'],
        'category_name' => $atts['category'],
        'tag' => $atts['tag'],
        'orderby' => $atts['orderby'],
        'order' => $atts['order']
    ];

    $recent_posts = new WP_Query($query_args);

    if ($recent_posts->have_posts()) {
        $output = '<ul>';

        while ($recent_posts->have_posts()) {
            $recent_posts->the_post();
            $output .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        }

        $output .= '</ul>';
    } else {
        $output = '<p>No recent posts found.</p>';
    }

    wp_reset_postdata();

    return $output;
}
add_shortcode('recent_posts', 'recent_posts_shortcode');

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close