【问题标题】:Is there a way to import json template files and use them on a page with Elementor from code?有没有办法从代码中导入 json 模板文件并在带有 Elementor 的页面上使用它们?
【发布时间】:2019-04-08 09:02:10
【问题描述】:

我正在尝试使用 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 的工作原理(至于它的结构),为什么我在导入后在站点文件夹中看不到 .json 文件(使用find 命令),或者给我任何关于我应该在哪里查看的指导,这将不胜感激。 (谷歌在这个问题上真的没有帮助,因为它主要展示了如何通过 GUI 来做这件事)。

谢谢!

【问题讨论】:

  • 澄清一下,您希望在 bash 命令行上导入 JSON 模板而不是我们的 GUI 模板导入方法?我注意到导入模板后没有保留 JSON 文件。因此,您找不到和归档的原因。
  • 是的!我想在不使用 gui 的情况下导入 json 模板。

标签: php json wordpress elementor


【解决方案1】:

我已经解决了同样的问题,并通过 curl 找到了以下解决方案:

  1. 登录wordpress
  2. 上传 elementor-kit.zip
  3. 确认导入(“激活”)

#!/bin/bash

# get an admin session cookie (adapt user/pwd as needed)
admin_page=$(curl -L --data 'log=ADMINUSER&pwd=ADMINPASSWORD'  -c wpcookie.txt 'https://{wordpress-url}/wp-login.php')

wp_nonce=$(grep -oPm1 "action=logout&#038;_wpnonce=\K[^']+" <<<$admin_page)
el_nonce=$(grep -oPm1 'var elementorCommonConfig.*"nonce":"\K[^"]+' <<<$admin_page)

echo wp_nonce $wp_nonce
echo el_nonce $el_nonce
echo Upload...
rval=$(curl '{wordpress-url}/wp-admin/admin-ajax.php' \
  -H "X-WP-NONCE: $wp_nonce" \
  -F action='elementor_import_kit' \
  -F "e_import_file=@elementor-kit.zip;type=application/zip" \
  -F data='{"stage":1}' \
  -F _nonce="$el_nonce" \
  -b wpcookie.txt)

data=$(jq -r '.data' <<< "$rval" )
session=$(jq -r '.session' <<< "$data" )

echo Activate... 
 curl '{wordpress-url}/wp-admin/admin-ajax.php' \
  -H "X-WP-NONCE: $wp_nonce" \
  -F action='elementor_import_kit' \
  -F data='{"stage":2, "session":'"\"$session\""',"include": ["settings","content","templates"],"overrideConditions":[]}' \
  -F _nonce="$el_nonce" \
  -b wpcookie.txt \
  -o output.txt

请注意,如果您将工具包导入其他站点,elementor 似乎会出现一些问题,并非所有帖子都正确链接到新的帖子 ID...

【讨论】:

    猜你喜欢
    • 2019-06-20
    • 2022-10-04
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多