【问题标题】:WordPress: Embed code with custom Gutenberg blockWordPress:使用自定义 Gutenberg 块嵌入代码
【发布时间】:2019-04-20 19:53:11
【问题描述】:

我正在尝试在我的 Gutenberg 块中包含自定义 HTML 嵌入输入,但不知道该怎么做。

我想知道在块内添加 HTML 的最佳方法是什么,就像使用自定义 HTML 块一样。有没有我错过的古腾堡元素?

我尝试了

的方法
attributes:{
        embed: {
            type: 'string',
            default: null,
        },
        ....
        }

然后我在edit()上创建了一个富文本

   let {  embed, ..... } = attributes;

<label>Embed HTML code here</label>
                    <RichText
                        tagName="div"
                        className="rich-text-holder"
                        placeholder= "Add the HTML"
                        value={ embed }
                        onChange={ ( embed ) => setAttributes( { embed } ) }
                    />

最后再次保存()

let { embed, .... } = attributes;

        function createMarkup() {
            return {__html: { embed } };
          }

 return().....
                    { embed &&
                    //<RawHTML className="listing-embed" >{ embed }</RawHTML>
                    <div dangerouslySetInnerHTML={ createMarkup() } />
                    }
....

我的第一个问题是属性保存为 &amp;lt;iframe src="https://www.w3schools.com"&gt;&amp;lt;/iframe&gt; 例如。第二个问题是在前端我只看到一个空的 div。

第三个问题是,即使我设法让代码显示为文本,而不是前端的实际代码

你有什么更好的方法或者知道我的代码有什么问题吗?

【问题讨论】:

    标签: wordpress reactjs wordpress-gutenberg gutenberg-blocks create-guten-block


    【解决方案1】:

    您可以查看自定义 HTML 块的工作方式in the github repository for Gutenberg

    我的第一个问题是属性保存为 例如。

    这大概是因为您使用的是 RichText。您可以改用TextareaControl 吗?也许它不会转义内容?

    第二个问题是在前端我只看到一个空的 div。

    我想这是因为 HTML 无效

    【讨论】:

    • 谢谢你,Simon,我确实使用了像自定义 HTML 块一样的 PlainText 元素,它起作用了
    猜你喜欢
    • 2021-06-19
    • 2021-04-11
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 2021-05-26
    • 2019-05-18
    • 1970-01-01
    相关资源
    最近更新 更多