自作プラグインの作り方

WordPress管理画面で「秒」も表示するプラグイン

WordPressの投稿編集画面では、デフォルトで時刻の「秒」部分が表示されません。
以下の手順で「秒」まで編集・表示できるようにするシンプルなプラグインを作成します。

1. プラグインファイルを作成

WordPress の wp-content/plugins フォルダに、例えば show-seconds.php という名前の新しいファイルを作成します。

内容は以下の通り:

<?php
/*
Plugin Name: Show Seconds in Post Edit
Description: 投稿編集画面で日時に「秒」も表示できるようにするプラグイン
Version: 1.0
Author: あなたの名前
*/
 
add_action('admin_footer', function () {
    echo <<<EOD
<script>
document.addEventListener('DOMContentLoaded', function () {
    const fields = ['mm', 'jj', 'aa', 'hh', 'mn'];
    const secsInput = document.createElement('input');
    secsInput.type = 'text';
    secsInput.name = 'ss';
    secsInput.size = 2;
    secsInput.value = (new Date()).getSeconds().toString().padStart(2, '0');
    const label = document.createElement('label');
    label.textContent = '秒';
    const container = document.querySelector('.timestamp-wrap');
    if (container) {
        container.appendChild(secsInput);
        container.appendChild(label);
    }
});
</script>
EOD;
});

2. プラグインを有効化

WordPressの管理画面「プラグイン」から「Show Seconds in Post Edit」を有効にしてください。

3. 動作確認

投稿や固定ページの編集画面に移動し、「公開日時」のフィールドに「秒」入力欄が追加されていることを確認します。

備考

  • 時刻入力欄はあくまでUI上の追加なので、保存時の動作やフォーマット変更が必要な場合は save_post フックなどを追加で使ってください。
  • 他のプラグインやテーマとの互換性には注意してください。
memo/wordpress/custum_plug_in.txt · 最終更新: by nfujishima
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0