Written by Anonymous
//キャンペーン(指定期間中のみ表示)
add_shortcode('campaign', 'campaign_shortcode');
if ( !function_exists( 'campaign_shortcode' ) ):
function campaign_shortcode( $atts, $content = null ) {
extract( shortcode_atts( array(
'from' => null, //いつから(開始日時)
'to' => null, //いつまで(終了日時)
'class' => null, //拡張クラス
), $atts, 'campaign' ) );
//内容がない場合は何も表示しない
if (!$content) return null;
//現在の日時を取得
$now = current_time('timestamp'); // ★修正
//いつから(開始日時)
$from = sanitize_shortcode_value($from);
$from_time = !empty($from) ? strtotime($from, $now) : strtotime('-1 day', $now); // ★修正
//いつまで(終了日時)
$to = sanitize_shortcode_value($to);
$to_time = !empty($to) ? strtotime($to, $now) : strtotime('+1 day', $now); // ★修正
//拡張クラス
if ($class) {
$class = ' '.$class;
}
$tag = null;
$content = apply_filters('campaign_shortcode_content', $content);
if (($from_time < $now) && ($to_time > $now)) {
$tag = '<div class="campaign'.esc_attr($class).'">'.
$content.
'</div>';
}
return $tag;
}
endif;