How to create a custom plugin - WPTRINITY

How to create a custom plugin

To create a plugin, the First you need to do is create a folder and then create a single file with one line of content. Navigate to the wp-content/plugins folder, and create a new folder named wptrinityplugin. Inside this new folder, create a file named wptrinityplugin.php. Open the file in a text editor, and paste the following information in it:

<?php    
/*    
Plugin Name:  Wpekart Plugin   
Plugin URI: https://wptrinity.com    
description: >-   a plugin to create  Wpekart    
Version: 1.2    
Author: Mr. Robin
Author URI: http:// wptrinity.com    
License: GPL2   
 */ ?> 

all this information above, only the plugin’s name is required. But if you want, you should add as much data as possible.

After that, you can go into the back end to activate your plugin. This plugin doesn’t do anything; but it is an active, functioning plugin.

Structuring Plugins

Structuring Plugins is very important. When you creating complex functionality, splitting your plugin into multiple files and folders might be easier.

For good practice always focus your plugin have one main class, that class in the main plugin file, and add one or more separate files for other functionality. If your plugin enhances WordPress’ back end with custom controls, you can create the usual CSS and JavaScript folders to store the appropriate files.

its aims for a balance between layout structure, usability, and minimalism. Split your plugin into multiple files as necessary, but don’t go overboard. I find it useful to look at the structure of popular plugins such as WP-PageNavi and Akismet.

Great hooks to help

WordPress offers three great hooks to help you take care of this:

The net result of all our effort is that a user should be able to install your plugin, use it for 10 years and then delete it without leaving a trace on the website, in the database or in the file structure.

Create a New Plugin File

To start putting together a new plugin, you will need access to your site’s directory. you’re in, you’ll need to navigate to the folder that contains your plugins, which is located at /wp-content/plugins/.

To add your new plugin, you’ll need to create a new folder inside this directory. Do so now, and give it any unique name you’d like. We’ll call ours wptrinityplugin.

This folder is where everything related to your plugin will ‘live’ on your site. Since this plugin is going to be very simple, it only needs to contain a single file, which you’ll create now. This will be a PHP file, which will contain the plugin’s code.

As such, open the new wptrinityplugin directory and right-click inside it to select Create new file.
You’ll be asked to name the file. This should be identical to the folder, so call it wptrinityplugin.php (make sure to use the right file extension). This file will now be added to the folder.

The file is empty now so paste the following text into it:

<?php
/**
* Plugin Name: wptrinity Plugin
* Plugin URI: https://www.wptrinity.com/
* Description: This is the very first plugin I ever created.
* Version: 1.0
* Author: Your Name Here
* Author URI: http://wekart.com/
**/

When that’s done, you can actually see the plugin in your site’s admin dashboard. Log in now and take a look in your plugin library

You can even go ahead and Activate your plugin now in the admin panel. Of course, the plugin doesn’t actually do anything yet. That’s because we haven’t added any functionality to it.

Add Code to Your Plugin

This plugin is going to change the text that appears when you insert a Read More tag. To see what this looks like normally, create a new blog post and click on the Insert Read More tag icon.

If you publish the post and check it out on the front end of your site, you will see that the tag has been replaced with a link that says “Continue reading”:

This is the text that we want to change. To do that, open up your very-first-plugin.php file again. Then, add a new line at the very end of the file, and paste in the following code:

function wp_modify_read_more_link() {

    return '<a class="more-link" href="' . get_permalink() . '">Click to Read!</a>';

}

add_filter( 'the_content_more_link', 'wp_modify_read_more_link' );

All this function does is get the link for the current post, and then apply the anchor text “Click to Read!”. This is the text that will replace “Continue reading.” Of course, you can feel free to change this text to anything you want to use
The final line in this code uses a filter to hook into a function called the_content_more_link, which represents the Read More link. The filter instructs WordPress to call our new function instead, so the standard link will be replaced with our new version.
At this point, you can save the plugin file. Since you already activated the plugin, its functionality should work right away.  By checking your website to see if the link text has changed.

Leave a Comment

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