Skip to content

Commit

Permalink
広告ショートコードの実装
Browse files Browse the repository at this point in the history
  • Loading branch information
yhira committed Dec 10, 2022
1 parent 28e7e16 commit a4c2ce4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
36 changes: 18 additions & 18 deletions lib/ad.php
Expand Up @@ -278,21 +278,21 @@ function is_index_middle_ad_visible($count){
endif;

//[ad]ショートコードに対して広告を表示する
add_filter('the_content', 'replace_ad_shortcode_to_advertisement');
if ( !function_exists( 'replace_ad_shortcode_to_advertisement' ) ):
function replace_ad_shortcode_to_advertisement($the_content){
//[ad]機能が有効な時
if (is_ad_shortcode_enable()) {
$ad_shortcode = '[ad]';
//本文にショートコードが含まれている場合
if (includes_string($the_content, $ad_shortcode)) {
ob_start();//バッファリング
get_template_part_with_ad_format(get_ad_shortcode_format(), 'ad-shortcode', is_ad_shortcode_label_visible());
//get_template_part('tmp/ad');//通常ページ用広告コード
$ad_template = ob_get_clean();
$the_content = preg_replace('{^(<p>)?'.preg_quote($ad_shortcode).'(</p>)?$}m', $ad_template, $the_content);
}
}
return $the_content;
}
endif;
// add_filter('the_content', 'replace_ad_shortcode_to_advertisement');
// if ( !function_exists( 'replace_ad_shortcode_to_advertisement' ) ):
// function replace_ad_shortcode_to_advertisement($the_content){
// //[ad]機能が有効な時
// if (is_ad_shortcode_enable()) {
// $ad_shortcode = '[ad]';
// //本文にショートコードが含まれている場合
// if (includes_string($the_content, $ad_shortcode)) {
// ob_start();//バッファリング
// get_template_part_with_ad_format(get_ad_shortcode_format(), 'ad-shortcode', is_ad_shortcode_label_visible());
// //get_template_part('tmp/ad');//通常ページ用広告コード
// $ad_template = ob_get_clean();
// $the_content = preg_replace('{^(<p>)?'.preg_quote($ad_shortcode).'(</p>)?$}m', $ad_template, $the_content);
// }
// }
// return $the_content;
// }
// endif;
12 changes: 12 additions & 0 deletions lib/shortcodes.php
Expand Up @@ -947,3 +947,15 @@ function updated_shortcode( $atts ) {
return $res;
}
endif;

//広告ショートコード
add_shortcode('ad', 'ad_shortcode');
if ( !function_exists( 'ad_shortcode' ) ):
function ad_shortcode( $atts ) {
ob_start();//バッファリング
get_template_part_with_ad_format(get_ad_shortcode_format(), 'ad-shortcode', is_ad_shortcode_label_visible());

return ob_get_clean();
}
endif;

0 comments on commit a4c2ce4

Please sign in to comment.