• Resolved MarkJ

    (@forusak)


    I tried disable plugins on wc-ajax and it is not working. I tried custom frontend url for *wc-ajax* and also woocommerce actions. I am comparing it with my function to disable plugins on wc-ajax, when I activate this function it decreases ajax loading time significantly, while this plugin does not.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Jose Mortellaro

    (@giuse)

    Hi Marek

    with the free version, you can disable only ajax actions that are listed in Actions => WooCommerce, not all of them.
    With the PRO version, you can disable whatever ajax action. You start a recording, you do the action, you stop the recording, and then you find the list of recorded actions where you can disable the plugins.

    What action are you trying to disable? If it’s an important action I will add it to the list of WooCommerce actions in the free version.

    @forusak

    Thread Starter MarkJ

    (@forusak)

    Ah I see, It is action from another plugin. Then why custom url doesn’t work? The function I am using has isset( $_GET['wc-ajax'] ) and it is working perfectly.

    Plugin Author Jose Mortellaro

    (@giuse)

    Custom URLs are only for the pages, not for ajax actions.
    If it wasn’t so many users had a lot of issues.
    When you write your custom code you know what you are doing and you use it where you need it. In this case, wc-ajax is clear, used only by WooCommerce, and you will not have problems. But in other cases, people would have serious problems for instance submitting a form, or whatever ajax action if they disable plugins using custom URLs and the same match works for ajax activities.
    This plugin deactivates entirely the other plugins. The PHP code doesn’t run either. If you don’t separate ajax from normal page URLs, it may cause disasters, and people will even don’t understand what happens.

    In your case, I suggest you use custom code. But as said, if the action is important I will add it to the list of WooCommerce actions that you find in Actions => WooCommerce in the free version.

    @forusak

    Thread Starter MarkJ

    (@forusak)

    @giuse Thanks, makes sense. I will use my code then. As you can see the rule is for all wc ajax actions, since I am trying to optimize wc and wp ajax.
    I have learned your plugin’s way to add custom actions, but it would be too much work for this case. Would you mind telling me if it is good idea do this? I am just starting with it and I am not sure if there is many plugins which doesn’t need to run on admin-ajax, in general. I see it is hard to know if any disabled plugin on ajax broke something or not.

    Plugin Author Jose Mortellaro

    (@giuse)

    Hi @forusak, you are welcome!

    I think in your case it’s easier to add your custom code.

    During the wc-ajax actions, you could disable all the plugins that have nothing to do with WooCommerce, but be careful to these two things that may happen:

    1. One of the plugins programmatically disable itself or another plugin if a third plugin is seen as not active
    2. One of the plugins calls a function or a class that is defined in another plugin without first checking if that function or class exists.

    If one of the two things above happens during the wc-ajax action, the result is that the first time the wc-ajax actin is triggered you will have some plugins permanently disabled. This problem occurs because the code of some plugins is not well written. Every time you use a function or class defined in another plugin you should first check they exists, but in the code of some plugins you can find something like this:

    $plugins = get_option( 'active_plugins' );
    if( in_array( 'example-plugin/example-plugin.php',$plugins ){
        //do something
    }

    or something like this:

    $plugins = get_option( ‘active_plugins’ );
    if( !in_array( ‘example-plugin/example-plugin.php’,$plugins ){
    //disable plugin…
    }

    In the first case, if example-plugin is globally active but disabled by your code, it will probably trigger a fatal error. In the second case it will disable a plugin globally.

    To avoid the first problem you should remove the filter that you add to disable the plugins. For the second issue you have to be care that you don’t leave active an add-on that depends on a “parent” plugin, and you disable the parent plugin, but not the add-on.

    You can write something like this:

    isset( $_REQUEST['wc-ajax'] ){
      add_filter( 'theme_root','__return_false',20 );
      add_filter( 'stylesheet','__return_false',20 );
      add_filter( 'template','__return_false',20 );
      add_filter( 'option_active_plugins', 'my_wc_action_actions_plugins',10,1 );
      add_action( 'plugins_loaded','my_remove_filters',9999 );
    }
    
    function my_wc_action_actions_plugins( $plugins ){
      $plugins = array(
        //Here you will define the plugins that you want active
      )
      return $plugins;
    }
    
    function my_remove_filters(){
    	remove_filter( 'option_active_plugins','my_wc_action_actions_plugins',10,1 );
    }

    I suppose you already know that the code goes in a mu-plugin that runs before any other normal plugin.
    The first lines are to disable also the theme. Probably you will not need the theme during the wc-ajax activities. I would use $_REQUEST instead of $_GET, so it will work both for $_GET and $_POST requests.

    I suggest you perform some tests before publishing the change. Try all kinds of things that you can do with WooCommerce and its add-ons.
    If it’s only for a single website, if you perform some tests you should not have problems.

    I hope it helps.

    Thread Starter MarkJ

    (@forusak)

    @giuse
    Amazing informations thank you. For th theme part, I would rather leave it active, if theme is made for woocommerce, there can be something. No idea if that can be the case. But also, many developers use to add custom wc code to functions, me also. Probably good reason to think of moving to custom plugins.

    If I may have one more question, I would like to know about admin-ajax something. Wc-ajax is quite common sense, but how about admin-ajax, is it true that even when a plugin doesn’t use ajax, it still can use a function or hook which is involved in an ajax call and if is plugin disabled ajaxes would return wrong data? In such case it would be almost impossible to tell by checking the code if it is safe to disable.

    • This reply was modified 4 years, 3 months ago by MarkJ.
Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘wc-ajax being ignored’ is closed to new replies.