// === CABABOOST 文脈CTA 位置制御 ここから ===
// 記事メタ cababoost_cta_pos で、文脈CTAの挿入位置を記事ごとに選べるようにする。
// ''(既定・全記事)… 従来どおり本文の直後(=ページ最下部)。挙動は一切変わらない
// 'h3:3' / 'h2:2' / 'h:4' … その見出しの直前に挿入する(h は h1-h6 のいずれか)
// Elementor記事では見出しがウィジェットdivに包まれているため、直近の
//
まで遡って「ウィジェットの直前」に差し込む
// (=完全な要素の境界に入れるのでDOMの入れ子は壊れない)。
// 位置を解決できない場合は必ず末尾へフォールバックする(CTAを落とさない)。
add_action( 'init', function () {
register_post_meta( 'post', 'cababoost_cta_pos', array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'default' => '',
'sanitize_callback' => 'cababoost_cta_pos_sanitize',
'auth_callback' => function ( $allowed, $meta_key, $post_id ) {
return current_user_can( 'edit_post', $post_id );
},
) );
} );
function cababoost_cta_pos_sanitize( $value ) {
$value = is_string( $value ) ? trim( $value ) : '';
if ( '' === $value ) {
return '';
}
return preg_match( '/^h[1-6]?:[1-9][0-9]?$/', $value ) ? $value : '';
}
function cababoost_cta_place( $content, $cta, $post_id ) {
$pos = cababoost_cta_pos_sanitize( get_post_meta( $post_id, 'cababoost_cta_pos', true ) );
if ( '' === $pos ) {
return $content . "\n" . $cta;
}
$parts = explode( ':', $pos );
$tag = $parts[0];
$nth = (int) $parts[1];
$pattern = ( 'h' === $tag ) ? '/
]*>/i' : '/<' . $tag . '\b[^>]*>/i';
if ( ! preg_match_all( $pattern, $content, $m, PREG_OFFSET_CAPTURE ) ) {
return $content . "\n" . $cta;
}
if ( count( $m[0] ) < $nth ) {
return $content . "\n" . $cta;
}
$at = $m[0][ $nth - 1 ][1];
$wrap = strrpos( substr( $content, 0, $at ), '
W1702HOBR-MAP – 郵便ポスト・郵便受けの専門メーカー| 郵便ポストの通販ならカバポスト