• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
seegatesite header

Seegatesite.com

Seegatesite.com - Programming Tutorial , Sharing , How and Learn Together

  • TOOLS
    • Bootstrap Navbar Online Generator
    • Customize Sidebar Menu Bootstrap 3
    • Bootstrap Demo
  • ADVERTISE
  • CATEGORIES
    • Android
    • Blogging Tips
    • Database
    • CSS
    • Info Gadget
    • Javascript
    • Linux
    • PHP
    • Various
    • WordPress
  • Q&A
  • PHP
  • JAVASCRIPT
  • JQUERY
  • ANGULAR
  • WORDPRESS
  • SEO
  • REACT
🏠 » Wordpress » How to Create a WordPress Plugin for Placing Ads Code On the Post

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

By Sigit Prasetya Nugroho ∙ November 30, 2014 ∙ Wordpress ∙ Leave a Comment

Share : TwitterFacebookTelegramWhatsapp

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/*
Plugin Name: Advertising Inside Post by seegatesite
Plugin URI: https://seegatesite.com/
Description: Creating space Advertising Inside Post
Version: 1.0
Author: Seegatesite
Author URI: https://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

1
2
3
4
5
6
7
8
/*
Plugin Name: Advertising Inside Post by seegatesite
Plugin URI: https://seegatesite.com/
Description: Creating space Advertising Inside Post
Version: 1.0
Author: Seegatesite
Author URI: https://seegatesite.com
*/

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

1
2
3
4
5
function ads_before_setting()
{
..............................
                ..............................
}

3. To show the ads settings menu, use

1
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

1
add_filter( 'the_content', 'adscontent' );

5. Create adscontent function to show your ad

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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)&lt;&gt;'')  // to showing ad under title post
{
$ads_up =  '&lt;div style="float:'.$locabove.'; margin:10px;"&gt;'.cleanstring($adsup).'&lt;/div&gt;';
$custom_content = $ads_up.$content;
}
if(trim($adscenter)&lt;&gt;'') // to showing ad in the middle post
{
$where = 3; // location ad will showed after paragraph 3
$arraycontent = explode("&lt;/p&gt;", $custom_content);
$newcontet='';
for ($i = 0; $i &lt; count($arraycontent); $i++)
{
if ($i == $where)
{
$newcontet .='&lt;div align="center" style=" margin:10px;"&gt;'.cleanstring($adscenter).'&lt;/div&gt;';
}
$newcontet .= $arraycontent[$i] . "&lt;/p&gt;";
}
$custom_content = $newcontet;
}
if(trim($adsbottom)&lt;&gt;'') // location ad in the bottom post
{
$ads_bott =  '&lt;div style="float:'.$locbottom.';margin:10px;"&gt;'.cleanstring($adsbottom).'&lt;/div&gt;';
$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.

Another WordPress Related Post :

  • How To Create The Fastest Social Share Button WordPress Without Plugin
  • Create WordPress Slider Without Javascript
  • The Point Responsive WordPress Theme For Blogging
  • The Doctors Free WordPress Responsive Theme For Medical
  • Develop Your Site With Start Blogging The Responsive WordPress Theme
  • Zerius Theme The Free WordPress Responsive Theme

Avatar for Sigit Prasetya Nugroho

About Sigit Prasetya Nugroho

This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Welcome to my Home,

Avatar for Sigit Prasetya NugrohoThis site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.



Popular Articles

Checked checkbox AdminLTE Bootstrap in Jquery

November 4, 2014 By Sigit Prasetya Nugroho 7 Comments

Simple create date format validation with jqueryUI

December 21, 2014 By Sigit Prasetya Nugroho Leave a Comment

Create Simple Progress Bar for Fake Online Generator with Jquery

January 10, 2015 By Sigit Prasetya Nugroho Leave a Comment

22+ Coolest Free Jquery Plugin For Premium Theme

October 3, 2015 By Sigit Prasetya Nugroho Leave a Comment

Easy Build Your Anti Copy Paste Plugin

October 6, 2015 By Sigit Prasetya Nugroho Leave a Comment

Popular Tags

adminlte (15) adsense (13) adsense tips (4) affiliate amazon (13) amazon (12) Android (8) angular (16) angular 4 (12) angular 5 (4) asin grabber (3) Bootstrap (27) codeigniter (5) create wordpress theme (5) crud (8) css (6) free wordpress theme (7) google adsense (4) imacros (4) increase traffic (6) jquery (34) laravel (10) laravel 5 (5) learn android (5) lumen api (4) modal dialog (5) mysql (6) nodeJs (4) optimize seo (4) pdo (6) php (30) plugin (53) pos (8) Publisher Tips (5) react (6) Reactjs (9) SEO (37) theme (17) tutorial angular (5) tutorial angular 4 (6) tutorial javascript (10) tutorial javascript beginners (4) twitter (3) wordpress (18) wordpress plugin (13) XMLRPC (5)




  • About
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

©2022 Seegatesite.com