You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress菜单保存时无法禁用Cloudflare插件,求修复方案

Fixing 502 Errors When Saving Large Menus with Cloudflare Plugin

Let's break down why your existing MU plugin isn't working, and how to fix it to properly disable the Cloudflare plugin during menu saves.

Why Your Original Code Failed

Your current code only checks for the exact nav-menus.php page URL via $_SERVER['PHP_SELF'], but there are three key gaps here:

  1. Strict URL matching: If your site is in a subdirectory, or your server appends extra path info to PHP_SELF, the strict equality check will fail.
  2. AJAX menu saves: Modern WordPress versions often use AJAX to save menu changes (especially with block-based menus), which means the request doesn't hit nav-menus.php directly—your code misses these cases entirely.
  3. Unindexed array: After unsetting the plugin from the active list, you didn't reindex the array, which can cause unexpected behavior when WordPress loads plugins.

Modified MU Plugin Code

This updated version fixes those gaps and ensures the Cloudflare plugin is disabled only when saving menus:

<?php
/*
Plugin Name: Disable Cloudflare During Menu Save
Plugin URI: http://www.example.com
Description: Temporarily disables the Cloudflare plugin when saving navigation menus to avoid timeout errors
Author: Kevin Pirnie
Version: 0.0.2
*/

// Filter active plugins to disable Cloudflare during menu saves
add_filter( 'option_active_plugins', function( $active_plugins, $option_name ) {
    // Only target the main active plugins option
    if ( $option_name !== 'active_plugins' ) {
        return $active_plugins;
    }

    $cloudflare_plugin = 'cloudflare/cloudflare.php';
    $is_menu_save_context = false;

    // Check if we're on the nav-menus admin page (GET or POST)
    if ( strpos( $_SERVER['PHP_SELF'], '/wp-admin/nav-menus.php' ) !== false ) {
        $is_menu_save_context = true;
    }

    // Check if it's an AJAX request for menu updates
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_POST['action'] ) && $_POST['action'] === 'update-nav-menu' ) {
        $is_menu_save_context = true;
    }

    // If we're in a menu save context, remove Cloudflare from active plugins
    if ( $is_menu_save_context && ( $plugin_key = array_search( $cloudflare_plugin, $active_plugins ) ) !== false ) {
        unset( $active_plugins[$plugin_key] );
        // Reindex the array to avoid plugin loading issues
        $active_plugins = array_values( $active_plugins );
        // Optional: Log to confirm the plugin was disabled
        // error_log( "Cloudflare plugin disabled for menu save request" );
    }

    return $active_plugins;
}, 999, 2 );

// Uncomment this block if you're running a multisite installation
/*
add_filter( 'site_option_active_plugins', function( $active_plugins, $option_name ) {
    if ( $option_name !== 'active_plugins' ) {
        return $active_plugins;
    }

    $cloudflare_plugin = 'cloudflare/cloudflare.php';
    $is_menu_save_context = false;

    if ( strpos( $_SERVER['PHP_SELF'], '/wp-admin/nav-menus.php' ) !== false ) {
        $is_menu_save_context = true;
    }

    if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_POST['action'] ) && $_POST['action'] === 'update-nav-menu' ) {
        $is_menu_save_context = true;
    }

    if ( $is_menu_save_context && ( $plugin_key = array_search( $cloudflare_plugin, $active_plugins ) ) !== false ) {
        unset( $active_plugins[$plugin_key] );
        $active_plugins = array_values( $active_plugins );
    }

    return $active_plugins;
}, 999, 2 );
*/

Key Improvements:

  • Flexible URL matching: Uses strpos() instead of strict equality to work with subdirectory installations.
  • AJAX support: Detects AJAX menu update requests, which are common in newer WordPress versions.
  • Array reindexing: Ensures the active plugins array stays properly formatted after unsetting.
  • Targeted filtering: Checks the $option_name to avoid interfering with other plugin-related options.

Alternative: Target Cloudflare's Cache Purge Directly

If you don't want to disable the entire plugin, you can instead intercept just the cache purge action. First, check the Cloudflare plugin's source code to find the hook they use for purging (e.g., save_post or a custom hook like cloudflare_purge_on_menu_save). Then add this to your MU plugin:

// Prevent Cloudflare from purging cache when saving menus
add_action( 'wp_update_nav_menu', function() {
    // Replace 'cloudflare_purge_cache' with the actual hook from Cloudflare's code
    remove_action( 'save_post', 'cloudflare_purge_cache' );
}, 10 );

Important Notes:

  • Make sure your MU plugin is saved in wp-content/mu-plugins/ with a .php extension (e.g., disable-cloudflare-menu-save.php). MU plugins load automatically, so no activation is needed.
  • Test in your staging environment first to confirm the 502 errors are gone.
  • If you're on multisite, uncomment the multisite filter block in the code.

内容的提问来源于stack exchange,提问作者Kevin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:32:42