How to stop PHP code running when in a child theme

I understand you can add extra CSS on top of the parent theme, but how do you remove PHP code from functions.php?

I want to remove the Masonry plugin in twentyfourteen theme, and have located the piece of code on line 254.

Read More
    if ( is_active_sidebar( 'sidebar-3' ) ) {
    wp_enqueue_script( 'jquery-masonry' );
    }

If I’m editing the parent theme, I can just comment this block out /* */.

But I’m not aware of a way to change this is my child theme.

Related posts

Leave a Reply

2 comments

  1. You can’t actually “remove” PHP code from the parent theme. What you can do is undo things done there.

    The counterpart of wp_enqueue_script is wp_dequeue_script.

    If you put this in your functions.php it should remove the Masonry Plugin (untested)

    add_action( 'wp_print_scripts', 'de_script', 100 );
    
    function de_script() {
        wp_dequeue_script( 'jquery-masonry' );
    }
    

    Source: Function Reference/wp dequeue script @ Codex

    As I haven’t tested this be aware of the fact that this could have unwanted side effects if the theme relies on the Masonry Plugin.

  2. I think, if your problem its just that script, you just can deregister it your child theme:

    function deregister_jqmasonry() {
       if ( is_active_sidebar( 'sidebar-3' ) ) {
           wp_deregister_script( 'jquery-masonry' );
       }
    }
    add_action( 'wp_enqueue_scripts', 'deregister_jqmasonry', 100 );
    

    High priority of add_action ensures it runs at the end.