Axe*_*xel 3 php wordpress carbon-fields carbon-fields-2
首先我下载了 https://carbonfields.net/zip/latest/并在 WP 后端安装了该插件。我也激活了。
\n\n对于这个测试用例,我使用“二十十六”模板进行全新安装的 WordPress,没有安装任何其他插件,并且根据Carbon Fields 的文档页面,我将以下代码添加到了 function.php 文件的顶部:
\n\n<?php // PHP 7\nuse Carbon_Fields\\Container;\nuse Carbon_Fields\\Field;\n\nadd_action( \'carbon_fields_register_fields\', \'crb_attach_theme_options\' );\nfunction crb_attach_theme_options() {\n Container::make( \'theme_options\', \'Theme Options\' )\n -> set_page_menu_position( 0 )\n -> add_fields( array(\n Field::make( \'text\', \'crb_text\')\n ) );\n}\nRun Code Online (Sandbox Code Playgroud)\n\n到目前为止,一切看起来都很好,因为“主题选项”正如预期一样出现在 WP 后端中。
\n\n\n\ncrb_text现在我尝试按如下方式检索字段值:
// this snippet starts exactly where the previous one ended\nadd_action( \'after_setup_theme\', \'crb_load\' );\nfunction crb_load() {\n // require_once( ABSPATH . \'/vendor/autoload.php\' ); original from website throws: "Failed opening required" so modified to:\n require_once( ABSPATH . \'wp-content/plugins/carbon-fields/vendor/autoload.php\' );\n \\Carbon_Fields\\Carbon_Fields::boot();\n var_dump( carbon_get_theme_option( \'crb_text\' ) ); // -> string(0) ""\n 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\n var_dump( get_option( \'_crb_text\' ) ); // -> string(4) "test"\n}\nRun Code Online (Sandbox Code Playgroud)\n\n正如您所看到的,我可以通过调用get_option( \'_crb_text\' )本机 WP 方式来检索数据,但插件功能carbon_get_theme_option( \'crb_text\' )不起作用。实际上,这对于“简单字段”来说很好,但在这种情况下,必须由插件自己的函数检索“复杂字段” carbon_get_theme_option()。
我也确实看过这个问题:use Carbon Fields in custom plugin class。但这个问题从我开始的地方结束。
\n\n先感谢您...
\n\nPS:我习惯使用 Carbon Fields 1.6,它在非常相似的设置下运行良好,但想升级到分支 2。
\n\n再次我的环境:define(\'WP_DEBUG\', true);,Carbon Fields 2.1.0,WordPress 4.8.2\xe2\x80\x93de_DE(除了Carbon Fields之外没有其他插件的全新安装),二十十六1.3,PHP 7
这是我与插件作者之一“Atanas Angelov”的一次聊天中的引用:
@Elstermann,您好,您无法获取该值,因为为了获取字段的值,必须首先定义它。所有字段都在carbon_fields_fields_registered 钩子中定义,因此在该钩子触发之前的任何carbonget* 调用都将不起作用(因为尚未定义任何字段)。
因此,这是一种已确认的启动 Carbon Fields 的方法:
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"
}
Run Code Online (Sandbox Code Playgroud)
只是为了强调这里的核心问题......这是对上面片段的回复:
是的 -
carbon_fields_fields_registered应该是您可以获得字段值的最早时间
仅当您想要检索早于主题文件中的数据时,这才有意义,因为carbon_fields_fields_registered加载主题文件时操作挂钩已经触发。因此,在您的主题文件中,只需调用以下命令就可以了:
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
Run Code Online (Sandbox Code Playgroud)
所有“carbon_get_*”函数都是这种情况,例如carbon_get_post_meta()、carbon_get_term_meta()、carbon_get_user_meta()、carbon_get_comment_meta()。
如果您想检索早于主题文件中的数据,请确保这发生在carbon_fields_fields_registered操作挂钩上或挂钩已被触发。
如果您开发一个集成了 Carbon Fields 的 WP 插件(对我来说就是这样),情况可能就是这样。当您引导插件时,carbon_fields_fields_registered动作挂钩不会发生,因此请确保时机正确。
正如问题中也提到的,您还可以使用:
get_option( '_your_field_name_prepended_by_lodash' )
Run Code Online (Sandbox Code Playgroud)
当您想要检索由以下方式设置的数据时:
Container::make( 'theme_options', 'Theme Options' ) -> add_fields()
Run Code Online (Sandbox Code Playgroud)
但这也有以下缺点:
Field::make(...)->set_default_value( $default_value )(与 Carbon Fields 方法相反)。| 归档时间: |
|
| 查看次数: |
3361 次 |
| 最近记录: |