How to Create a WordPress Plugin for Placing Ads Code On the Post

How to place an ads in wordpress posts without changing the theme. By creating a wordpress plugin, we can place ads on wordpress posts correspond to our desire.

How to Create a WordPress plugin for placing ad code on the post

In this tutorial I will share create simple wordpress plugin for placing ad code on the post.

Ad script can be placed in three different locations
– Under the title of the post
– In the middle of posts
– At the end of the posting

For example, I use wordpress theme RTPANEL as an implementation to create advertising script location plugin

Let’s try to create this plugin.
1. Create a folder called AIP
2. Create a php file with the name AIP.php
3. Copy the script below and paste it on AIP.php

<?php
/*
Plugin Name: Advertising Inside Post by seegatesite
Plugin URI: http://seegatesite.com/
Description: Creating space Advertising Inside Post
Version: 1.0
Author: Seegatesite
Author URI: http://seegatesite.com
*/
function adscontent( $content ) {
    if ( is_single() )
	{
    	$adsup=get_option( 'sgt_ads_up' );
		$adscenter=get_option( 'sgt_ads_center' );
		$adsbottom=get_option( 'sgt_ads_bottom' );
		$locbottom =get_option('sgt_loc_above');
		$locabove = get_option('sgt_loc_bottom');
		$custom_content = $content;
		if(trim($adsup)<>'')
		{
			$ads_up =  '<div style="float:'.$locabove.'; margin:10px;">'.cleanstring($adsup).'</div>';
			$custom_content = $ads_up.$content;
		}
		if(trim($adscenter)<>'')
		{
			$where = 3;
			$arraycontent = explode("</p>", $custom_content);
			$newcontet='';
			for ($i = 0; $i < count($arraycontent); $i++)
			{
				if ($i == $where)
				{
					$newcontet .='<div align="center" style=" margin:10px;">'.cleanstring($adscenter).'</div>';
				}
				$newcontet .= $arraycontent[$i] . "</p>";
			}
			$custom_content = $newcontet;
		}
		if(trim($adsbottom)<>'')
		{
			$ads_bott =  '<div style="float:'.$locbottom.';margin:10px;">'.cleanstring($adsbottom).'</div>';
			$custom_content = $custom_content.$ads_bott;
		}
        return $custom_content;
    } else {
        return $content;
    }
}
function cleanstring($adsup)
{
	return str_replace('\"','"',$adsup);
}
function ads_before_setting(){
		if ( isset ($_POST['submit']) )
			{
				$adsup = cleanstring($_POST['txtup']);
				$adscenter = cleanstring($_POST['txtcenter']);
				$adsbottom = cleanstring($_POST['txtbottom']);
				$locbottom = $_POST['locbottom'];
				$locabove = $_POST['locabove'];
				update_option( 'sgt_ads_up', $adsup  );
				update_option( 'sgt_ads_center', $adscenter );
				update_option( 'sgt_ads_bottom', $adsbottom );
				update_option( 'sgt_loc_above', $locabove );
				update_option( 'sgt_loc_bottom', $locbottom );
				echo "<h3>Update succesfull</h3>";
			}
			$adsup=get_option( 'sgt_ads_up' );
			$adscenter=get_option( 'sgt_ads_center' );
			$adsbottom=get_option( 'sgt_ads_bottom' );
			$locbottom =get_option('sgt_loc_above');
			$locabove = get_option('sgt_loc_bottom');
			echo '<hr/><div class="wrap">'.PHP_EOL;
			echo '<h2>Advertising Inside Post Settings</h2>'.PHP_EOL;
			echo '<form action="" method="post"><table>';
			echo '<tr><td align="left" colspan="2"><h3>Ad above post</h3></td></tr>';
			echo '<tr><td>Description</td><td> : Ads are placed under the title of the post</td></tr>';
			echo '<tr><td>Script</td><td> : <textarea rows="4" cols="50" name="txtup">'.cleanstring($adsup).'</textarea></td></tr>';
			echo '<tr><td>Location</td><td> : <select name="locabove"><option value="left">Left</option><option value="right">Right</option></select></td></tr>';
			echo '<tr><td align="left" colspan="2"><hr/></td></tr>';
			echo '<tr><td align="left" colspan="2"><h3>Ad middle post</h3></td></tr>';
			echo '<tr><td>Description</td><td> : Ads are placed in the middle of the content post</td></tr>';
			echo '<tr><td>Script</td><td> : <textarea rows="4" cols="50" name="txtcenter" >'.cleanstring($adscenter).'</textarea></td></tr>';
			echo '<tr><td align="left" colspan="2"><hr/></td></tr>';
			echo '<tr><td align="left" colspan="2"><h3>Ad bottom post</h3></td></tr>';
			echo '<tr><td>Description</td><td> : Ads are placed in the bottom of the content post</td></tr>';
			echo '<tr><td>Script</td><td> : <textarea rows="4" cols="50" name="txtbottom">'.cleanstring($adsbottom).'</textarea></td></tr>';
			echo '<tr><td>Location</td><td> : <select name="locbottom"><option value="left">Left</option><option value="right">Right</option></select></td></tr>';
			echo '<tr><td colspan="2"><hr/><input type="submit" name="submit" value="Update setting"/></td></tr>';
			echo '</table></form>';

	}
