我遇到了一个很有希望解决的小问题。我的目标是从服务器获取一个现有的XML文件,解析它,然后将它作为一个列表注入Wordpress的原始WYSIWYG编辑器,这样站点所有者在编写新的帖子时就可以很容易地获得列表。现在,我在wp-admin/edit Advanced.php文件中有以下代码:
/**
* Fires after the title field.
*
* @since 3.5.0
*
* @param WP_Post $post Post object.
*/
do_action( 'edit_form_after_title', $post );
if ( post_type_supports($post_type, 'editor') ) {
?>
<div id="postdivrich" class="postarea<?php if ( $_wp_editor_expand ) { echo ' wp-editor-expand'; } ?>">
<?php
/** LOAD XML FROM SERVER AND PARSE AS UL INTO EACH NEW WP POST **/
$xml = simplexml_load_file('../my-folder/file.xml');
$product = "<br/><br/><h2 style='text-align:center; color:#003300;'><u>Products Available Now</u></h2><br/><ul style='text-align:center; list-style:none; color:#003300;'>";
foreach( $xml as $value ) {
$product .= "<li>";
$product .= $value->Description .= " $";
$product .= $value->Price .= " / ";
$product .= $value->QtyUnit .= "\n";
$product .= "</li>";
};
?>
<?php wp_editor($product, 'content', array(
'_content_editor_dfw' => $_content_editor_dfw,
'drag_drop_upload' => true,
'tabfocus_elements' => 'content-html,save-post',
'editor_height' => 300,
'tinymce' => array(
'resize' => false,
'wp_autoresize_on' => $_wp_editor_expand,
'add_unload_trigger' => false,
),
) ); ?>虽然它有效,但这会引起一些问题。
( 1)它将数据注入每个WYSIWYG编辑器,包括我想避免的页面。如果可能的话,内容应该只出现在post编辑器中。
2)当重新加载特定的管理页面时,它会导致一个非常严重的错误,它会擦除列表以外的任何内容。我无法保存任何草稿,或编辑帖子或网页,除非我在编辑过程中在浏览器中保持该会话打开。
不确定这些问题能否得到解决,但是任何和所有的帮助都是真诚的感谢!
发布于 2017-05-23 00:35:16
你应该永远不要修改WP核心文件。建议您对原始文件进行更新或恢复。
您所需要的可以通过这个小插件来实现:
<?php
/**
* Plugin Name: Default Post Content
*/
add_action( 'load-post-new.php', 'new_post_so_44123076' );
function new_post_so_44123076() {
# Only load if post type not defined (only occurs for Posts)
if( isset($_GET['post_type']) )
return;
add_filter( 'default_content', 'default_content_so_44123076' );
}
function default_content_so_44123076( $content ) {
# Build your own custom content
$content = "My html content.";
return $content;
}为插件创建一个文件夹,将代码放入文件(custom-content.php)中,并将XML放在同一个文件夹中。
可以这样检索到:
$xml = plugins_url( '/file.xml', __FILE__ );https://stackoverflow.com/questions/44123076
复制相似问题