wordpresswp-editor

wp_editor doesn't work in front-end


I'm writing a plugin for Wordpress and I'm using the posts. I basically created an alternative posts edit page. That's the problem:

When I load the page to write a new post wp_editor work as always, but when I use it to edit the post the header disappear and the content is written in white color. This makes impossible to read the text.

The only one thing that remains is the "Add media" button that does't work..

My code is pretty simple and I don't understand why it work so bad. Here's the snippet:

public static function showNewTemplatePanel(){
  if ( !current_user_can( 'manage_options' ) )  {
    wp_die( __( 'You cannot view this page' ) );
  }
  $template_id = null;

  //  Check if the user posted some data
  echo '<div class="wrap">';

  if ( isset( $_POST['posted_data'] ) && $_POST['posted_data'] == 'Y'){
    $template = new CMS_Template();

    if ( is_numeric( $_POST['template_id'] ) && $_POST['template_id'] > 0)
      $template = new CMS_Template($_POST['template_id']);

    $template->template_name = $_POST['template_name'];
    $template->template_content = $_POST['cms_template_editor'];
    $template->template_role = $_POST['role'];
    $template->template_subject = $_POST['template_subject'];

    $return = $template->save();

    if ( !$return || is_wp_error($return) ) {
      echo "<h1>Errore durante il salvataggio, ". $return ."</h1>";
      if ( is_wp_error( $return ) ) {
        echo '<div class="error">'.$return->get_error_message()."</div>";
      }
    } else {
      echo "<h1>Template salvato correttamente!</h1>";
      $template_id = $return;
    }
  }

  if ( isset( $_GET['template'] ) && is_numeric( $_GET['template'] ) ){
    $template_id = intval( $_GET['template'] );
  }

  if ( $template_id != null ) {
    ?>
    <h1>Edit Template</h1>
    <?php
  } else {
    ?>
    <h1>New Template</h1>
      <ul>
        <li>

        </li>
      </ul>
    </p>
    <?php
  }
  CMS::showTemplateEditor($template_id);
  echo '</div>';
}

CMS::showTemplateEditor() function

public static function showTemplateEditor( $id = null ){
  $template = new CMS_Template($id);
  ?>
    <form name="form-1" method="post" action="">
    <input type="hidden" name="posted_data" value="Y">
    <input type="hidden" name="template_id" value="<?php echo $template->ID; ?>">
    <table class="form-table">
      <tr class="form-field form-required">
        <th scope="row">
          <label for="template_name">Template Name</label>
        </th>
        <td>
          <input type="text" name="template_name" value="<?php echo $template->template_name; ?>"/>
        </td>
      </tr>
      <tr class="form-field form-required">
        <th scope="row">
          <label for="template_role">Assigned Role</label>
        </th>
        <td>
          <select name="role" id="role">
           <?php wp_dropdown_roles($template->template_role); ?>
          </select>
        </td>
      </tr>
      <tr class="form-field form-required">
        <th scope="row">
          <label for="template_name">Subject</label>
        </th>
        <td>
          <input type="text" name="template_subject" value="<?php echo $template->template_subject; ?>"/>
        </td>
      </tr>
      <tr class="form-field form-required">
        <th scope="row">
          <label for="template_name">Template Body</label>
        </th>
        <td>
          <?php wp_editor( $template->template_content, 'cms_template_editor'  ); ?>
        </td>
      </tr>
    </table>
    <p class="submit">
      <input type="submit" class="button button-primary" value="Save"/>
    </p>
  </form>
  <?php
}

CMS_Template Class

class CMS_Template {
public $ID;
public $template_name;
public $template_role;
public $template_subject;
public $template_content;

const ROLE_META = 'CMS_TEMPLATE_ROLE_META';
const SUBJECT_META = 'CMS_TEMPLATE_SUBJECT_META';

function __construct( $id = null ){
  $this->ID = 0;
  $this->template_name = "";
  $this->template_role = get_editable_roles()[0]['name'];
  $this->template_subject = "";
  $this->template_content = "";

  if ( $id != null && is_numeric( $id) && $id > 0 ) {
    $this->load( $id );
  }
}

public function save(){

  if ( trim( $this->template_name ) == "" )
    return false;

  if ( trim( $this->template_content ) == "" )
    return false;

  $return = wp_insert_post(array(
    'ID'              => $this->ID,
    'post_author'     => get_current_user_id(),
    'post_content'    => $this->template_content,
    'post_title'      => $this->template_name,
    'post_type'       => CMS::CMS_POST_TYPE,
    'meta_input'      => array( ROLE_META     => $this->template_role,
                                SUBJECT_META  => $this->template_subject ),
    'post_status'     => 'publish'
  ));

  return $return;
}

public function load( $id = null ){
  if ( $id != null && is_numeric( $id ) && $id > 0 ) {
    $post = get_post($id);
    if ( $post != null && $post->post_type == CMS::CMS_POST_TYPE ){
      $this->ID = $id;
      $this->template_name = $post->post_title;
      $this->template_content = $post->post_content;
      $this->template_role = get_post_meta( $post->ID, ROLE_META, true );
      $this->template_subject = get_post_meta( $post->ID, SUBJECT_META, true );
    }
  }
}

}


Solution

  • I found the problem.

    I have another file, the CMS_List_Table.php. In the bulk function handler i change this

    if( 'edit'===$this->current_action() ) {
        CMS::showNewTemplatePanel();
        wp_die();
    }
    

    In this

    if( 'edit'===$this->current_action() ) {
        CMS::showNewTemplatePanel();
    }