【问题标题】:Wordpress custom block not showing in block inserterWordpress 自定义块未显示在块插入器中
【发布时间】:2022-01-08 07:54:57
【问题描述】:

我正在尝试为网站创建自定义块,但该块没有出现在编辑器对话框中。我已经阅读了多个教程并更改了很多代码,但它根本行不通。

我检查过的内容:

  • 该块是通过插件添加的,但在以下情况下它也不起作用 移至主题。
  • 我知道插件工作正常,因为我可以使用其他 wp 插件中没有问题的钩子/操作。
  • 我尝试过同时使用“init”和“enqueue_block_assets”,但都没有 工作。
  • 我已验证所有文件位置和路径都正确,因为我 已将它们呼应出来进行检查。
  • 我已经改成默认主题了,还是没有出现。

任何帮助将不胜感激。

这里是js块src(已编译):

import { registerBlockType } from '@wordpress/blocks'

registerBlockType('ghs/landing-page-block', {
    title: 'Landing Page',
    apiVersion: 2,
    category: 'design',
    icon: 'smiley',
    description: 'Layout for the GHS landing page',
    keywords: ['GHS', 'landing', 'page', 'front'],
    edit: () => {
        return (<div>hello</div>)
    },
    save: () => {
        return (<div>hello</div>)
    }
});

和注册它的php:

add_action('init', function() {
    $asset_file = include( WP_PLUGIN_DIR . '/ghs-custom-blocks/assets/js/landing-page-block.asset.php');

    wp_register_script('ghs-landing-page',
        WP_PLUGIN_DIR . '/ghs-custom-blocks/assets/js/landing-page-block.js',
        $asset_file['dependencies'],
        $asset_file['version']);

    register_block_type('ghs/landing-page-block', [
        'api_version' => 2,
        'editor_script' => 'ghs-landing-page',
    ]);
});

【问题讨论】:

    标签: javascript php wordpress wordpress-gutenberg


    【解决方案1】:

    解决了,因为我用的是WP_PLUGIN_DIR 而不是plugin_dir_url(__FILE__)。这意味着 js 请求 url 来自根目录而不是 wp 安装。

    【讨论】:

      猜你喜欢
      • 2021-05-26
      • 2020-06-11
      • 1970-01-01
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-16
      • 1970-01-01
      相关资源
      最近更新 更多