0%
Loading ...

Tutorial: Develop WordPress Plug-in in 6 Steps – Add filter method

Images (4)

Here I will demonstrate how to build a wordpress plugin that will add prefix and suffix to each post content.

Step 01:

The name of the plugin that will be created here will be called PreSuff (Prefix / Suffix).
Create a directory for plugin under wordpress-root\wp_content\plugins. The directory name will be presuff.
So under wordpress root directory We will have: \wp_content\plugins\presuff.

Step 02:

Create a file called presuff.php. the content of the file will be:

<?php

/*
Plugin Name: PreSuff plugin
Plugin URI: http://www.flash-jet.com
Description: A plugin demo that adds prefix and suffix to post content.
Version: 1.0
Author: Gabriel Magen
Author URI: http://www.flash-jet.com
*/

?>

Step 03:

Add a filter:
add_filter(‘the_content’, ‘presuff’);

Step 04:

Add a function:
function presuff($content) {
$prefix= <<<PRE

<h3><center>Prefix to add to post<hr>

PRE;
$suffix= <<<SUFF

<h3><center>Suffix to add to post

SUFF;
return $prefix.$content.$suffix;
}

Step 05:

The whole file will look like the following:

<?php

/*
Plugin Name: PreSuff plugin
Plugin URI: http://www.flash-jet.com
Description: A plugin demo that adds prefix and suffix to post content.
Version: 1.0
Author: Gabriel Magen
Author URI: http://www.flash-jet.com
*/
add_filter(‘the_content’, ‘presuff’);

function presuff($content) {
$prefix= <<<PRE

<h3><center>Prefix to add to post<hr>

PRE;
$suffix= <<<SUFF

<h3><center>Suffix to add to post

SUFF;
return $prefix.$content.$suffix;
}
?>

Step 06: 

  • Activate the plugin.
  • Create a Post.
  • Result: the post include HTML content you wrote under $prefix and $suffix.