-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwp-adsense-time.php
70 lines (60 loc) · 1.96 KB
/
wp-adsense-time.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
<?php
/*
Plugin Name: Adsense Time
Plugin URI: http://github.com/fwenzel/wp-adsense-time
Description: This plugin allows you to include Google Adsense blocks in blog entries older than a specific value.
Version: 1.0
Author: Fred Wenzel
Author URI: http://fredericiana.com/
*/
/** OPTIONS */
// where do you want it to show up? After the first paragraph ("paragraph") or after the article text ("after")?
define('ADSENSE_POS', 'paragraph');
// how old do you want the entry to be before showing ads? (days)
define('ADSENSE_TIME', 1);
/* EXAMPLE ad block: copy your ad code from Adsense here */
define('AD_BLOCK', <<<EOF
<script type="text/javascript"><!--
google_ad_client = "pub-12345";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text";
google_ad_channel = "12345";
google_color_border = "DDDDDD";
google_color_bg = "FFFFFF";
google_color_link = "333333";
google_color_text = "777777";
google_color_url = "0066CC";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
EOF
);
/** END OF OPTIONS */
function adsense_time($posttext) { // show some ads
global $single, $posts;
if (!$single) return $posttext;
if (strtotime('+'.ADSENSE_TIME.' day', get_the_time('U')) > time()) return $posttext;
if ($posts[0]->post_status == 'static') return $posttext; // no adsense on static pages
switch (ADSENSE_POS) {
case 'paragraph':
$absatz = strpos($posttext, '</p>');
if ($absatz === false) {
$returntext = AD_BLOCK."<br />\n\n".$posttext;
} else {
$absatz += 4;
$returntext = substr($posttext, 0, $absatz);
$returntext .= "\n".AD_BLOCK."<br />\n";
$returntext .= substr($posttext, $absatz);
}
break;
case 'after':
default:
$returntext = $posttext . '<br/>' . AD_BLOCK;
break;
}
return $returntext;
}
add_filter('the_content', 'adsense_time');