2

我有一个到目前为止没有功能的插件。这是当前的结构:

<?php
class Test
{
    public function __construct()
    {

    }
}

$wpTest = new Test();

我想使用Carbon Fields WordPress 插件。安装后,我根据网站上的说明更改了结构,仅适用于 OOP。

<?php
use Carbon_Fields\Container;
use Carbon_Fields\Field;

class Test
{

    public function __construct()
    {
        add_action( 'carbon_fields_register_fields', array( $this, 'crb_attach_theme_options') );
        add_action( 'after_setup_theme', array( $this , 'crb_load' ) );
    }

    public function crb_load()
    {
        require_once( 'vendor/autoload.php' );
        \Carbon_Fields\Carbon_Fields::boot();
    }

    public function crb_attach_theme_options()
    {
        Container::make( 'theme_options', __( 'Plugin Options', 'crb' ) )
            ->add_fields( array(
                Field::make( 'text', 'crb_text', 'Text Field' ),
            ) );
    }

}

$wpTest = new Test();

这没用。我如何解决它?

4

2 回答 2

4

作者本身的问题的答案可能适用于他自己的特定目的。

但是,如果您遇到这个问题很长一段时间,您可能希望将 Carbon Fields 集成到您自己的插件中(由于这个问题的语言化)。在这种情况下,您应该(至少)注意一个问题,即您的碳场数据可用的点;如果您想在插件执行时检索 Carbon Fields 数据。

TL;DR: In carbon_fields_fields_registeredaction hook 是您可以获得 Carbon Fields 值的最早阶段。这些字段首先必须在carbon_fields_register_fields动作挂钩中定义。有关其他说明,您还可以查看此答案

因此,这是一个确保有适当时机的引导程序:

use Carbon_Fields\Container;
use Carbon_Fields\Field;

class YourFancyPlugin
{

    public function __construct()
    {
        add_action( 'after_setup_theme', array( $this,
            'load_carbon_fields'
        ) );

        add_action( 'carbon_fields_register_fields', array( $this,
            'register_carbon_fields'
        ) );

        /* will succesfuly retrieve the data of the fields registered at
         * carbon_fields_register_fields action hook
         * if you retrieve the data before carbon_fields_fields_registered action hook
         * has fired it won't work
         */
        add_action( 'carbon_fields_fields_registered', array( $this,
            // picked this name only to emphasize whats going on
            'carbon_fields_values_are_available'
        ) );

        /* do all the stuff that doesn't rely on values of your Carbon Fields */
    }

    public function load_carbon_fields()
    {
        require_once 'vendor/autoload.php'; // modify depending on your actual setup
        \Carbon_Fields\Carbon_Fields::boot();
    }

    public function register_carbon_fields()
    {
        Container::make( 'theme_options', 'YourFancyPlugin options' )
            -> add_fields( array(
                Field::make( 'text', 'YourFancyPlugin_option_1')
            ) );
    }

    public function carbon_fields_values_are_available()
    {
        /* retrieve the values of your Carbon Fields related to your plugin */
        var_dump( carbon_get_theme_option( 'YourFancyPlugin_option_1' ) );
        /* do all the stuff that does rely on values of your Carbon Fields */
    }

}

new YourFancyPlugin();
于 2017-10-05T19:38:57.297 回答
3

我找到了我的问题的答案。从部分来看,问题是我vendor/autoload.php在访问__construct().

下面是解决此任务的示例

use Carbon_Fields\Container;
use Carbon_Fields\Field;



class PluginOption
{

    public function __construct()
    {
        require_once( 'vendor/autoload.php' );
        \Carbon_Fields\Carbon_Fields::boot();
        add_action( 'carbon_fields_register_fields', array( $this, 'crb_attach_theme_options') );
    }

    public function crb_attach_theme_options()
    {
        Container::make( 'theme_options', __( 'Plugin Option', 'crb' ) )
        ->add_fields( array(
            Field::make( 'text', 'crb_text', 'Text Field' ),
            ) );
    }

}

$wpTest = new PluginOption();
于 2017-08-23T13:40:43.873 回答