ThemeShaper Forums » Thematic

[closed]

Adding Adsense ad code in a Widget

(10 posts)
  • Started 3 years ago by noumaan
  • Latest reply from noumaan
  • This topic is not resolved
  1. Hi

    I am new to theme development. I am preparing a child theme based on thematic for my website. So far the community members have helped me a lot, thanks to all.

    New issue: It seems like one cannot enter Adsense code in a simple text widget. I searched and found out that people have created little code files (just like plugins) that can be used inside a theme as a widget. I do not want to use an external file.

    I want to know how I can display ads in a text widget in my primary aside sidebar.

    Posted 3 years ago #
  2. I haven't really had to think hard about this yet, but I would maybe try setting it up as a shortcode.

    // adsense shortcodes
    // https://www.google.com/adsense/static/en_US/AdFormats.html ad sizes
    
    function snix_shortcode_adsense() {
        return '<div id="adsense"><script><!--
    	google_ad_client = "pub-3637220125174754";
    	google_ad_slot = "4668915978";
    	google_ad_width = 468;
    	google_ad_height = 60;
    	google_adtest = "on";
    	//-->
    </script>
    
    <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div>';
    }
    
    add_shortcode('adsense', 'snix_shortcode_adsense');
    
    // enable shortcodes in sidebar widgets
    add_filter('widget_text', 'do_shortcode');

    You are going to have to swap out the correct google code for the correct size and obviously change out the ad client, think this came from http://www.wprecipes.com/how-to-embed-adsense-anywhere-on-your-posts and the add_filter for 'do_shortcode' I believe came from middlesister who graces us on the forums with knowledge occasionally.

    Posted 3 years ago #
  3. Oh, forgot to mention the reasoning behind this is you can then just drop " [adsense] " into the widget text and be done. The way it is now is set up at the moment works best for content, so you could actually duplicate it, rename the appropriate sections and have one for content and one for the sidebar.

    Posted 3 years ago #
  4. you could probably build out that shortcode so that it accepts height and width parameters.

    so [adsense height=480 width=468]

    function snix_shortcode_adsense($atts) {
    
      extract(shortcode_atts(array(
    	"height" => '60', //default size
    	"width"=> '468',
        ), $atts));
    
        return '<div id="adsense"><script><!--
    	google_ad_client = "pub-3637220125174754";
    	google_ad_slot = "4668915978";
    	google_ad_width = $width;
    	google_ad_height = $height;
    	google_adtest = "on";
    	//-->
    </script>
    
    <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div>';
    }
    
    add_shortcode('adsense', 'snix_shortcode_adsense');

    tooootally untested, ymmv

    Posted 3 years ago #
  5. Oh, and make sure you don't leave in " google_adtest = "on"; " that is a test thing to get ads to display for testing purposes.

    The height and width is a good idea, at one point I think I actually was going to do something like this, but couldn't find the info on if the ads were adjustable (I would assume not and I didn't really look hard). Might have to think of a way to combine them where you select 3 sizes to use in the php and do [adsense type=sidebar] [adsense type=content] which would also make it easier on the end user. I might have to think about this one a bit now and try to one up you....... :P

    Posted 3 years ago #
  6. you mean like?

    function snix_shortcode_adsense($atts) {
    
      extract(shortcode_atts(array(
    	"type" => ""
        ), $atts));
    
        if ($type=="skyscraper"){
             $height = "480";
             $width = "60";
        } else if ($type=="landscape"){
             $height = "60";
             $width = "480";
        } else {
             $height = "100";  //set your default size
             $width = "100";
        }
        return '<div id="adsense"><script><!--
    	google_ad_client = "pub-3637220125174754";
    	google_ad_slot = "4668915978";
    	google_ad_width = $width;
    	google_ad_height = $height;
    	google_adtest = "on";
    	//-->
    </script>
    
    <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div>';
    }
    
    add_shortcode('adsense', 'snix_shortcode_adsense');

    you'd need to change the if statement to use the default adsense sizes. if you want to use them all it might be better to use a switch case instead of all those IF statements. isn't there a plugin for this? if not, it wouldn't make a bad plugin.

    Posted 3 years ago #
  7. Thank you helgatheviking and Scottnix

    So basically shortcodes are like functions, if I set them right I won't even need an adsense plugin to display the ads. The plugin that I previously used inserted similar shortcodes into posts. I guess if I can do this right I can remove that plugin too. Perfect.

    Just one little question to be extra sure:

    I need to do this in my child theme's functions.php?

    ===

    Another question, I want to tell the theme that if this shortcode has been displayed on a page once, then it should not repeat itself. Like for example I create a shortcode for posts. So that the ad appears inside the post and use it inside all my posts. But it will get repeated on all posts in home page too. So I need to tell the theme that if this short code has been used once then don't use it again and do nothing next time it appears.

    Thank you guys, I can not believe I am learning how to replace plugins :) this is awesome.

    Posted 3 years ago #
  8. I am trying the following code to use a create shortcode to display ads inside posts. What I am trying to do is that the shortcode displays ads on index pages only in the first post.

    Shortcode for adsense_insidepost so that it appears only once. I thought I can do this by assigning a variable in the loop after a post is displayed. $display_ad="noad" and then in the shortcode function I try to check the value of display_ad if it is noad then return this or else return adsense code.

    But apparantely this is not working.

    function snix_shortcode_adsense_insidepost() {
    
    	if ($display_ad == "noad") { 
    
    		return '<!--no ads displayed-->';
    
    	} else { 
    
    		return '<!--testcode--><script type="text/javascript"><!--
    google_ad_client = "ca-pub-************";
    /* inside post large */
    google_ad_slot = "*********";
    google_ad_width = 300;
    google_ad_height = 250;
    google_adtest = "on";
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>';
    
    }
    
    }
    
    add_shortcode('ad_insidepost', 'snix_shortcode_adsense_insidepost');
    
    //Replace Index Loop to Add AD display Variable
    
    // The Index Loop
    
    function childtheme_override_index_loop()  {
    
    		global $options, $blog_id;
    
    		foreach ($options as $value) {
    		    if (get_option( $value['id'] ) === FALSE) {
    		        $$value['id'] = $value['std'];
    		    } else {
    		    	if (THEMATIC_MB)
    		    	{
    		        	$$value['id'] = get_option($blog_id,  $value['id'] );
    		    	}
    		    	else
    		    	{
    		        	$$value['id'] = get_option( $value['id'] );
    		    	}
    		    }
    		}
    
    		/* Count the number of posts so we can insert a widgetized area */ $count = 1;
    		while ( have_posts() ) : the_post();
    
    				thematic_abovepost(); ?>
    
    				<div id="post-<?php the_ID();
    					echo '" ';
    					if (!(THEMATIC_COMPATIBLE_POST_CLASS)) {
    						post_class();
    						echo '>';
    					} else {
    						echo 'class="';
    						thematic_post_class();
    						echo '">';
    					}
         				thematic_postheader(); ?>
    					<div class="entry-content">
    <?php thematic_content(); ?>
    <?php
    $display_ad="noad" ;
    
     ?>
    
    					<?php wp_link_pages('before=<div class="page-link">' .__('Pages:', 'thematic') . '&after=</div>') ?>
    					</div><!-- .entry-content -->
    					<?php thematic_postfooter(); ?>
    				</div><!-- #post -->
    
    			<?php 
    
    				thematic_belowpost();
    
    				comments_template();
    
    				if ($count==$thm_insert_position) {
    						get_sidebar('index-insert');
    				}
    				$count = $count + 1;
    		endwhile;
    	}
    
     // end index_loop
    
    ?>
    Posted 3 years ago #
  9. why don't you just use the counter that already exists?

    in your loop instead of:

    <?php
    $display_ad="noad" ;
    
     ?>

    use something like:

    if($count==1){
      do_shortcode('[snix_shortcode_adsense_insidepost]');
    }

    though i'm starting to think you don't need a shortcode at all. to add it to every "single" post you could hook into the thematic_post filter

    Posted 3 years ago #
  10. Hi helgatheviking

    thank you very much for your advice. I tried the above code but it seems like I need to use something like this

    <?php
    if($count==1){
    thematic_content();
    } else {
    strip_shortcodes($content);
    }
    ?>

    Now this code words but it makes all posts disappear after the first post because I think that thmatic uses thematic_content instead of $content so can you please tell me how I can strip_shortcode thematic_content here?

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.