• dkti

    (@dkti)


    I am optimizing performance on my company’s wordpress blog for https://www.nownovel.com/blog and would like to deactivate the ajax-search-lite plugin on my blog posts because I only use the search on the blog home and search pages.

    I have tried a couple different things to stop the ajax-search-lite code from loading on blog posts, but haven’t been successful. I suspect that the plugin is being loaded differently to other plugins.

    Does someone know how to do this?

    This is all the code I have tried so far – none of it works:

    function conditional_load_ajax_search_lite() {
      if ( is_home() || is_search() ) {
        include_once( ABSPATH . 'wp-content/plugins/ajax-search-lite/ajax-search-lite.php' );
      }
    }
    add_action( 'wp_loaded', 'conditional_load_ajax_search_lite' );
    function conditional_load_ajax_search_lite() {
      if ( !is_single() && ( is_home() || is_search() ) ) {
        include_once( ABSPATH . 'wp-content/plugins/ajax-search-lite/ajax-search-lite.php' );
      }
    }
    add_action( 'plugins_loaded', 'conditional_load_ajax_search_lite' );
    function remove_ajax_search_lite_on_posts() {
      if ( is_single() ) {
        remove_all_actions( 'plugins_loaded', 'ajax-search-lite' );
      }
    }
    add_action( 'init', 'remove_ajax_search_lite_on_posts' );
    function conditional_load_ajax_search_lite( $plugins ) {
      if ( is_single() ) {
        if( ( $key = array_search( 'ajax-search-lite/ajax-search-lite.php', $plugins ) ) !== false ) {
          unset( $plugins[$key] );
        }
      }
      return $plugins;
    }
    add_filter( 'option_active_plugins', 'conditional_load_ajax_search_lite' );

    I also tried using mu_plugins, like so:

    function conditional_load_ajax_search_lite( $plugins ) {
      if ( is_single() ) {
        $key = array_search( 'ajax-search-lite/ajax-search-lite.php', $plugins );
        if ( false !== $key ) {
          unset( $plugins[$key] );
        }
      }
      return $plugins;
    }
    add_filter( 'mu_plugin_active', 'conditional_load_ajax_search_lite' );

    and so:

    function conditional_load_ajax_search_lite( $plugins ) {
      if ( is_home() || is_page( 'search' ) ) {
        $plugins[] = 'ajax-search-lite/ajax-search-lite.php';
      }
      return $plugins;
    }
    add_filter( 'mu_plugin_active', 'conditional_load_ajax_search_lite' );

    All solutions had no effect

    • This topic was modified 2 years ago by dkti.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author wpdreams

    (@wpdreams)

    Hi,

    The plugin works the same as any other plugin, there is no difference whatsoever.

    These won’t work properly, because is_single() is_home() etc.. are not available and will not work in the MU plugin context, you can only check the request URLs. Also, these codes you have must be executed within the wpmu plugins directory to have any effect. If you use these within another plugin or within the theme functions.php, it will not work properly (or not at all).

    I have actually developed a MU plugin which can enable plugins based on the URL, you can check that out here. You can probably easily modify the source or use parts of it for your specific case.

    All the best,
    Ernest M.

    Thread Starter dkti

    (@dkti)

    Thanks Ernest, your code was very helpful and I have solved the problem using it. I made a change to your code to allow you to define the URIs using regex. Can I submit the code to your repo?

    Plugin Author wpdreams

    (@wpdreams)

    Sure! You can make a pull request and I will review, test and approve.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Stop Ajax Search Lite loading on my blog posts’ is closed to new replies.