【问题标题】:Gutenberg - Looking to inject block data into wp_head on saveGutenberg - 希望在保存时将块数据注入 wp_head
【发布时间】:2019-04-24 01:35:23
【问题描述】:

我创建了一个代码编辑器块,用户可以在其中插入 CSS 样式。与其将这些保存到 post_content 并显示在体内,不如将它们注入头部。

我过去曾使用 PHP 钩子完成此操作,但似乎无法弄清楚如何将其转换为新的 JS API 钩子格式

我发现这是通过functions.php添加的:

function hook_css() {
    ?>
        <style>
            .wp_head_example {
                background-color : #f1f1f1;
            }
        </style>
    <?php
}
add_action('wp_head', 'hook_css');

但我想使用来自用户输入的动态数据在块级别的基础上实现这一点。任何帮助将不胜感激!

【问题讨论】:

    标签: javascript wordpress wordpress-gutenberg gutenberg-blocks


    【解决方案1】:

    一种选择是将 CSS 保存为 post meta 并使用 post meta 将 CSS 插入到标题中。

    请参阅 Gutenberg tutorials on meta boxes 作为通过 Gutenberg 更新帖子元数据的指南。

    【讨论】:

      【解决方案2】:

      感谢 Simons 的回答,我能够完美地完成这项工作。

      结合他将数据存储为 post meta 的建议和 tutorial 我能够完成我的项目。

      【讨论】:

        猜你喜欢
        • 2018-01-03
        • 2022-06-19
        • 1970-01-01
        • 1970-01-01
        • 2019-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-16
        相关资源
        最近更新 更多