php - 如何从 "Carbon Fields 2.1.0"WP 插件中检索字段值?他们在哪个阶段可以访问?

标签 php wordpress carbon-fields carbon-fields-2

首先我下载了​​https://carbonfields.net/zip/latest/并在 WP 后端安装插件。我也激活了它。

对于这个测试用例,我使用“二十六”模板和全新的 WordPress 安装,没有安装任何其他插件,并根据 documentation page of Carbon Fields我将以下代码添加到我的 functions.php 文件的顶部:

<?php // PHP 7
use Carbon_Fields\Container;
use Carbon_Fields\Field;

add_action( 'carbon_fields_register_fields', 'crb_attach_theme_options' );
function crb_attach_theme_options() {
    Container::make( 'theme_options', 'Theme Options' )
        -> set_page_menu_position( 0 )
        -> add_fields( array(
            Field::make( 'text', 'crb_text')
        ) );
}

到目前为止一切看起来都很好,因为“主题选项”按照预期出现在 WP 后端。

screenshot Carbon Fields works in WP backend

现在我尝试按如下方式检索字段值 crb_text:

// this snippet starts exactly where the previous one ended
add_action( 'after_setup_theme', 'crb_load' );
function crb_load() {
    // require_once( ABSPATH . '/vendor/autoload.php' ); original from website throws: "Failed opening required" so modified to:
    require_once( ABSPATH . 'wp-content/plugins/carbon-fields/vendor/autoload.php' );
    \Carbon_Fields\Carbon_Fields::boot();
    var_dump( carbon_get_theme_option( 'crb_text' ) ); // -> string(0) ""
    var_dump( carbon_get_theme_option( '_crb_text' ) ); // -> string(0) "" isn't actually the right way to do it but give it a try for testing purpose
    var_dump( get_option( '_crb_text' ) ); // -> string(4) "test"
}

如您所见,我可以通过调用 get_option( '_crb_text' ) 来检索数据,这是原生 WP 方式,但插件函数 carbon_get_theme_option( 'crb_text' ) 不起作用。实际上,这对于“简单字段”来说没问题,但是有一些“复杂字段”必须由插件自己的函数检索,在这种情况下是 carbon_get_theme_option()

我也看过这个问题:use Carbon Fields in custom plugin class .但是这个问题在我的问题开始的地方结束。

提前致谢...


PS:我习惯于使用 Carbon Fields 1.6,它在非常相似的设置下运行良好,但想升级到分支 2。


再次是我的环境:define('WP_DEBUG', true);,Carbon Fields 2.1.0,WordPress 4.8.2–de_DE(全新安装,除 Carbon Fields 外没有其他插件),Twenty 16 1.3 , PHP 7

最佳答案

这是我与其中一位插件作者“Atanas Angelov”的聊天中的引述:

Hi @Elstermann you couldn't get the value because in order to get a field's value it has to be defined first. All fields are defined in the carbon_fields_fields_registered hook so any carbonget* calls before that hook has fired will not work (since no fields are defined yet).

因此,这是一种经过确认的引导碳场的方法:

use Carbon_Fields\Container;
use Carbon_Fields\Field;

add_action( 'carbon_fields_register_fields', 'crb_attach_theme_options' );
function crb_attach_theme_options() {
    Container::make( 'theme_options', 'Theme Options' ) -> add_fields( array(
        Field::make( 'text', 'crb_text')
    ) );
}

add_action( 'after_setup_theme', 'crb_load' );
function crb_load() {
    require_once( ABSPATH . 'wp-content/plugins/carbon-fields/vendor/autoload.php' );
    \Carbon_Fields\Carbon_Fields::boot();
}

add_action( 'carbon_fields_fields_registered', 'crb_values_are_avail' );
function crb_values_are_avail() {
    var_dump( carbon_get_theme_option( 'crb_text' ) ); // -> string(0) "test"
}

只是为了强调这里的核心问题......这是对上面片段的回复:

Yes - carbon_fields_fields_registered should be the earliest you can get a field's value


说明和相关性说明

这仅在您想要比主题文件更早检索数据时才有意义,因为在加载主题文件时 carbon_fields_fields_registered 操作 Hook 已经触发。因此,在您的主题文件中,只需调用:

carbon_get_theme_option( 'your_name_of_a_carbon_field' );
// for example in the "header.php" in your theme directory you could use
<style>body{background-color:<?php
    echo carbon_get_theme_option( 'custom_body_background' );
?>}</style> // just to give a real life like example

所有“carbon_get_*”函数都是这种情况,例如 carbon_get_post_meta()carbon_get_term_meta()carbon_get_user_meta()carbon_get_comment_meta()


有用

如果您想在主题文件之前检索数据,请确保这发生在 carbon_fields_fields_registered 操作 Hook 上,或者该 Hook 已经被触发。

如果您开发了一个 WP 插件并在其中集成了 Carbon Fields(这对我来说就是这样),则可能会出现这种情况。当您引导您的插件时,carbon_fields_fields_registered 操作 Hook 没有发生,因此请确保有正确的时间。

备选

如问题中所述,您还可以使用:

get_option( '_your_field_name_prepended_by_lodash' )

当您想要检索由以下人员设置的数据时:

Container::make( 'theme_options', 'Theme Options' ) -> add_fields()

但这伴随着以下缺点:

  1. 这不适用于复杂字段和
  2. 您无权访问由 Field::make(...)->set_default_value( $default_value ) 设置的值(与 Ca​​rbon Fields 方法相反)。

关于php - 如何从 "Carbon Fields 2.1.0"WP 插件中检索字段值?他们在哪个阶段可以访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46575565/

相关文章:

namespaces - 在命名空间中使用 Carbon 字段

javascript - 循环访问远程数据

php - 如何检查日期是否为 j-n-Y 格式?如果是的话如何将其转换为 d-m-Y 格式?

php - 如何从 WordPress 站点 URL 中删除 index.php

mysql - 从 MySQL 导出/导入 WordPress 元数据?

php - 这个正则表达式有什么作用

php - 在自定义插件类中使用 Carbon Fields

wordpress - Carbon-fields 不显示制造的领域

php - 在异常中使用 print_r?

php - Codeigniter 事件记录类 - 从数据库获取除一个字段之外的所有字段