投稿一覧に更新日をリセットするボタン追加する方法

Cocoonカスタマイズ集
この記事は約9分で読めます。
記事内に広告が含まれています。

記事に記載したコードは、スキンに追加されています。

はじめに

誤字を修正するだけでも、投稿を更新すると更新日時が変更されます。

Cocoonでは、これを防ぐために「更新日の変更」機能がエディターに備わっています。

そこで、投稿一覧に更新日を表示するカラムを追加し、更新日をリセットできるボタンを設置します。

この記事では、この方法について説明します。

完成イメージ

実装手順

以下のコードをfunctions.phpに追加します。

wp_postsテーブル内のpost_modified、post_modified_gmtを「更新日時=投稿日時」とします。

// 管理画面一覧にカラムを追加
add_filter('manage_edit-post_columns', function ($columns) {
  $columns['last_modified'] = '最終更新日';

  return $columns;
});


// 管理画面一覧のカラムを並び替え可能に設定
add_filter('manage_edit-post_sortable_columns', function ($columns) {
  $columns['last_modified'] = 'modified';

  return $columns;
});


// 管理画面一覧のカラムにデータを表示
add_action('manage_posts_custom_column', function ($column_name, $post_id) {
  if ($column_name !== 'last_modified') {
    return;
  }

  $p_date = get_the_date('Y年n月j日 H:i', $post_id);
  $u_date = get_the_modified_date('Y年n月j日 H:i', $post_id);

  // 投稿日と更新日が同じ場合
  if ($p_date === $u_date) {
    return;
  }

  $raw_url   = admin_url("admin-post.php?action=post_date_reset&id={$post_id}");
  $nonce_url = wp_nonce_url($raw_url, 'post_date_reset_action', 'post_date_reset_nonce');

  echo esc_html($u_date);
  echo '<br><a class="button button-small" href="' . esc_url($nonce_url) . '">リセット</a>';
}, 10, 2);


// 投稿データを保存
add_action('admin_post_post_date_reset', function () {
  $post_id = isset($_GET['id']) ? absint($_GET['id']) : 0;

  if (!$post_id) {
    wp_die('無効な投稿IDです。');
  }

  // nonceチェック
  if (!check_admin_referer('post_date_reset_action', 'post_date_reset_nonce')) {
    wp_die('セキュリティチェックに失敗しました。');
  }

  // 権限チェック
  if (!current_user_can('edit_post', $post_id)) {
    wp_die('この操作を行う権限がありません。');
  }

  // 更新日を投稿日に変更
  global $wpdb;
  $wpdb->query(
    $wpdb->prepare(
      "UPDATE {$wpdb->posts} SET post_modified = post_date, post_modified_gmt = post_date_gmt WHERE ID = %d",
      $post_id
    )
  );

  $redirect_url  = wp_get_referer();
  $safe_redirect = wp_validate_redirect($redirect_url, admin_url('edit.php'));

  wp_safe_redirect(add_query_arg('post_date_reset_done', '1', $safe_redirect));
  exit;
});

参考:投稿一覧の一括操作に「更新日をリセット」を追加する方法

投稿一覧の一括操作に「更新日をリセット」オプションを追加します。
これにより、複数の投稿を選択し、更新日をリセットできます。

完成イメージ

実装手順

以下のコードをfunctions.phpに追加します。

// 一括操作を追加
add_filter('bulk_actions-edit-post', function ($bulk_actions) {
  $bulk_actions['post_date_reset'] = '更新日をリセット';

  return $bulk_actions;
});


// 一括操作を実行
add_filter('handle_bulk_actions-edit-post', function ($redirect_url, $action, $post_ids) {
  if ($action !== 'post_date_reset') {
    return $redirect_url;
  }

  // 権限のある投稿IDのみを抽出
  $valid_ids = array_filter($post_ids, function ($id) {
    return current_user_can('edit_post', $id);
  });

  if (empty($valid_ids)) {
    return $redirect_url;
  }

  global $wpdb;

  // 更新日を投稿日に変更
  $ids_placeholder = implode(',', array_fill(0, count($valid_ids), '%d'));
  $wpdb->query(
    $wpdb->prepare(
      "UPDATE {$wpdb->posts}
       SET post_modified = post_date, post_modified_gmt = post_date_gmt
       WHERE ID IN ({$ids_placeholder})",
      $valid_ids
    )
  );

  set_transient('my_admin_notice', [
    'type'    => 'success',
    'message' => count($valid_ids) . ' 件の投稿の更新日をリセットしました。',
  ], 30);

  return $redirect_url;
}, 10, 3);


// 通知メッセージを出力
add_action('admin_notices', function () {
  if ($notice = get_transient('my_admin_notice')) {
    echo '<div class="post-date-reset__admin-notice notice notice-' . esc_attr($notice['type']) . ' is-dismissible"><p>' . esc_html($notice['message']) . '</p></div>';
    delete_transient('my_admin_notice');
  }
});

参考:全投稿・固定ページの更新日をリセットする方法

ダッシュボードに全ての投稿・固定ページの更新日をリセットするウィジェットを追加します。

完成イメージ

実装手順

以下のコードをfunctions.phpに追加します。

// ダッシュボードウィジェットを追加
add_action('wp_dashboard_setup', function () {
  wp_add_dashboard_widget(
    'post-date-reset',
    '更新日リセット',
    'render_post_date_reset'
  );
});


/**
 * ダッシュボードにウィジェットを出力
 *
 * @return void
 */
if (!function_exists('render_post_date_reset')) :
function render_post_date_reset() {
  ?>
  <form method="post">
    <?php wp_nonce_field('post_date_reset_action', 'post_date_reset_nonce'); ?>
    <input type="hidden" name="post_date_reset" value="1">
    <button type="submit" class="button button-primary" onclick="return confirm('投稿の更新日をすべて投稿日と同じに戻します。よろしいですか?')">
      全投稿をリセット
    </button>
  </form>
  <?php
}
endif;


// 管理画面からの設定データ更新
add_action('admin_init', function () {
  global $pagenow;

  if ($pagenow !== 'index.php' || !isset($_POST['post_date_reset'])) {
    return;
  }

  // nonceチェック
  if (!check_admin_referer('post_date_reset_action', 'post_date_reset_nonce')) {
    return;
  }

  // 権限チェック
  if (!current_user_can('manage_options')) {
    wp_die('この操作を行う権限がありません。');
  }

  global $wpdb;

  // 更新日を投稿日に変更('post' タイプのみを対象にする場合は post_type = 'post' に変更)
  $wpdb->query("
    UPDATE {$wpdb->posts}
    SET post_modified = post_date,
        post_modified_gmt = post_date_gmt
    WHERE post_type = 'post'
  ");

  set_transient('my_admin_notice', [
    'type'    => 'success',
    'message' => '全投稿の更新日をリセットしました。',
  ], 30);

  wp_safe_redirect(admin_url('index.php'));
  exit;
});


// 通知メッセージを出力
add_action('admin_notices', function () {
  if ($notice = get_transient('my_admin_notice')) {
    echo '<div class="post-date-reset__admin-notice notice notice-' . esc_attr($notice['type']) . ' is-dismissible"><p>' . esc_html($notice['message']) . '</p></div>';
    delete_transient('my_admin_notice');
  }
});
タイトルとURLをコピーしました