2

I've run into a little situation that is hopefully possible to solve. My goal is to take an existing XML file from the server, parse it, and then inject it as a list into Wordpress's original WYSIWYG editor so that the site owner has the list readily available when he writes a new post. Right now, I have this code in my wp-admin/edit-form-advanced.php file:

/**
 * 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,
    ),
) ); ?>

Although it works, this causes a couple issues.

1) It injects the data into every WYSIWYG editor, including pages, which I would like to avoid. The content should only appear in post editors if possible.

2) It causes a pretty serious bug that erases anything but the list whenever that particular admin page is reloaded. I can't save any drafts, or edit posts or pages unless I keep that session open in the browser during the editing process.

Not sure if these issues can be solved, but any and all help is sincerely appreciated!!

A. Hickman
  • 225
  • 1
  • 2
  • 7

1 Answers1

2

You should never modify WP core files. It's advisable that you update or restore the original files.

What you need can be achieved with this little plugin:

<?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;
}

Make a folder for the plugin, put the code inside a file (custom-content.php) and put the XML on the same folder.

It can be retrieved like this:

$xml = plugins_url( '/file.xml', __FILE__ );
brasofilo
  • 25,496
  • 15
  • 91
  • 179
  • 1
    This worked beautifully, thank you very much! I restored the original file and personalized the plugin code. Specified an absolute path to the XML file because it needs to be in a specific folder on the server. Really appreciate the help! – A. Hickman May 23 '17 at 12:58