未定义的属性:WP_Error:: $name

发布于 2025-01-11 13:41:18 字数 444 浏览 0 评论 0原文

有人可以帮我解决这个问题吗?尝试使用事件类型作为事件的名称,但它在标题中抛出错误

if (empty($_POST['_tmem_event_name'])){
    // phpcs:ignore WordPress.Security.NonceVerification
    $_POST['_tmem_event_name'] = get_term(
        sanitize_text_field(
            wp_unslash(empty($_POST['tmem_event_type']))
        ),
        'event-types'
    )->name;
    // phpcs:ignore WordPress.Security.NonceVerification
}

Can anyone help me with this please? Trying to use the event-type as the name of the event but it's throwing the error in the title

if (empty($_POST['_tmem_event_name'])){
    // phpcs:ignore WordPress.Security.NonceVerification
    $_POST['_tmem_event_name'] = get_term(
        sanitize_text_field(
            wp_unslash(empty($_POST['tmem_event_type']))
        ),
        'event-types'
    )->name;
    // phpcs:ignore WordPress.Security.NonceVerification
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落□心尘 2025-01-18 13:41:18

对于那些想要更具体答案的人来说,empty() 调用是根本问题。 empty() 检查提供的变量(本例中为 $_POST['term_event_type'])是否为空并返回 true值。我怀疑这里的意图是这样的:

// phpcs:disable WordPress.Security.NonceVerification
// phpcs:disable WordPress.WP.GlobalVariablesOverride
if ( empty( $_POST['_tmem_event_name'] ) ) {
    $post       = wp_unslash( $_POST );
    $event_type = ! empty( $post['tmem_event_type'] ) ? sanitize_text_field( $post['tmem_event_type'] ) : false;

    if ( $event_type ) {
        $post['_tmem_event_name'] = get_term(
            $event_type,
            'event-types'
        )->name;
    }
}
// phpcs:enable WordPress.Security.NonceVerification
// phpcs:enable WordPress.WP.GlobalVariablesOverride

由于您包含了 phpcs 忽略行,我假设您希望坚持编码标准 - 您也可能会收到有关覆盖全局变量的错误。我为你禁用了它。但是,您确实不应该禁用其中任何一个 - 它们的存在是有原因的,并且修复起来非常简单。如果您需要的话,很乐意提供帮助。

For those who want a more specific answer, the empty() call is the underlying problem. empty() checks if the provided variable ($_POST['term_event_type'] in this example) is empty and returns a true or false value. I suspect the intent here was something like this:

// phpcs:disable WordPress.Security.NonceVerification
// phpcs:disable WordPress.WP.GlobalVariablesOverride
if ( empty( $_POST['_tmem_event_name'] ) ) {
    $post       = wp_unslash( $_POST );
    $event_type = ! empty( $post['tmem_event_type'] ) ? sanitize_text_field( $post['tmem_event_type'] ) : false;

    if ( $event_type ) {
        $post['_tmem_event_name'] = get_term(
            $event_type,
            'event-types'
        )->name;
    }
}
// phpcs:enable WordPress.Security.NonceVerification
// phpcs:enable WordPress.WP.GlobalVariablesOverride

Since you're including a phpcs ignore line, I'll assume you're hoping to stick with a coding standard - you'll also likely be getting an error about overriding global variables. I disabled it for you. However, you really shouldn't be disabling either of those - they exist for a reason and are pretty simple to fix. Happy to help if you need it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文