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

WordPress多站点插件激活触发致命错误,求技术解决方案

Fixing the "Can't use function return value in write context" Fatal Error in Your WordPress Plugin

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 the empty() 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 using empty() 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:50:43