WordPress多站点插件激活触发致命错误,求技术解决方案
Hey there, let's break down this error and fix it step by step! That fatal error you're seeing has nothing to do with switching false to true—it's a PHP compatibility issue, plus a small logic structure mistake in your code. Here's what's going on and how to fix it:
What's Causing the Error?
Before PHP 5.5, the empty() function can't accept a function's return value directly (like empty(get_option(...))). It only works with variables. Your code does exactly this in two places, which triggers the fatal error. On top of that, your second conditional has a messed-up bracket structure that's breaking the intended logic.
Step 1: Fix the Update Notice Conditional (Line 15)
Your original line 15 code:
/* Change to true */ if (false == defined( 'CWCFP_PLUGIN' ) && empty( get_option( 'cwcfp-update-notice-dismissed' ) ) ){ add_action( 'admin_notices', 'cwcf_update_notice' ); }
Replace it with this corrected version:
/* Change to true */ $dismissed_update_notice = get_option( 'cwcfp-update-notice-dismissed' ); if ( !defined( 'CWCFP_PLUGIN' ) && empty( $dismissed_update_notice ) ) { add_action( 'admin_notices', 'cwcf_update_notice' ); }
- We first store the result of
get_option()in a variable ($dismissed_update_notice), which makes it compatible with older PHP versions. - Swapped
false == defined()for!defined()to make the code more readable—this does the exact same check, just cleaner.
Step 2: Fix the Review Notice Conditional
Your original review notice code has both the empty() function issue and a bracket error:
/* Change to true */ if ( false == defined( 'CWCFP_PLUGIN' ) && empty( get_option( 'cwcf-review-notice-dismissed' ) && !empty( get_option( 'cwcfp-update-notice-dismissed' ) ) ) ){ add_action( 'admin_notices', 'cwcf_review_notice' ); }
Replace it with this:
/* Change to true */ $dismissed_review_notice = get_option( 'cwcf-review-notice-dismissed' ); $dismissed_update_notice = get_option( 'cwcfp-update-notice-dismissed' ); if ( !defined( 'CWCFP_PLUGIN' ) && empty( $dismissed_review_notice ) && !empty( $dismissed_update_notice ) ) { add_action( 'admin_notices', 'cwcf_review_notice' ); }
- Again, we store
get_option()results in variables first to fix theempty()compatibility issue. - Fixed the bracket structure to correctly implement your intended logic: show the review notice only if the Pro plugin isn't active, the review notice hasn't been dismissed, and the update notice HAS been dismissed.
Full Corrected Plugin Code
Here's how the full fixed code will look:
<?php if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } function cwcf_update_notice() { $url = 'https://conditionalcheckoutfields.com/downloads/conditional-woo-checkout-field-pro/'; $link = sprintf( wp_kses( __( '<a href="%s">Click here</a> to upgrade Conditional Checkout Field to Pro! Get an unlimited number of checkout fields, duplicate checkout fields based on product quantity in the customer\'s cart, and more!', 'conditional-woo-checkout-field' ), array( 'a' => array( 'href' => array() ) ) ), esc_url( $url ) ); ?> <div class="notice notice-info cwcfp-update-notice is-dismissible"> <p><?php echo $link; ?></p> </div> <?php } /* Change to true */ $dismissed_update_notice = get_option( 'cwcfp-update-notice-dismissed' ); if ( !defined( 'CWCFP_PLUGIN' ) && empty( $dismissed_update_notice ) ) { add_action( 'admin_notices', 'cwcf_update_notice' ); } add_action( 'admin_enqueue_scripts', 'cwcf_update_notice_assets' ); function cwcf_update_notice_assets() { wp_enqueue_script( 'cwcfp-notice-update', CWCF_PLUGIN_URL . '/includes/js/notice-update.js', array( 'jquery' ), '1.0', true ); wp_enqueue_script( 'cwcf-notice-review', CWCF_PLUGIN_URL . '/includes/js/notice-review.js', array( 'jquery' ), '1.0', true ); } add_action( 'wp_ajax_cwcfp_dismiss_notice', 'cwcfp_dismiss_notice' ); function cwcfp_dismiss_notice(){ update_option( 'cwcfp-update-notice-dismissed', '1' ); } function cwcf_review_notice() { $url = 'https://wordpress.org/support/plugin/conditional-woo-checkout-field/reviews/#new-post'; $link = sprintf( wp_kses( __( 'Could we ask for a 5 star review of Conditional Checkout Field? It would be greatly appreciated! <a href="%s">Click here</a> to leave a review.', 'conditional-woo-checkout-field' ), array( 'a' => array( 'href' => array() ) ) ), esc_url( $url ) ); ?> <div class="notice notice-info cwcf-review-notice is-dismissible"> <p><?php echo $link; ?></p> </div> <?php } /* Change to true */ $dismissed_review_notice = get_option( 'cwcf-review-notice-dismissed' ); $dismissed_update_notice = get_option( 'cwcfp-update-notice-dismissed' ); if ( !defined( 'CWCFP_PLUGIN' ) && empty( $dismissed_review_notice ) && !empty( $dismissed_update_notice ) ) { add_action( 'admin_notices', 'cwcf_review_notice' ); } add_action( 'wp_ajax_cwcf_review_dismiss_notice', 'cwcf_review_dismiss_notice' ); function cwcf_review_dismiss_notice(){ update_option( 'cwcf-review-notice-dismissed', '1' ); }
Why This Works
- Storing
get_option()results in variables before usingempty()makes your code compatible with all PHP versions supported by WordPress (including older versions <5.5). - The corrected bracket structure ensures your notice display logic works exactly as you intended, without wrapping complex expressions inside
empty().
内容的提问来源于stack exchange,提问作者Kevin.a

