Documentation

Selective WP Hide component loading using filters

Posted in: Getting Started (4) How To (20) Plugin Options Explained (42) Actions / Filters (36)      Code Example (22)  

The wp-hide/module_load_component filter controls whether an individual WP Hide component is loaded. It is evaluated for each component while a module’s load_components() method processes its component list.

The filter accepts four arguments: $load (whether to load the component, default TRUE), $component_id (unique component identifier), $component_class (component PHP class name), and $module (module instance).

Return FALSE to skip including and instantiating the component, or return the original $load value to preserve the default behavior. This allows conditional loading based on the component, module, or request context, without modifying WP Hide core files.

Disable specific components:



    add_filter( 'wp-hide/module_load_component', 'custom_wp_hide_component_loading', 10, 4 );
    function custom_wp_hide_component_loading( $load, $component_id, $component_class, $module )
        {
            $disabled_components = array(
                'general-feed',
                'general-oembed',
                'general-emulate'
            );

            if ( in_array( $component_id, $disabled_components, TRUE ) )
                return FALSE;

            return $load;
        }

Disable a component for REST API, AJAX, and cron requests:



    add_filter( 'wp-hide/module_load_component', 'custom_wp_hide_component_loading', 10, 4 );
    function custom_wp_hide_component_loading( $load, $component_id, $component_class, $module )
        {
            if ( $component_id !== 'general-oembed' )
                return $load;

            if ( defined( 'REST_REQUEST' ) && REST_REQUEST )
                return false;

            if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
                return false;

            if ( defined( 'DOING_CRON' ) && DOING_CRON )
                return false;

            return $load;
        }

Disable a component in wp-admin only:



    add_filter( 'wp-hide/module_load_component', 'custom_wp_hide_component_loading', 10, 4 );
    function custom_wp_hide_component_loading( $load, $component_id, $component_class, $module )
        {
            if ( $component_id !== 'general-emulate' )
                return $load;

            if ( is_admin() )
                return false;

            return $load;
        }

Disable one component on one exact page URL:



    add_filter( 'wp-hide/module_load_component', 'custom_wp_hide_component_loading', 10, 4 );
    function custom_wp_hide_component_loading( $load, $component_id, $component_class, $module )
        {
            $disabled_component = 'general-oembed';
            $target_path        = '/my-page/';

            $request_path = wp_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
            $request_path = untrailingslashit( $request_path );
            $target_path   = untrailingslashit( $target_path );

            if ( $component_id === $disabled_component && $request_path === $target_path )
                return false;

            return $load;
        }

Disable a component on staging or local environments:



    add_filter( 'wp-hide/module_load_component', 'custom_wp_hide_component_loading', 10, 4 );
    function custom_wp_hide_component_loading( $load, $component_id, $component_class, $module )
        {
            if ( $component_id !== 'general-html' )
                return $load;

            $host = isset( $_SERVER['HTTP_HOST'] ) ? strtolower( $_SERVER['HTTP_HOST'] ) : '';

            $staging_hosts = array(
                'staging.example.com',
                'local.example.test',
                'localhost'
            );

            if ( in_array( $host, $staging_hosts, true ) )
                return false;

            return $load;
        }

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn
Scroll to top