반응형
페이지에서 연락처 양식 7 쇼트코드를 사용하지 않는 한 연락처 양식 7 CSS 및 JS 제거
그 페이지에서 숏코드를 사용할 때만 css와 javascript를 보여주고 싶습니다.워드프레스 페이지에 숏코드가 없는 경우 연락처 양식의 js와 css를 표시해서는 안 됩니다.이를 위해 제가 한 일은 제 액티브 테마 함수에 다음 코드를 붙여넣은 것입니다.php 파일.
add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );
위 코드는 컨택폼 7 플러그인의 js와 css를 완전히 제거합니다.제가 필요한 것은 연락처 양식 7 쇼트코드가 붙여져 있으면 둘 다 표시되어야 합니다.
여기 질문에 대한 답이 있습니다.쇼트코드가 없으면 css와 js의 연락처 양식이 제거되고 쇼트코드가 있으면 css와 js가 추가됩니다.
function rjs_lwp_contactform_css_js() {
global $post;
if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'contact-form-7') ) {
wp_enqueue_script('contact-form-7');
wp_enqueue_style('contact-form-7');
}else{
wp_dequeue_script( 'contact-form-7' );
wp_dequeue_style( 'contact-form-7' );
}
}
add_action( 'wp_enqueue_scripts', 'rjs_lwp_contactform_css_js');
테마 파일의 위젯과 쇼트코드에 해당하는 다른 버전이 필요했습니다.
add_filter( 'wpcf7_load_css', '__return_false' );
add_filter( 'wpcf7_load_js', '__return_false' );
remove_action( 'wp_enqueue_scripts','wpcf7_recaptcha_enqueue_scripts', 20 );
add_filter('pre_do_shortcode_tag', 'enqueue_wpcf7_css_js_as_needed', 10, 2 );
function enqueue_wpcf7_css_js_as_needed ( $output, $shortcode ) {
if ( 'contact-form-7' == $shortcode ) {
wpcf7_recaptcha_enqueue_scripts();
wpcf7_enqueue_scripts();
wpcf7_enqueue_styles();
}
return $output;
}
아래 코드를 사용합니다.이 코드에 페이지 ID를 추가할 수 있습니다.
function dvk_dequeue_scripts() {
$load_scripts = false;
if( is_singular() ) {
$post = get_post();
if( has_shortcode($post->post_content, 'contact-form-7') ) {
$load_scripts = true;
}
}
if( ! $load_scripts ) {
wp_dequeue_script( 'contact-form-7' );
wp_dequeue_style( 'contact-form-7' );
}
}
add_action( 'wp_enqueue_scripts', 'dvk_dequeue_scripts', 99 );
언급URL : https://stackoverflow.com/questions/48923540/remove-contact-form-7-css-and-js-unless-contact-form-7-shortcode-is-used-in-the
반응형
'programing' 카테고리의 다른 글
| jQuery 구문 오류: #/ (0) | 2023.09.19 |
|---|---|
| 신용카드 번호를 세션에 저장하는 것 - 그 주변의 방법? (0) | 2023.09.19 |
| SQL 삽입 문 반환 "0/무개 행 삽입" (0) | 2023.09.19 |
| 부트스트랩 3과 함께 부트스트랩-테마.css를 사용하는 방법? (0) | 2023.09.14 |
| 워드프레스 기능에 코드를 적용하는 것.단 하나의 카테고리로 php. (0) | 2023.09.14 |