Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Uncaught Error: Call to undefined method stdClass::format_value_for_rest()

未关闭
#964 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
php
领域
api

调研方向

定位 acf_format_value_for_rest() 入口点,并检查在调用 format_value_for_rest() 之前如何使用 acf_get_field_type() 的结果。使用缺少该方法的字段类型复现未定义方法错误,然后添加覆盖,表明 REST 值在没有未捕获错误的情况下得到处理。

由索引模型根据 Issue 内容生成。

描述

Describe the bug
External plugin sometimes break the REST api because they don't implement format_value_for_rest. |

Now this is really their fault. But a simple solution is to a function exists check:

method_exists( $type, 'format_value_for_rest' ) 
/**
 * Format a given field's value for output in the REST API.
 * 
 * @param        $value
 * @param        $post_id
 * @param        $field
 * @param string  $format 'light' for normal REST API formatting or 'standard' to apply ACF's normal field formatting.
 * @return mixed
 */
function acf_format_value_for_rest( $value, $post_id, $field, $format = 'light' ) {
        if ( $format === 'standard' && function_exists( 'acf_format_value' ) ) {
                $value_formatted = acf_format_value( $value, $post_id, $field );
        } else {
                $type = acf_get_field_type( $field['type'] );
        
                if ( method_exists( $type, 'format_value_for_rest' ) ) {
                        $value_formatted = $type->format_value_for_rest( $value, $post_id, $field );
                } else {
                        // Handle the case if neither function nor method exists
                        $value_formatted = $value; // or set default/fallback
                }
        }
         
        /**
         * Filter the formatted value for a given field.
         *
         * @param mixed      $value_formatted The formatted value.
         * @param string|int $post_id The post ID of the current object.
         * @param array      $field The field array.
         * @param mixed      $value The raw/unformatted value.
         * @param string     $format The format applied to the field value.
         */
        return apply_filters( 'acf/rest/format_value_for_rest', $value_formatted, $post_id, $field, $value, $format );
}
主要语言
PHP
星标
946
派生
197
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

AdvancedCustomFields/acf 的其他 Issue

查看 AdvancedCustomFields/acf 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。