php - 有没有办法从代码中导入 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 做到这一点的东西)。
谢谢!
解决方案
推荐阅读
- ssl - 从 .cer 和 pfx 的密码生成 pfx
- ios - UICollectionViewController 未注册创建的自定义单元类
- python - 如何在 python-docx 中同时应用粗体和居中?
- sql - 使用不包含的 Active Record Join 获取嵌套对象
- android - android - 如何使用 LiveData 和 RxJava 处理错误?
- reactjs - 404 未找到:protobufjs@^5.0.0
- android - 如何在 android 上更改系统视图图标/样式、功能?
- c# - 当元素不存在时,使用 Selenium (C#) 的 IF ELSE 条件
- apache - 如何使用 Apache 在我的 URL 中隐藏 IP 和端口?
- java - 二进制代码上的约束计数集