0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

wordpress の管理画面に入力項目を追加する(theme拡張/add_meta_box)

Posted at

wordpressの投稿画面に、添付画像の一覧を表示するBoxを追加してみます

add_meta_boxesに追加したアクション関数でadd_meta_box関数を呼び出すことで、情報出力欄を追加します

投稿編集画面に任意の文字列を表示する

出力イメージ
image.png

1. 文字列出力の関数を用意する

function.php
function attached_image_list($post){
  //$postは編集対象の投稿の情報が取得されます
  echo "image_list:".$post->ID;
}

2. filter add_meta_boxに関数 'attached_image_list'を関連づける

function.php
function oj_add_meta_box(){
  $html_id = "attach_image_list";
  $meta_box_title= "添付画像(ギャラリー画像)";
  $screen = null;
  $context = "normal";
  $priority= "high";

  add_meta_box($html_id,$meta_box_title,
    'attached_image_list',
    $screen,$context,$priority);

}

3. add_meta_boxesアクションに 2で定義した関数を追加

function.php
add_action('add_meta_boxes','oj_add_meta_box');

add_meta_box filterについて

$contextを"advanced"にすると入力欄が下の方に移動します

おまけ、添付画像の一覧を出力する

wordpressにpsr-4対応のautoloaderを仕込んであるので、わざわざContollerクラスを実装しています(なので、wordpressぽくないかも)

funciton.php
// 投稿画面のカテゴリーの配置調整
add_action('wp_terms_checklist_args', function ( $args, $post_id = null ) {
  $args['checked_ontop'] = false;
  return $args;
});

add_action('add_meta_boxes','oj_add_meta_box');

function oj_add_meta_box(){
  $html_id = "attach_image_list";
  $meta_box_title= "添付画像(ギャラリー画像)";
  $screen = null;
  $context = "normal";
  $priority= "high";
  add_meta_box($html_id,$meta_box_title,
    'attached_image_list',
    $screen,$context,$priority);
}

function attached_image_list($post){
  $gallery = new \controllers\PostGallery($post);
  $content = $gallery->get_attachment_list_for_admin_post();
  echo $content;
}
src/controllers/PostGallery.php
<?php


namespace controllers;


class PostGallery
{

  private $post;
  /**
   * PostGallery constructor.
   */
  public function __construct($post)
  {
      $this->post = $post;
  }

  public function get_attachment_list_for_admin_post(){
    $list = $this->get_all_attached_files();
    $content = "\n";
    foreach ($list as $att_id => $attachment){
      $image_output = wp_get_attachment_link( $att_id, "thumbnail", true, false, false);
      $content .= "<li>".$attachment->post_title."$image_output</li>";
    }
    if (strlen($content)>0){
      $content = "<ul>".$content."</ul>";
    }
    return $content;
  }

  private function get_all_attached_files(){
    // from media.php
    $attachments = get_children(
      array(
        'post_parent'    => $this->post->ID,
        'post_status'    => 'inherit',
        'post_type'      => 'attachment',
        'post_mime_type' => 'image',
        'order'          => 'ASC',
        'orderby'        => 'menu_order ID',
      )
    );
    return $attachments;
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?