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.
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)<>'') // 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.
Leave a Reply