How to create custom plugin in wordpress?

In this article we will teach you how to create custom plugin in wordpress.

Plugin is a collection of code or function containing a list of files and folders that can be added to wordpress website to implement specific feature or functionality. WordPress Plugins is written in PHP Programming Language and can be integrate to any limit with wordpress.

Why we need a plugin?

Some times in our website we needs some special functionality like social media sharing, follow us buttons, subscription form, ecommerce shop, contact form, booking form, recent posts, Search Engine Optimization, Website Optimizations, Social Media Campaigns, custom menus or customized look of our website. And For this we adds a plugin to our website. Plugin can be free or paid. We can find number of plugins on WordPress.org website.

If you want to create custom plugin in wordpress for your website. Then follow the steps given below.

To create a plugin create a folder my-custom-plugin in plugins folder located at website_root_path/wp-content/plugins/ and create a file my-custom-plugin.php under folder my-custom-plugin and paste the code given below.

<?php
   /*
   Plugin Name: My Custom Plugin
   Plugin URI: https://example.com
   description: This is our custom plugin
   Version: 1.0
   Author: Mr. Chetan Rohilla
   Author URI: https://readymadecode.com
   License: Free
   */
?>

And go to your WordPress Dashboard’s Plugin Page and activate your plugin.

Now Lets say we want to create a plugin to show Google Ads in different places of our website.

Then paste the code given below in your my-custom-plugin.php file created above.

// function that runs when shortcode is called
function mcp_google_ads_shortcode() { 

// Advertisement code pasted inside a variable
	$string .= '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
	<ins class="adsbygoogle"
	style="display:block; text-align:center;"
	data-ad-format="fluid"
	data-ad-layout="in-article"
	data-ad-client="ca-pub-0123456789101112"
	data-ad-slot="9876543210"></ins>
	<script>
	(adsbygoogle = window.adsbygoogle || []).push({});
	</script>';
	
// Ad code returned
	return $string; 

} 
// register shortcode
add_shortcode('mcp_googelads', 'mcp_google_ads_shortcode');

Now we have a shortcode mcp_googelads and we can paste it where we want to show our google ad. Similarly you can add any code to your plugin file my-custom-plugin.php. Here we have create some articles so try these articles to create your own custom plugin with some functionality. And also from these articles move your code from functions.php file to your own plugin file my-custom-plugin.php.

Create custom meta boxes in wordpress

Custom taxonomies in wordpress without plugin

Create custom post type in wordpress without plugin

Add Menu in Footer WordPress

Create sidebar in wordpress without plugin

Ways to use shortcode:

You can use the shortcode in two ways:

Method 1 : Paste the shortcode in your posts, pages, sidebars like this [mcp_googleads].

Method 2 : Paste the shortcode in your theme files header.php, footer.php, page.php, single.php, archive.php like this

<?php echo do_shortcode("[mcp_googleads]"); ?>

That’s it Now you can create your own wordpress plugin with custom features and also you can distribute or share this plugin by making the zip of your plugin’s folder.

If you want more tutorials and tricks about wordpress then visit our WordPress Page and follow us on facebooktwittertumblrlinkdedin and if you like this article then share this.

Comments are closed.