seiya.n1987
11/22/2019 - 11:28 AM

CF7 reCAPTCHAマークの表示制御

<?php
/***********************************************
 *CF7 reCaptchaの表示制御
 ***********************************************/
add_action( 'wp_enqueue_scripts', 'wpcf7_display_control_recaptcha_mark' );
add_action( 'wp_head', 'wpcf7_add_pagetop_style_on_recaptcha_mark', 99 );

function judge_control_recaptcha_mark() {

	$display_judge = false;
	$queried_object = get_queried_object();

	$search_short_code = 'contact-form-7';

	if( !empty( $queried_object ) && is_object( $queried_object ) ) {

		// 個別記事か固定ページ
		if( get_class( $queried_object ) === 'WP_Post' ) {
			$post_content = $queried_object->post_content;
			if( has_shortcode( $post_content, $search_short_code ) ) {
				$display_judge = true;
			}
		}

		// カテゴリ―かカスタムタクソノミーページ
		elseif( in_array( get_class( $queried_object ), array(
			'WP_Term',
		) ) ) {
			global $wp_query;
			$wp_query_posts = $wp_query->posts;
			$wp_query_posts_contents = array_column( $wp_query_posts, 'post_content' );
			$mix_contents = implode( '', $wp_query_posts_contents );

			if( has_shortcode( $mix_contents, $search_short_code ) ) {
				$display_judge = true;
			}
		}

	}

	return $display_judge;
}

function wpcf7_display_control_recaptcha_mark() {

	$display_judge = judge_control_recaptcha_mark();

    if( !$display_judge ) {
        wp_deregister_script( 'google-recaptcha' );
    }

}

function wpcf7_add_pagetop_style_on_recaptcha_mark() {
	$display_judge = judge_control_recaptcha_mark();

    if( $display_judge ) {
        echo '<style>.pagetop{bottom:90px;}</style>';
    }
}