WordPress.org

Ready to get started?Download WordPress

Forums

php and javascript with google adsense code (2 posts)

  1. thebusysingleparent
    Member
    Posted 3 months ago #

    I have pieced together code from two different tuturials and some forums to be able to put google adsense code inside the post loop for a multi author site.

    The following is a working (I think) code that I placed in my site plugin folder (alternative to functions.php). It combines php and javascript - which took me most of this day to figure out as I've been learning about php and not a clue yet about javascript.

    //Insert ads after second paragraph of single post content.
    
    add_filter( 'the_content', 'prefix_insert_post_ads' );
    
    function prefix_insert_post_ads( $content ) {
    
     if(get_the_author_meta( 'publisher_id' )){
            $input = array(get_option('publisher_id'), get_the_author_meta( 'publisher_id' ));
        }
     else{
            $input = array(get_option('publisher_id'));
        }
        shuffle($input);
    
            $div_open = '<div class="google_ad_in_post">';
            $div_close = '</div>';
            $ad_code = '<script type="text/javascript">
    var input = "<?php echo json_encode($input[0]);?>";
    <!--
        google_ad_client = "ca-",document.write(input);
        google_ad_width = 250;
        google_ad_height = 250;
        //-->
        </script>
        <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>';
    
    	$ad_code = $div_open . $ad_code . $div_close;
    
    	if ( is_singular('post') && ! is_admin() ) {
    		return prefix_insert_after_paragraph( $ad_code, 2, $content );
    	}
    
    	return $content;
    }
    
    // Parent Function that makes the magic happen
    
    function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
    	$closing_p = '</p>';
    	$paragraphs = explode( $closing_p, $content );
    	foreach ($paragraphs as $index => $paragraph) {
    
    		if ( trim( $paragraph ) ) {
    			$paragraphs[$index] .= $closing_p;
    		}
    
    		if ( $paragraph_id == $index + 1 ) {
    			$paragraphs[$index] .= $insertion;
    		}
    	}
    
    	return implode( '', $paragraphs );
    }

    On my test site at http://test.thesingleparentperspective.com/take-time-to-praise-your-child/ you can see the ad is showing in the post.

    I created a custom field in the user profile for the publisher id code and it is called in the above code.

    Now, the ad is showing fine but by looking in the chrome browser inspect element feature (don't know what it is called just somewhat know how to use it) I can not tell if it is displaying the right publisher id for the right author as it is only showing the actual code

    <script type="text/javascript">
    var input = "<?php echo json_encode($input[0]);?>";
    <!--
        google_ad_client = "ca-",document.write(input);
        google_ad_width = 250;
        google_ad_height = 250;
        //-->
        </script>

    My request is if someone could verify that this code is good and safe.

    Please keep in mind that I am teaching myself how to write code. While I am very far from being able to write my own code I'm getting somewhat good at disecting other's code and manipulating for what I need.

    Any constructive critisism is appreciated.

  2. bcworkz
    Member
    Posted 3 months ago #

    I'm no security expert, but your code looks fine. If there were any risk, it would be when 'publisher_id' is saved as author meta or an option. If that input is validated and sanitized, your code should be safe, I don't see any other potential holes.

Reply

You must log in to post.

About this Topic

Tags

No tags yet.