-1

我正在为 Wordpress 编写插件,并且正在使用这些帖子。我基本上创建了一个替代帖子编辑页面。那就是问题所在:

当我加载页面以编写新帖子时,wp_editor 一如既往地工作,但是当我使用它来编辑帖子时,标题消失并且内容以白色书写。这使得无法阅读文本。

剩下的唯一一件事是“添加媒体”按钮不起作用。

我的代码很简单,我不明白为什么它的效果这么差。这是片段:

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() 函数

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_模板类

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

}

4

1 回答 1

0

我发现了问题。

我有另一个文件,CMS_List_Table.php。在批量函数处理程序中我改变了这个

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

在这个

if( 'edit'===$this->current_action() ) {
    CMS::showNewTemplatePanel();
}
于 2017-03-24T14:31:47.663 回答