Uncaught Error: Call to undefined method stdClass::format_value_for_rest()
还没有人认领这个 Issue。
评估
调研方向
定位 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,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
AdvancedCustomFields/acf 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
AdvancedCustomFields/acf#1041 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 86/100
AdvancedCustomFields/acf#1038 ·
-
难度 2/5 1-3 小时 新手友好度 78/100
AdvancedCustomFields/acf#1034 · 2 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 86/100
AdvancedCustomFields/acf#1032 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 78/100
AdvancedCustomFields/acf#1029 · 1 条评论 ·
查看 AdvancedCustomFields/acf 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 2 天内回复
-
UX
难度 2/5 1-3 小时 新手友好度 72/100
ProfessionalWiki/NeoWiki#1573 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 2/5 1-3 小时 新手友好度 68/100
-
bug
难度 2/5 1-3 小时 新手友好度 68/100
endoflife-date/endoflife.date#11194 ·
维护者通常 1 天内回复