function register_my_custom_menu_page_aip(){
    add_menu_page( 'Ads Inside Post', 'Ads InInside Post', 'manage_options', 'ads_before_setting', 'ads_before_setting', '', 6 );
}

add_filter( 'the_content', 'adscontent' );
add_action( 'admin_menu', 'register_my_custom_menu_page_aip' );
?>

Explanation
1. Create plugin header

/*
Plugin Name: Advertising Inside Post by seegatesite
Plugin URI: http://seegatesite.com/
Description: Creating space Advertising Inside Post
Version: 1.0
Author: Seegatesite
Author URI: http://seegatesite.com
*/

2. Create setting function for save your ads script to wordpress database

function ads_before_setting()
{
		..............................
                ..............................
}

3. To show the ads settings menu, use

add_action( 'admin_menu', 'register_my_custom_menu_page_aip' );

to call register_my_custom_menu_page_aip function
4. The ad script will show in your content with

add_filter( 'the_content', 'adscontent' );

5. Create adscontent function to show your ad

function adscontent( $content ) {
    if ( is_single() ) 
	{
    	$adsup=get_option( 'sgt_ads_up' );
		$adscenter=get_option( 'sgt_ads_center' );
		$adsbottom=get_option( 'sgt_ads_bottom' );
		$locbottom =get_option('sgt_loc_above');
		$locabove = get_option('sgt_loc_bottom');
		$custom_content = $content;
		if(trim($adsup)<>'')  // to showing ad under title post
		{
			$ads_up =  '<div style="float:'.$locabove.'; margin:10px;">'.cleanstring($adsup).'</div>';
			$custom_content = $ads_up.$content;
		}
		if(trim($adscenter)<>'') // to showing ad in the middle post
		{
			$where = 3; // location ad will showed after paragraph 3
			$arraycontent = explode("</p>", $custom_content);
			$newcontet='';
			for ($i = 0; $i < count($arraycontent); $i++) 
			{
				if ($i == $where) 
				{ 
					$newcontet .='<div align="center" style=" margin:10px;">'.cleanstring($adscenter).'</div>';
				}
				$newcontet .= $arraycontent[$i] . "</p>";
			}
			$custom_content = $newcontet;
		}
		if(trim($adsbottom)<>'') // location ad in the bottom post
		{
			$ads_bott =  '<div style="float:'.$locbottom.';margin:10px;">'.cleanstring($adsbottom).'</div>';
			$custom_content = $custom_content.$ads_bott;
		}
        return $custom_content;
    } else {
        return $content;
    }
}

Similarly, how to create a plugin to put your ad code in wordpress post, you can download my plugin here.

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

%d bloggers like this: