To Create WordPress Plugin you need to know how basic php , css and html works.
Here is some function which ussually i used for wordpress plugin.
PREFIX
It’s suggested to prefix your function and variable. For example using mfp (My First Plugin). Here is the example $mfp_string , mfp_function.
DIR and URL
You can detect directory and url WordPress Plugin using function plugin_dir_path( FILE ) and plugin_dir_url( FILE ).
I ussually use constant some constanta like this :
define( 'MFP_DIR' , plugin_dir_path( __FILE__ ) );
define( 'MFP_URL' , plugin_dir_url( __FILE__ ) );
HOOK
Hook is function so you don’t need to change the core code. There are 2 hooks : action and filter.
ACTION
Here are some function which you need to know : do_action , add_action and remove_action
DO_ACTION
For example your index.php in theme like this
<?php
do_action( 'mfp_header_before' );
echo '<div>HEADER</div>';
do_action( 'mfp_header_after' );
do_action( 'mfp_content_before' );
echo '<div>CONTENT</div>';
do_action( 'mfp_content_after' );
do_action( 'mfp_footer_before' );
echo '<div>FOOTER</div>';
do_action( 'mfp_footer_after' );
?>
ADD_ACTION
You can add action in the plugin without change index.php in the theme. For example :
<?php
function mfp_example_header_before1() {
echo '<div>HEADER BEFORE 1</div>';
}
add_action( 'mfp_header_before','mfp_example_header_before1' );
function mfp_example_header_before2() {
echo '<div>HEADER BEFORE 2</div>';
}
add_action( 'mfp_header_before','mfp_example_header_before2' );
function mfp_example_footer_before1() {
echo '<div>FOOTER BEFORE 1</div>';
}
add_action( 'mfp_footer_before','mfp_example_footer_before1' );
?>
REMOVE_ACTION
If you want to remove just use for example remove_action( ‘mfp_header_before’,’mfp_example_header_before2′ ); or if you want to remove all action remove_all_actions( ‘mfp_header_before’ );
WORDPRESS ACTION STANDAR WHICH YOU NEED TO KNOW : wp_head , wp_footer
FILTER
Filter is hook for variable. Function which you need to know apply_filters and add_filter.
Example :
<?php
function mfp_do_filter( $mfp_text ) {
$mfp_text = "VAR DO FILTER";
return $mfp_text;
}
add_filter( 'mfp_var_filter', 'mfp_do_filter' );
function mfp_apply_filter() {
$mfp_var = "VAR APPLY FILTER";
$mfp_var = apply_filters( 'mfp_var_filter' , $mfp_var );
echo $mfp_var;
}
mfp_apply_filter();
?>
REMOVE_FILTER
If you want to remove filter. Just use for example remove_filter( ‘mfp_var_filter’ , ‘mfp_do_filter’ ); or if you want ro remove all just use function remove_all_filter( ‘mfp_var_filter’ );
WORDPRESS FILTER WHICH YOU NEED TO KNOW : the_content
After you know some basic functions now it’s time to create your first plugin.
HERE IS SAMPLE FIRST PLUGIN TO INSERT BEFORE AND AFTER CONTENT IN THE POST / PAGE
<?php
/*
Plugin Name: My First Plugin
Plugin URI: http://wpamanuke.com/how-to-create-wordpress-plugin/
Description: First plugin which i made
Version: 1.0
Author: WPAmanuke
Author URI: http://wpamanuke.com
License: GPLv2
*/
function mfp_filter_the_content_before( $content ) {
$custom_content = '<div>Before Content Filter</div>';
$custom_content .= $content;
return $custom_content;
}
add_filter( 'the_content', 'mfp_filter_the_content_before' );
function mfp_filter_the_content_after( $content ) {
$custom_content = '<div>After Content Filter</div>';
$custom_content = $content . $custom_content;
return $custom_content;
}
add_filter( 'the_content', 'mfp_filter_the_content_after' );
?>