我使用此方法创建了一个自定义贴子页面,但我希望内容是导入的XML文件内容是此文件的内容。https://stackoverflow.com/a/32314726/3697484
我将XML内容作为文件保存在插件文件夹中。
我基本上是在寻找一种方法,在管理选项页面中使用WordPress导入器功能来自动化这个过程用户单击管理选项页面中的链接=它将导入包含内容的XML页面。
我不知道是否有一个函数或API来完成它。请帮忙
没有导入类别、标记或自定义字段。
我实际上在尝试一种从elementor导入json输出的方法/从wordpress exporter导入elementor页面的xml输出
这是elementor将json转换为页面的地方:https://github.com/elementor/elementor/blob/master/includes/template-library/sources/local.php - 第895行
public function admin_import_template_form() {
if ( ! self::is_base_templates_screen() ) {
return;
}
/** @var \\Elementor\\Core\\Common\\Modules\\Ajax\\Module $ajax */
$ajax = Plugin::$instance->common->get_component( \'ajax\' );
?>
<div id="elementor-hidden-area">
<a id="elementor-import-template-trigger" class="page-title-action"><?php echo __( \'Import Templates\', \'elementor\' ); ?></a>
<div id="elementor-import-template-area">
<div id="elementor-import-template-title"><?php echo __( \'Choose an Elementor template JSON file or a .zip archive of Elementor templates, and add them to the list of templates available in your library.\', \'elementor\' ); ?></div>
<form id="elementor-import-template-form" method="post" action="<?php echo admin_url( \'admin-ajax.php\' ); ?>" enctype="multipart/form-data">
<input type="hidden" name="action" value="elementor_library_direct_actions">
<input type="hidden" name="library_action" value="direct_import_template">
<input type="hidden" name="_nonce" value="<?php echo $ajax->create_nonce(); ?>">
<fieldset id="elementor-import-template-form-inputs">
<input type="file" name="file" accept=".json,application/json,.zip,application/octet-stream,application/zip,application/x-zip,application/x-zip-compressed" required>
<input type="submit" class="button" value="<?php echo esc_attr__( \'Import Now\', \'elementor\' ); ?>">
</fieldset>
</form>
</div>
</div>
<?php
}
另一种方法:
private function import_single_template( $file_name ) {
$data = json_decode( file_get_contents( $file_name ), true );
if ( empty( $data ) ) {
return new \\WP_Error( \'file_error\', \'Invalid File\' );
}
$content = $data[\'content\'];
if ( ! is_array( $content ) ) {
return new \\WP_Error( \'file_error\', \'Invalid File\' );
}
$content = $this->process_export_import_content( $content, \'on_import\' );
$page_settings = [];
if ( ! empty( $data[\'page_settings\'] ) ) {
$page = new Model( [
\'id\' => 0,
\'settings\' => $data[\'page_settings\'],
] );
$page_settings_data = $this->process_element_export_import_content( $page, \'on_import\' );
if ( ! empty( $page_settings_data[\'settings\'] ) ) {
$page_settings = $page_settings_data[\'settings\'];
}
}
$template_id = $this->save_item( [
\'content\' => $content,
\'title\' => $data[\'title\'],
\'type\' => $data[\'type\'],
\'page_settings\' => $page_settings,
] );
if ( is_wp_error( $template_id ) ) {
return $template_id;
}
return $this->get_item( $template_id );
}
Not sure if they use this:
public function import_template( $name, $path ) {
if ( empty( $path ) ) {
return new \\WP_Error( \'file_error\', \'Please upload a file to import\' );
}
$items = [];
$file_extension = pathinfo( $name, PATHINFO_EXTENSION );
if ( \'zip\' === $file_extension ) {
if ( ! class_exists( \'\\ZipArchive\' ) ) {
return new \\WP_Error( \'zip_error\', \'PHP Zip extension not loaded\' );
}
$zip = new \\ZipArchive();
$wp_upload_dir = wp_upload_dir();
$temp_path = $wp_upload_dir[\'basedir\'] . \'/\' . self::TEMP_FILES_DIR . \'/\' . uniqid();
$zip->open( $path );
$valid_entries = [];
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
for ( $i = 0; $i < $zip->numFiles; $i++ ) {
$zipped_file_name = $zip->getNameIndex( $i );
$zipped_extension = pathinfo( $zipped_file_name, PATHINFO_EXTENSION );
if ( \'json\' === $zipped_extension ) {
$valid_entries[] = $zipped_file_name;
}
}
if ( ! empty( $valid_entries ) ) {
$zip->extractTo( $temp_path, $valid_entries );
}
$zip->close();
$file_names = array_diff( scandir( $temp_path ), [ \'.\', \'..\' ] );
foreach ( $file_names as $file_name ) {
$full_file_name = $temp_path . \'/\' . $file_name;
$import_result = $this->import_single_template( $full_file_name );
unlink( $full_file_name );
if ( is_wp_error( $import_result ) ) {
return $import_result;
}
$items[] = $import_result;
}
rmdir( $temp_path );
} else {
$import_result = $this->import_single_template( $path );
if ( is_wp_error( $import_result ) ) {
return $import_result;
}
$items[] = $import_result;
}
return $items;
}
是否有可能复制此过程?
Json导出:
{
"version":"0.4",
"title":"Export Template",
"type":"page",
"content":[
{
"id":"1f161830",
"settings":{
"content_width":{
"unit":"px",
"size":"980"
},
"custom_height":{
"unit":"px",
"size":"120"
},
"content_position":"middle",
"structure":"20",
"background_background":"classic",
"background_color":"#003e53",
"padding":{
"unit":"px",
"top":"0",
"right":"0",
"bottom":"30",
"left":"0",
"isLinked":false
},
"padding_tablet":{
"unit":"px",
"top":"0",
"right":"20",
"bottom":"0",
"left":"20",
"isLinked":false
},
"padding_mobile":{
"unit":"px",
"top":"30",
"right":"30",
"bottom":"30",
"left":"30",
"isLinked":true
}
},
"elements":[
{
"id":"4c55a461",
"settings":{
"_column_size":50,
"_inline_size":"64.388",
"padding":{
"unit":"px",
"top":"0",
"right":"0",
"bottom":"0",
"left":"0",
"isLinked":false
}
},
"elements":[
{
"id":"7c41176b",
"settings":{
"title":"Your Dream Vacation is Here",
"header_size":"h3",
"align_mobile":"center",
"title_color":"#ffffff",
"typography_typography":"custom",
"typography_font_size":{
"unit":"px",
"size":36
},
"typography_font_size_mobile":{
"unit":"px",
"size":32
},
"typography_font_family":"Roboto",
"typography_font_weight":"300"
},
"elements":[
],
"isInner":false,
"widgetType":"heading",
"elType":"widget"
}
],
"isInner":false,
"elType":"column"
},
{
"id":"528d240a",
"settings":{
"_column_size":50,
"_inline_size":"35.613"
},
"elements":[
{
"id":"79cddb6",
"settings":{
"text":"Book A Room",
"align":"right",
"align_mobile":"center",
"size":"lg",
"icon":"fa fa-angle-double-right",
"icon_align":"right",
"typography_typography":"custom",
"typography_font_size_tablet":{
"unit":"px",
"size":"15"
},
"typography_font_family":"Playfair Display",
"background_color":"#e4bc36",
"button_background_hover_color":"#8daca6",
"border_radius":{
"unit":"px",
"top":"0",
"right":"0",
"bottom":"0",
"left":"0",
"isLinked":true
}
},
"elements":[
],
"isInner":false,
"widgetType":"button",
"elType":"widget"
}
],
"isInner":false,
"elType":"column"
}
],
"isInner":false,
"elType":"section"
},
{
"id":"8ffa3fa",
"settings":{
"content_width":{
"unit":"px",
"size":"980"
},
"content_position":"middle",
"structure":"20",
"padding":{
"unit":"px",
"top":"80",
"right":"0",
"bottom":"0",
"left":"0",
"isLinked":false
},
"padding_tablet":{
"unit":"px",
"top":"60",
"right":"30",
"bottom":"30",
"left":"30",
"isLinked":false
},
"reverse_order_mobile":"reverse-mobile"
},
"elements":[
{
"id":"a721fd6",
"settings":{
"_column_size":50,
"_inline_size":null,
"padding":{
"unit":"px",
"top":"0",
"right":"60",
"bottom":"0",
"left":"0",
"isLinked":false
},
"padding_mobile":{
"unit":"px",
"top":"0",
"right":"0",
"bottom":"0",
"left":"0",
"isLinked":false
}
},
"elements":[
{
"id":"452fa18b",
"settings":{
"title":"Enjoy Some You-Time",
"header_size":"h3",
"align_mobile":"center",
"title_color":"#8daca6",
"typography_typography":"custom",
"typography_font_size":{
"unit":"px",
"size":36
},
"typography_font_family":"Playfair Display",
"typography_font_weight":"normal"
},
"elements":[
],
"isInner":false,
"widgetType":"heading",
"elType":"widget"
},
{
"id":"3c932056",
"settings":{
"editor":"<p>I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.<\\/p>",
"align_mobile":"center",
"typography_typography":"custom",
"typography_font_family":"Roboto"
},
"elements":[
],
"isInner":false,
"widgetType":"text-editor",
"elType":"widget"
},
{
"id":"2ae50433",
"settings":{
"text":"Read More",
"align":"left",
"align_mobile":"center",
"size":"md",
"icon":"fa fa-angle-double-right",
"icon_align":"right",
"typography_typography":"custom",
"typography_font_family":"Playfair Display",
"background_color":"#e4bc36",
"button_background_hover_color":"#8daca6",
"border_radius":{
"unit":"px",
"top":"0",
"right":"0",
"bottom":"0",
"left":"0",
"isLinked":true
}
},
"elements":[
],
"isInner":false,
"widgetType":"button",
"elType":"widget"
}
],
"isInner":false,
"elType":"column"
},
{
"id":"169d14c2",
"settings":{
"_column_size":50,
"_inline_size":null
},
"elements":[
{
"id":"41d9ad60",
"settings":{
"image":{
"id":591,
"url":"http:\\/\\/demo.geekygreenowl.com\\/wp-content\\/uploads\\/2019\\/11\\/main2.jpg"
},
"image_size":"full",
"image_border_border":"solid",
"image_border_width":{
"unit":"px",
"top":"10",
"right":"10",
"bottom":"10",
"left":"10",
"isLinked":true
},
"image_border_color":"#ffffff",
"image_box_shadow_box_shadow_type":"yes",
"image_box_shadow_box_shadow":{
"horizontal":"10",
"vertical":"10",
"blur":"20",
"spread":0,
"inset":"",
"color":"rgba(0,0,0,0.12)"
},
"_padding_mobile":{
"unit":"px",
"top":"0",
"right":"0",
"bottom":"30",
"left":"0",
"isLinked":false
}
},
"elements":[
],
"isInner":false,
"widgetType":"image",
"elType":"widget"
}
],
"isInner":false,
"elType":"column"
}
],
"isInner":false,
"elType":"section"
}
]
}