【问题标题】:PHP Class of composer package not found未找到作曲家包的 PHP 类
【发布时间】:2020-08-16 22:26:01
【问题描述】:

我正在编写一个 Wordpress 插件来创建自定义 Gutenberg 块。我正在使用一个名为 Carbon Fields 的作曲家包。但是当我尝试使用这个包时,我得到了一个错误:

致命错误:未捕获的错误:在第 10 行的 /my-path/my-plugin/my-plugin.php 中找不到类“Carbon_Fields\Block”

奇怪的是,我可以毫无问题地使用 Container 和 Field 类。

注意

我刚刚包含了基本的文件结构和代码,以便您可以专注于重要的事情。

文件结构

  • 我的插件
    • 供应商
      • html汉堡
        • 核心
          • Block.php
          • Carbon_Fields.php
          • 容器.php
          • Field.php
    • my-plugin.php

代码

my-plugin.php

add_action( 'after_setup_theme', 'carbon_fields_init' );
function carbon_fields_init() {
        require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';
        \Carbon_Fields\Carbon_Fields::boot();
}

use \Carbon_Fields\Block;
use \Carbon_Fields\Filed;

Block::make( __( 'Hero Image' ) )
    ->add_fields( array(
        Field::make( 'text', 'heading', __( 'Block Heading' ) ),
    ) )
    ->set_render_callback( function ( $fields, $attributes, $inner_blocks ) {
        ?>

        <div class="block">
            <div class="block__heading">
                <h1><?php echo esc_html( $fields['heading'] ); ?></h1>
            </div><!-- /.block__heading -->

        <?php
    } );

Block.php

namespace Carbon_Fields;

class Block extends Container {
    public static function make() {
        return call_user_func_array( array( 'parent', 'make' ), array_merge( array( 'block' ), func_get_args() ) );
    }
}

【问题讨论】:

    标签: php wordpress oop composer-php carbon-fields


    【解决方案1】:

    最后我从葡萄牙语教程 (https://www.youtube.com/watch?v=bKY-7_wR2n0) 中得到了答案。我必须将块创建包装在一个函数中,并将该函数设置为 carbon_fields_register_fields 动作挂钩的回调。

    use Carbon_Fields\Field;
    use Carbon_Fields\Block;
    
    add_action( 'after_setup_theme', 'crb_load' );
    function crb_load() {
        require_once( 'vendor/autoload.php' );
        \Carbon_Fields\Carbon_Fields::boot();
    }
    
    add_action( 'carbon_fields_register_fields', 'crb_add_test_block' );
    function crb_add_test_block() {
        Block::make( __( 'My Shiny Gutenberg Block' ) )
            ->add_fields( array(
                Field::make( 'text', 'heading', __( 'Block Heading' ) ),
                Field::make( 'rich_text', 'content', __( 'Block Content' ) ),
            ) )
            ->set_render_callback( function ( $fields, $attributes, $inner_blocks ) {
                ?>
    
                <div class="block">
                    <div class="block__heading">
                        <h1><?php echo esc_html( $fields['heading'] ); ?></h1>
                    </div><!-- /.block__heading -->
    
                    <div class="block__content">
                        <?php echo apply_filters( 'the_content', $fields['content'] ); ?>
                        <a href="">Reload</a>
                    </div><!-- /.block__content -->
                </div><!-- /.block -->
    
                <?php
            } );
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-26
      • 2018-08-23
      • 2017-04-08
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 2019-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多