我正在使用 WordPress 框架,需要使用 Carbon Fields。问题是一切都在一个 lib 目录中,然后分成更多的目录,例如类、函数、短代码、插件等。然后这些目录相应地命名为 \ThemeName\Functions、\ThemeName\Plugins 等。
从 Carbonfields 文档中,它说将以下行添加到根目录中的 functions.php
use Carbon_Fields\Container;
use Carbon_Fields\Field;
但是如何在我的插件目录中使用 Carbon Fields?
当我尝试以下操作时,出现错误:
namespace \ThemeName\Plugins;
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', 'Text Field' ),
) );
}
add_action( 'after_setup_theme', 'crb_load' );
function crb_load() {
echo get_stylesheet_directory() . '/vendor/autoload.php';
require_once( get_stylesheet_directory() . '/vendor/autoload.php' );
\Carbon_Fields\Carbon_Fields::boot();
}
我希望能够将我所有的 Carbon Fields 代码放在这个文件中,但是由于它在命名空间中,我得到了错误
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'crb_load' not found or invalid function name in C:\xampp64\htdocs\sophie\wp-includes\class-wp-hook.php on line 286
否则,如果我删除名称空间,它可以正常工作。