首页 > 解决方案 > 有没有办法从代码中导入 json 模板文件并在带有 Elementor 的页面上使用它们?

问题描述

我正在尝试使用 bash 脚本在 WordPress 网站上动态设置一些页面。我希望页面使用 Elementor,以便未来的用户可以轻松配置它们。我已经发现可以通过 SQL 命令设置页面模板:

INSERT INTO
wp_postmeta
(post_id, meta_key, meta_value)
VALUES
(4, '_wp_page_template', 'elementor_canvas');

我想导入(并插入)json 模板文件(从另一个 Elementor 页面导出)。

我查看了https://github.com/elementor/elementor/issues/881并找到了这段代码(来自@crazypsycho):

function importTemplate( $filepath ) {
    $fileContent = file_get_contents( $filepath );
    $fileJson = json_decode( $fileContent, true );

    $result = \Elementor\Plugin::instance()->templates_manager->import_template( [
            'fileData' => base64_encode( $fileContent ),
            'fileName' => 'test.json',
        ]
    );

    if ( empty( $result ) || empty( $result[0] ) ) {
        return;
    }

    update_post_meta( $result[0]['template_id'], '_elementor_location', 'myCustomLocation' );
    update_post_meta( $result[0]['template_id'], '_elementor_conditions', [ 'include/general' ] );
}

我不明白该代码放在哪里以及如何执行它,将我的 json 模板文件放在哪里以及如何在帖子上显示它们。wp-cli 和 Elementor 的集成对我没有太大帮助,该import_library功能导致权限错误。

我还查看了导入模板时数据库中的更改,我发现它在wp_postmeta表中添加了一些值。但是,当我尝试重新创建它时,页面根本没有改变。

我正在寻找最简单的方法来完成从代码导入和插入 json Elementor 模板文件到站点。

如果有人能解释 Elementor 的工作原理(至于它的结构),为什么我在find导入后在站点文件夹中看不到 .json 文件(使用命令),或者给我任何关于我应该在哪里查看的指导,那将不胜感激. (谷歌在这个问题上真的没有帮助,因为它主要展示了如何通过 GUI 做到这一点的东西)。

谢谢!

标签: phpjsonwordpresselementor

解决方案


推荐阅读