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

时间:2017-08-22 15:29:00

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

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

Creating Plan (Running solver): / pkg install: No solution was found
to satisfy constraints

maintained incorporations:  
pkg://solaris/consolidation/SunVTS/SunVTS-incorporation@7.19.2-0.175.3.0.0.26.3
pkg://solaris/consolidation/X/X-incorporation@0.5.11-0.175.3.1.0.2.1489
pkg://solaris/consolidation/admin/admin-incorporation@0.5.11-0.175.1.0.0.5.0
pkg://solaris/consolidation/cacao/cacao-incorporation@0.5.11-0.175.3.0.0.24.0
pkg://solaris/consolidation/cde/cde-incorporation@0.5.11-0.175.3.0.0.16.0
pkg://solaris/consolidation/cns/cns-incorporation@0.5.11-0.175.3.0.0.24.0
pkg://solaris/consolidation/dbtg/dbtg-incorporation@0.5.11-0.175.3.0.0.29.0
pkg://solaris/consolidation/ddt/ddt-incorporation@8.9.15.9.11  
pkg://solaris/consolidation/desktop/desktop-incorporation@0.5.11-0.175.3.0.0.28.0
pkg://solaris/consolidation/desktop/gnome-incorporation@0.5.11-0.175.3.0.0.26.0
pkg://solaris/consolidation/gfx/gfx-incorporation@0.5.11-0.175.1.0.0.5.0
pkg://solaris/consolidation/ips/ips-incorporation@0.5.11-0.175.3.0.0.26.0
pkg://solaris/consolidation/java-7/java-7-incorporation@1.7.0.85.33-0 
pkg://solaris/consolidation/java-8/java-8-incorporation@1.8.0.60.27-0 
pkg://solaris/consolidation/jdmk/jdmk-incorporation@0.5.11-0.175.2.0.0.22.0
pkg://solaris/consolidation/l10n/l10n-incorporation@0.5.11-0.175.3.0.0.26.2
pkg://solaris/consolidation/man/man-incorporation@0.5.11-0.175.3.0.0.28.0
pkg://solaris/consolidation/nspg/nspg-incorporation@0.5.11-0.175.1.0.0.5.0
pkg://solaris/consolidation/nvidia/nvidia-incorporation@0.5.11-0.175.3.0.0.17.0
pkg://solaris/consolidation/osnet/osnet-incorporation@0.5.11-0.175.3.1.0.5.0
pkg://solaris/consolidation/sfw/sfw-incorporation@0.5.11-0.175.3.0.0.30.0
pkg://solaris/consolidation/sic_team/sic_team-incorporation@0.5.11-0.175.3.0.0.20.0
pkg://solaris/consolidation/solaris_re/solaris_re-incorporation@0.5.11-0.175.3.1.0.5.0
pkg://solaris/consolidation/ssm/ssm-incorporation@2.3.3.0-0.175.3.0.0.29.0
pkg://solaris/consolidation/sunpro/sunpro-incorporation@0.5.11-0.175.3.20.0.2.0
pkg://solaris/consolidation/ub_javavm-6/ub_javavm-6-incorporation@1.6.0.999.99-0
pkg://solaris/consolidation/userland/userland-incorporation@0.5.11-0.175.3.1.0.3.0
pkg://solaris/consolidation/xvm/xvm-incorporation@0.5.11-0.175.1.0.0.5.0
pkg://solaris/entire@0.5.11-0.175.3.1.0.5.2

Plan Creation: Package solver is unable to compute solution.
Dependency analysis is unable to determine exact cause. Try specifying
expected results to obtain more detailed error messages. Include
specific version of packages you wish installed.

我想使用Carbon Fields WordPress插件。安装完成后,我根据网站上的说明改变了结构,只适应了OOP。

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

    }
}

$wpTest = new Test();

它不起作用。我该如何解决?

2 个答案:

答案 0 :(得分:2)

我找到了问题的答案。从部分来看,问题是我在访问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();

答案 1 :(得分:0)

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

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

TL; DR:carbon_fields_fields_registered动作挂钩是您可以获得碳场值的最早阶段。首先必须在carbon_fields_register_fields动作钩子中定义这些字段。有关其他说明,您还可以查看this answer

所以这是一个确保有正确时间的引导程序:

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();