Bạn hãy gián đoạn mã sau vào theme của bạn, vd: functions.php

add_filter( 'option_active_plugins', 'tmdev_exclude_plugins' );

function tmdev_exclude_plugins( $plugins ) {
    /**
     * If we're not performing our AJAX request, return early.
     */
    if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || ! isset( $_REQUEST['action'] ) || 'your_action_request' !== $_REQUEST['action'] ) {
     return $plugins;
    }
    /**
     * The list of plugins to exclude. Flip the array to make the check that happens later possible
     */
    $denylist_plugins = array_flip(
     array(
         'advanced-custom-fields-pro/acf.php',
         'advanced-forms/advanced-forms.php',
         'classic-editor/classic-editor.php',
         'wp-db-backup/wp-db-backup.php',
         'ewww-image-optimizer/ewww-image-optimizer.php',
         'limit-login-attempts-reloaded/limit-login-attempts-reloaded.php',
         'simple-custom-post-order/simple-custom-post-order.php',
         'theme-switcha/theme-switcha.php',
         'woocommerce/woocommerce.php',
         'wordpress-importer/wordpress-importer.php',
         'wp-mail-log/wp-mail-log.php',
         'amazon-s3-and-cloudfront/wordpress-s3.php',
         'wp-offload-ses/wp-offload-ses.php',
         'wppusher/wppusher.php'
     )
    );
    /**
     * Loop through the active plugins, if it's not in the deny list, allow the plugin to be loaded
     * Otherwise, remove it from the list of plugins to load
     */
    foreach ( $plugins as $key => $plugin ) {
     if ( ! isset( $denylist_plugins[ $plugin ] ) ) {
         continue;
     }
     unset( $plugins[ $key ] );
    }

    return $plugins;
}

Đối với REST API ban can thêm đoạn sau vào hàm tmdev_exclude_plugins của filter: option_active_plugins

Nếu bạn chưa biết cách viết REST API hãy tham khảo bài sau: Hướng dẫn tạo REST API trong WordPress

add_filter( 'option_active_plugins', 'tmdev_exclude_plugins' );
function tmdev_exclude_plugins( $plugins ) {
    /**
     * If we're not performing our AJAX request, return early.
     */
    if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || ! isset( $_REQUEST['action'] ) || 'your_action_request' !== $_REQUEST['action'] ) {
     return $plugins;
    }

    /**
     * check endpoint REST API
     */
    $request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
    if ( '/wp-json/your-namespace/v1/product-filter/' !== trailingslashit( $request_uri ) ) {
        return $plugins;
    }    

    /**
     * The list of plugins to exclude. Flip the array to make the check that happens later possible
     */
    $denylist_plugins = array_flip(
     array(
         'advanced-custom-fields-pro/acf.php',
         'advanced-forms/advanced-forms.php',
         'classic-editor/classic-editor.php',
         'wp-db-backup/wp-db-backup.php',
         'ewww-image-optimizer/ewww-image-optimizer.php',
         'limit-login-attempts-reloaded/limit-login-attempts-reloaded.php',
         'simple-custom-post-order/simple-custom-post-order.php',
         'theme-switcha/theme-switcha.php',
         'woocommerce/woocommerce.php',
         'wordpress-importer/wordpress-importer.php',
         'wp-mail-log/wp-mail-log.php',
         'amazon-s3-and-cloudfront/wordpress-s3.php',
         'wp-offload-ses/wp-offload-ses.php',
         'wppusher/wppusher.php'
     )
    );
    /**
     * Loop through the active plugins, if it's not in the deny list, allow the plugin to be loaded
     * Otherwise, remove it from the list of plugins to load
     */
    foreach ( $plugins as $key => $plugin ) {
     if ( ! isset( $denylist_plugins[ $plugin ] ) ) {
         continue;
     }
     unset( $plugins[ $key ] );
    }
    return $plugins;
}

 

Bạn cần thay các giá trị sau để phù hợp với query của bạn:

  • your_action_request: Đổi thành action name của bạn
  • /wp-json/your-namespace/v1/product-filter/: Đổi thành endpoint Rest API của bạn
  • $denylist_plugins: Đây là các plugin trong website của ban

Nguồn tham khảo: https://deliciousbrains.com/wordpress-rest-api-vs-custom-request-handlers/

Bài viết liên quan

post-no-image

Add the Meta Box Upload Multiple Images and multiple metabox

post-no-image

Add the Meta Box Repeat

post-no-image

Kỹ thuật debounce trong javascript – Trì hoãn nhập từ khóa trong ô input

post-no-image

Thêm VS Code snippets

post-no-image

Query only seach by title

post-no-image

Hướng dẫn tạo form có validate, upload file nhiều bước và xử lý ajax