在自定义插件类中使用Carbon Fields [英] use Carbon Fields in custom plugin class

查看:206
本文介绍了在自定义插件类中使用Carbon Fields的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a plugin which has no functionality so far. This is the current structure:

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

    }
}

$wpTest = new Test();

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

I want to use the Carbon Fields WordPress plugin. After installing it I changed the structure according to the instructions from the website, only with the adaptation to 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();

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

It does not work. How do I fix it?

推荐答案

我找到了问题的答案.从这部分来看,问题在于我访问了__construct()之后连接了vendor/autoload.php.

I found the answer to my question. From the part, the problem was that I connected the vendor/autoload.php after accessing the __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();

这篇关于在自定义插件类中使用Carbon Fields的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