web-dev-qa-db-ja.com

プラグインが使用されていないときにCSS/JSの読み込みを禁止する

Visual Composerプラグインを含むWordpressテーマ(the7)があります。これは私のクライアントがバックエンドのものを編集するのには良いことですが、フロントエンドにロードされるすべてのページに不要なCSSとJSも追加します。これらを削除するにはどうすればいいですか。

追加する行は次のとおりです。

<link rel='stylesheet' id='js_composer_front-css'  href='[domain]/wp-content/plugins/js_composer/assets/css/js_composer.min.css?ver=5.0.1' type='text/css' media='all' />

<script type='text/javascript' src='[domain]/wp-content/plugins/js_composer/assets/js/dist/js_composer_front.min.js?ver=5.0.1'></script>

私は この質問 を見つけました/これは可能な方法を与えます、しかし私はそれを働かせることができません。

私は特定のCSSファイルがどこからロードされているかを突き止めました、それはこの関数のクラスVc_Baseにあります:

public function enqueueStyle() {
    $post = get_post();
    if ( $post && preg_match( '/vc_row/', $post->post_content ) ) {
        wp_enqueue_style( 'js_composer_front' );
    }
    wp_enqueue_style( 'js_composer_custom_css' );
}

だから私は私のfunctions.phpでこれを設定します:

function inf_remove_junk()
{
    wp_dequeue_style('js_composer_front');
    wp_dequeue_style('js_composer_custom_css');
    wp_dequeue_script('wpb_composer_front_js');

    // also tried this
    remove_action('wp_enqueue_scripts', array('Vc_Base', 'enqueueStyle'));
}

if (!is_admin()) {
    add_action('wp_head', 'inf_remove_junk');
}

inf_remove_junk関数は確実に実行されますが、CSSは削除されません。別のポイントに引っ掛ける必要がありますか、それとも他の何かをする必要がありますか?

1
DisgruntledGoat

wp_enqueue_scripts以外のアクションwp_headを使う必要があります。

function inf_remove_junk() {
    if (!is_admin()) {
          wp_dequeue_style('js_composer_front');
          wp_dequeue_style('js_composer_custom_css');
          wp_dequeue_script('wpb_composer_front_js');
     }

}

add_action( 'wp_enqueue_scripts', 'inf_remove_junk' );

しかし、それはフロントエンドからスクリプトを削除します。

  1. アーカイブページでは[VCはショートコードを実行しないと思います。そのため、is_admin()の代わりにis_archive()条件を確認できます。

  2. または、コンテンツからショートコードを確認して次のようなアセットを削除することもできます。

    関数inf_remove_junk(){

    // 1. Check shortcode exist in post content and disable scripts.
            global $post;
            if ( stripos($post->post_content, '[YOUR_SHORTCODE]') ) {
    
    // or
    
    // 2. Disable scripts on all pages except single page, post, custom Post etc.
    
            if ( ! singular() ) {
    
    // or
    
    // 3. Disable on archive, 404 and search page
            if ( is_archive() || is_404() || is_search() ) {
                  wp_dequeue_style('js_composer_front');
                  wp_dequeue_style('js_composer_custom_css');
                  wp_dequeue_script('wpb_composer_front_js');
             }     
    

    add_action( 'wp_enqueue_scripts'、 'inf_remove_junk');

また、プレミアムプラグインをサポートするには、より良い答えを得るためにプラグインの作者に連絡する必要があります。

1
maheshwaghmare

あなたはあなたがスクリプトとスタイルをデキューすることを確認する必要があります の後 それらはキューに入れられます。そうでなければ、あなたのコードは何もしていないように見えるでしょう。実際には、スクリプトを正しくデキューしていますが、後でまた追加されています。

どのフックにエンキューされているのかはわかりませんが、デフォルトの優先順位がwp_enqueue_scriptsである場合、これを低い優先順位でデキューすることで機能するはずです。

//* Dequeue scripts and styles
function wpse_106269_wp_enqueue_scripts() {
    wp_dequeue_style( 'js_composer_front' );
    wp_dequeue_style( 'js_composer_custom_css' );
    wp_dequeue_script( 'wpb_composer_front_js' );
}

//* Make sure we dequeue scripts and styles after they are enqueued
function wpse_106269_wp_head() {
  add_action( 'wp_enqueue_scripts', 'wpse_106269_wp_enqueue_scripts', 20 );
}

//* wp_head is only fired on public pages
add_action( 'wp_head', 'wpse_106269_wp_head');
0
Nathan Johnson