删除联系表7 CSS和JS,除非页面中使用联系表7短代码 [英] Remove Contact Form 7 CSS and JS Unless Contact form 7 shortcode is used in the page
问题描述
我只想在该页面中使用简码时显示CSS和javascript。如果短代码未出现在wordpress页面中,则不应显示联系表格的js和css。为此,我将以下代码粘贴到了活动主题function.php文件中。
I want to show the css and javascript only when the shortcode is used in that page. If the short code not present in the wordpress page then the js and css of contact form should not be shown. For that what i have done is i have pasted the following code in my active themes function.php file.
add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );
以上代码完全删除了联系表格7插件的js和css。我需要的是如果粘贴了联系表单7短代码,则应同时显示两者。
The above code totally removes the js and css of contact form 7 plugin. What i need is if contact form 7 shortcode is pasted then both should be shown.
推荐答案
这是您问题的答案。如果没有简码,将删除联系表单的css和js,如果将简写css和js添加。
Here is the answer for your question. If there is not shortcode the css and js of contact form will be removed and if there is shortcode css and js will be added.
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');
这篇关于删除联系表7 CSS和JS,除非页面中使用联系表7短代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!