What is plugin
Plugins are block of code that will add functionality to existing functionality in WordPress website, and that will not part of any WordPress setup code or any installed theme so if any change made to any theme it will not affect to plugin code. so let’s move to steps that will help you to learn how to create your own plugin in you website.
To know more about what is plugin you can review WordPress codex document here
In this example we will add one short code that will show some custom information where we use it.
Step1: Create directory for plugin
As first Step you will need to Create a new folder for your plugin inside project_directory/wp-content/plugins directory. and you should give name of directory based on functionality of plugin so we will name it gvtech_message
Step2: Add Code files
Create the main PHP file for your plugin with same name as we set in plugin folder gvtech_message . or we can name it index.php also
Step3: Add code to Initial file
Now we have added file but it will need to inform our WordPress setup that this is plugin in-order to make it working. so let’s add information of plugin in our main PHP file. Open file we have created in step 2 and add below content,
<?php /* Plugin Name: gvtech_message Plugin URI: <plugin URI> Description: <plugin description> Version: <plugin version>1.0.0 Author: <author> Author URI: <author URI>https://www.gvtechnolab.in License: GPL2 License URI: https://www.gnu.org/licenses/gpl-2.0.html Text Domain: wpb-tutorial Domain Path: /languages */ ?>
by adding this commented code at top of our plugins main file it will considers comments as header information about plugin. To know more about header information you can go to WordPress codex website (click Here).
Step4: See plugin in admin
now open or Login to WordPress backend admin and go to plugins menu > installed plugins
here you will see our new created plugin ready to install. but still it will do nothing as we have not added any code in our plugin.
Step5: Add Shortcode code to plugin
add_shortcode( 'gvtech_message', 'gvtech_message_content_func' ); function gvtech_message_content_func( $atts ) { $atts = shortcode_atts( array( 'message' => '', 'author' => 'default' ), $atts); return '<div class="gvtech_message_wrap"><p>'.$atts['message'].'</p><span><em>'.$atts['author'].'</em></span></div>'; }
Step6: Use shortcode
Simply add short code to any page or post like bellow and it will show message with author name and also we can design it by adding styling to this particular class and elements.
[gvtech_message message="message content here" author="writed by"]
That’s it now you need to add your custom functionality code in your plugin files. and enjoy coding. we will here to provide you more content and will get back soon with another topic for you
Here you can find file on GitHub : https://github.com/gvgohil232/gvtech_message.git
thanks 🙂