Last active
February 22, 2020 05:17
-
-
Save damiencarbery/87a44cde45919d17a69b43f5e16e9513 to your computer and use it in GitHub Desktop.
Inject Adverts into Posts and Pages - Inject a specified page or widget area into post or page content. https://www.damiencarbery.com/2019/09/inject-adverts-into-posts-and-pages/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Inject Adverts into Posts and Pages | |
Plugin URI: https://www.damiencarbery.com/2019/09/inject-adverts-into-posts-and-pages/ | |
Description: Inject a specified page or widget area into post or page content. | |
Author: Damien Carbery | |
Author URI: https://www.damiencarbery.com | |
Version: 0.1 | |
*/ | |
// Add "Ad Content" page or sidebar after the specified paragraphs. | |
// Modified version of: http://www.wpbeginner.com/wp-tutorials/how-to-insert-ads-within-your-post-content-in-wordpress/ | |
add_filter( 'the_content', 'dcwd_insert_page_as_adverts' ); | |
function dcwd_insert_page_as_adverts( $content ) { | |
$ad_content_page_id = 10; | |
// Add advert after these paragraphs. | |
$paragraph_ids = array( 2, 4, 5, 8 ); | |
$use_page_content_for_adverts = true; | |
if ( is_singular() && is_main_query() ) { | |
// Do not add the ad content to the ad content page! | |
if ( get_the_ID() == $ad_content_page_id ) { | |
return $content; | |
} | |
// Whether to use page content or widgets for adverts. | |
$ad_content = ''; | |
if ( $use_page_content_for_adverts ) { | |
$ad_content_page_raw = get_post( $ad_content_page_id ); // http://localhost/wordpress/editorskit/ad-content/ | |
$ad_content = '<div class="ad-content" style="border:2px dashed #777; font-size:0.8em;">'. $ad_content_page_raw->post_content .'</div>'; | |
} | |
else { | |
ob_start(); | |
if ( is_active_sidebar( 'ad-content' ) ) { | |
dynamic_sidebar( 'ad-content' ); | |
} | |
$ad_content = '<div class="ad-content" style="border:2px dashed #777; font-size:0.8em;">' . ob_get_clean() . '</div>'; | |
} | |
$closing_p = '</p>'; | |
$paragraphs = explode( $closing_p, $content ); | |
foreach ($paragraphs as $index => $paragraph) { | |
// If it is not an empty paragraph (after removing whitespace at start and end) | |
// then append the '</p>' again - it was removed by explode() function. | |
if ( trim( $paragraph ) ) { | |
$paragraphs[$index] .= $closing_p; | |
} | |
// Add the advert if we are after one of the specified paragraphs. | |
if ( in_array( $index + 1, $paragraph_ids ) ) { | |
$paragraphs[$index] .= $ad_content; | |
} | |
} | |
// Optionally append the page at the end of the content too. | |
//$paragraphs[] = $ad_content; | |
return implode( '', $paragraphs ); | |
} | |
return $content; | |
} | |
add_action( 'widgets_init', 'dcwd_register_adverts_sidebar' ); | |
function dcwd_register_adverts_sidebar() { | |
$args = array( | |
'name' => 'Adverts', | |
'id' => 'ad-content', | |
'description' => 'Adverts to insert into pages.', | |
'class' => 'ad-content', | |
'before_widget' => '<div id="%1$s" class="widget %2$s">', | |
'after_widget' => '</div>', | |
'before_title' => '<h2 class="widgettitle">', | |
'after_title' => '</h2>' | |
); | |
register_sidebar( $args ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment