ChatGPT-4o が使えるようになったので、過去にやってもらった同じ内容を投げてみた
以前 ChatGPT と Bard に WordPress のプラグインを作ってとお願いしてやってみました。
今回 ChatGPT-4o が使えるようになったので、まったく同じ質問でプラグインを作ってもらってみました。さてどの程度向上したのでしょうか。
以下、実際のやり取り
WordPress のプラグインを新規作成する際のテンプレートのコードを書いてくれませんか。
もちろんです!WordPressのプラグインを新規作成する際の基本的なテンプレートコードを以下に示します。このテンプレートは、WordPressプラグインの基本構造を持っており、プラグインを動作させるための最小限のコードが含まれています。
プラグインファイルの作成
-
プラグインフォルダの作成:
プラグインのディレクトリをwp-content/plugins
内に作成します。例えば、my-first-plugin
というフォルダを作成します。 -
メインプラグインファイルの作成:
フォルダ内にメインプラグインファイルを作成します。例えば、my-first-plugin.php
というファイルを作成します。
メインプラグインファイルの内容
<?php
/*
Plugin Name: My First Plugin
Plugin URI: https://example.com/my-first-plugin
Description: これは私の最初のプラグインです。
Version: 1.0.0
Author: あなたの名前
Author URI: https://example.com
License: GPL2
*/
// セキュリティ対策:直接アクセスを防ぐ
if ( !defined('ABSPATH') ) {
exit;
}
// プラグインの初期化関数
function my_first_plugin_init() {
// ここにプラグインの初期化コードを記述します
// 例:カスタム投稿タイプの登録
}
// プラグインのアクティベーションフック
function my_first_plugin_activate() {
// プラグインがアクティベートされたときに実行するコードをここに記述します
// 例:オプションのデフォルト値を設定する
add_option('my_first_plugin_option', 'default_value');
}
// プラグインのデアクティベーションフック
function my_first_plugin_deactivate() {
// プラグインがデアクティベートされたときに実行するコードをここに記述します
// 例:オプションを削除する
delete_option('my_first_plugin_option');
}
// アクションフックを追加
add_action('init', 'my_first_plugin_init');
// アクティベーション・デアクティベーションフックを追加
register_activation_hook(__FILE__, 'my_first_plugin_activate');
register_deactivation_hook(__FILE__, 'my_first_plugin_deactivate');
// プラグインの主要機能を追加する関数
function my_first_plugin_main_function() {
// ここにプラグインの主要な機能を記述します
// 例:ショートコードの登録
add_shortcode('my_first_shortcode', 'my_first_shortcode_function');
}
// ショートコードのコールバック関数
function my_first_shortcode_function($atts) {
// ショートコードが呼び出されたときに実行するコードをここに記述します
return "Hello, this is my first shortcode!";
}
// アクションフックを追加
add_action('wp_loaded', 'my_first_plugin_main_function');
?>
プラグインの説明
- Plugin Name: プラグインの名前。
- Plugin URI: プラグインの詳細ページのURL。
- Description: プラグインの説明。
- Version: プラグインのバージョン。
- Author: プラグインの作者の名前。
- Author URI: 作者のウェブサイトのURL。
- License: プラグインのライセンス。
基本機能の説明
-
セキュリティ対策:
ABSPATH
が定義されていない場合は、直接アクセスを防ぐためにスクリプトを終了します。 - 初期化関数: プラグインの初期化処理を行うための関数。
- アクティベーションフック: プラグインが有効化されたときに実行するコードを記述する関数。
- デアクティベーションフック: プラグインが無効化されたときに実行するコードを記述する関数。
- 主要機能: プラグインの主要機能を実装する関数。
- ショートコード: ショートコードを登録し、そのコールバック関数を定義する部分。
このテンプレートを基に、必要な機能を追加してプラグインをカスタマイズしてください。
このコードにトップメニューに管理画面を追加するコードを追加して頂けませんか。
もちろんです!以下に管理画面メニューをトップレベルメニューに追加するコードを組み込んだテンプレートを示します。
メインプラグインファイルの内容
<?php
/*
Plugin Name: My First Plugin
Plugin URI: https://example.com/my-first-plugin
Description: これは私の最初のプラグインです。
Version: 1.0.0
Author: あなたの名前
Author URI: https://example.com
License: GPL2
*/
// セキュリティ対策:直接アクセスを防ぐ
if ( !defined('ABSPATH') ) {
exit;
}
// プラグインの初期化関数
function my_first_plugin_init() {
// ここにプラグインの初期化コードを記述します
// 例:カスタム投稿タイプの登録
}
// プラグインのアクティベーションフック
function my_first_plugin_activate() {
// プラグインがアクティベートされたときに実行するコードをここに記述します
// 例:オプションのデフォルト値を設定する
add_option('my_first_plugin_option', 'default_value');
}
// プラグインのデアクティベーションフック
function my_first_plugin_deactivate() {
// プラグインがデアクティベートされたときに実行するコードをここに記述します
// 例:オプションを削除する
delete_option('my_first_plugin_option');
}
// 管理画面メニューを追加する関数
function my_first_plugin_admin_menu() {
add_menu_page(
'My First Plugin Settings', // ページタイトル
'My First Plugin', // メニュータイトル
'manage_options', // 権限
'my-first-plugin', // メニューのスラッグ
'my_first_plugin_settings_page', // 表示する関数
'dashicons-admin-generic', // アイコン
80 // メニューの位置
);
}
// 管理画面ページの内容を表示する関数
function my_first_plugin_settings_page() {
?>
<div class="wrap">
<h1>My First Plugin Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields('my_first_plugin_options_group');
do_settings_sections('my-first-plugin');
submit_button();
?>
</form>
</div>
<?php
}
// 設定オプションを登録する関数
function my_first_plugin_settings_init() {
register_setting('my_first_plugin_options_group', 'my_first_plugin_option');
add_settings_section(
'my_first_plugin_settings_section',
'General Settings',
'my_first_plugin_settings_section_callback',
'my-first-plugin'
);
add_settings_field(
'my_first_plugin_option_field',
'Sample Option',
'my_first_plugin_option_field_callback',
'my-first-plugin',
'my_first_plugin_settings_section'
);
}
// セクションの説明を表示するコールバック関数
function my_first_plugin_settings_section_callback() {
echo '設定をカスタマイズできます。';
}
// フィールドの内容を表示するコールバック関数
function my_first_plugin_option_field_callback() {
$value = get_option('my_first_plugin_option', 'default_value');
echo '<input type="text" name="my_first_plugin_option" value="' . esc_attr($value) . '" />';
}
// アクションフックを追加
add_action('init', 'my_first_plugin_init');
add_action('admin_menu', 'my_first_plugin_admin_menu');
add_action('admin_init', 'my_first_plugin_settings_init');
// アクティベーション・デアクティベーションフックを追加
register_activation_hook(__FILE__, 'my_first_plugin_activate');
register_deactivation_hook(__FILE__, 'my_first_plugin_deactivate');
// プラグインの主要機能を追加する関数
function my_first_plugin_main_function() {
// ここにプラグインの主要な機能を記述します
// 例:ショートコードの登録
add_shortcode('my_first_shortcode', 'my_first_shortcode_function');
}
// ショートコードのコールバック関数
function my_first_shortcode_function($atts) {
// ショートコードが呼び出されたときに実行するコードをここに記述します
return "Hello, this is my first shortcode!";
}
// アクションフックを追加
add_action('wp_loaded', 'my_first_plugin_main_function');
?>
管理画面メニューの説明
-
add_menu_page(): 管理画面にトップレベルのメニューを追加します。
- ページタイトル: メニューが選択されたときに表示されるページのタイトル。
- メニュータイトル: 管理画面メニューに表示されるタイトル。
- 権限: このメニュー項目を表示するために必要なユーザー権限。
- メニューのスラッグ: メニュー項目のスラッグ。ページを識別するための一意の文字列。
- 表示する関数: メニューがクリックされたときに表示されるページの内容を出力する関数。
- アイコン: メニューのアイコン。
- メニューの位置: メニューの表示位置。
- settings_fields(): オプションページに必要なhiddenフィールドを生成します。
- do_settings_sections(): 指定されたページのセクションとフィールドを出力します。
- submit_button(): 送信ボタンを生成します。
このコードを追加することで、WordPressの管理画面にトップレベルメニューが追加され、そのメニューからプラグインの設定ページにアクセスできるようになります。
このコードの「 my_first_plugin_settings_page() 」内に、テキストが入力できるフォームをひとつと、それをデータベースに保存するコードを追記して頂けませんか。
もちろんです!以下に、テキスト入力フォームを追加し、その値をデータベースに保存するためのコードを追記します。
メインプラグインファイルの内容
<?php
/*
Plugin Name: My First Plugin
Plugin URI: https://example.com/my-first-plugin
Description: これは私の最初のプラグインです。
Version: 1.0.0
Author: あなたの名前
Author URI: https://example.com
License: GPL2
*/
// セキュリティ対策:直接アクセスを防ぐ
if ( !defined('ABSPATH') ) {
exit;
}
// プラグインの初期化関数
function my_first_plugin_init() {
// ここにプラグインの初期化コードを記述します
// 例:カスタム投稿タイプの登録
}
// プラグインのアクティベーションフック
function my_first_plugin_activate() {
// プラグインがアクティベートされたときに実行するコードをここに記述します
// 例:オプションのデフォルト値を設定する
add_option('my_first_plugin_option', 'default_value');
}
// プラグインのデアクティベーションフック
function my_first_plugin_deactivate() {
// プラグインがデアクティベートされたときに実行するコードをここに記述します
// 例:オプションを削除する
delete_option('my_first_plugin_option');
}
// 管理画面メニューを追加する関数
function my_first_plugin_admin_menu() {
add_menu_page(
'My First Plugin Settings', // ページタイトル
'My First Plugin', // メニュータイトル
'manage_options', // 権限
'my-first-plugin', // メニューのスラッグ
'my_first_plugin_settings_page', // 表示する関数
'dashicons-admin-generic', // アイコン
80 // メニューの位置
);
}
// 管理画面ページの内容を表示する関数
function my_first_plugin_settings_page() {
?>
<div class="wrap">
<h1>My First Plugin Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields('my_first_plugin_options_group');
do_settings_sections('my-first-plugin');
submit_button();
?>
</form>
</div>
<?php
}
// 設定オプションを登録する関数
function my_first_plugin_settings_init() {
register_setting('my_first_plugin_options_group', 'my_first_plugin_option');
register_setting('my_first_plugin_options_group', 'my_first_plugin_text_input'); // テキスト入力フィールドのオプションを登録
add_settings_section(
'my_first_plugin_settings_section',
'General Settings',
'my_first_plugin_settings_section_callback',
'my-first-plugin'
);
add_settings_field(
'my_first_plugin_option_field',
'Sample Option',
'my_first_plugin_option_field_callback',
'my-first-plugin',
'my_first_plugin_settings_section'
);
add_settings_field(
'my_first_plugin_text_input_field',
'Text Input',
'my_first_plugin_text_input_field_callback',
'my-first-plugin',
'my_first_plugin_settings_section'
);
}
// セクションの説明を表示するコールバック関数
function my_first_plugin_settings_section_callback() {
echo '設定をカスタマイズできます。';
}
// フィールドの内容を表示するコールバック関数
function my_first_plugin_option_field_callback() {
$value = get_option('my_first_plugin_option', 'default_value');
echo '<input type="text" name="my_first_plugin_option" value="' . esc_attr($value) . '" />';
}
// テキスト入力フィールドのコールバック関数
function my_first_plugin_text_input_field_callback() {
$value = get_option('my_first_plugin_text_input', '');
echo '<input type="text" name="my_first_plugin_text_input" value="' . esc_attr($value) . '" />';
}
// アクションフックを追加
add_action('init', 'my_first_plugin_init');
add_action('admin_menu', 'my_first_plugin_admin_menu');
add_action('admin_init', 'my_first_plugin_settings_init');
// アクティベーション・デアクティベーションフックを追加
register_activation_hook(__FILE__, 'my_first_plugin_activate');
register_deactivation_hook(__FILE__, 'my_first_plugin_deactivate');
// プラグインの主要機能を追加する関数
function my_first_plugin_main_function() {
// ここにプラグインの主要な機能を記述します
// 例:ショートコードの登録
add_shortcode('my_first_shortcode', 'my_first_shortcode_function');
}
// ショートコードのコールバック関数
function my_first_shortcode_function($atts) {
// ショートコードが呼び出されたときに実行するコードをここに記述します
return "Hello, this is my first shortcode!";
}
// アクションフックを追加
add_action('wp_loaded', 'my_first_plugin_main_function');
?>
追加した内容の説明
- register_setting('my_first_plugin_options_group', 'my_first_plugin_text_input'): テキスト入力フィールドのオプションを登録します。
- add_settings_field('my_first_plugin_text_input_field', 'Text Input', 'my_first_plugin_text_input_field_callback', 'my-first-plugin', 'my_first_plugin_settings_section'): テキスト入力フィールドを設定ページに追加します。
- my_first_plugin_text_input_field_callback(): テキスト入力フィールドを表示するコールバック関数。フォームで入力された値をデータベースに保存します。
このコードにより、管理画面のプラグイン設定ページにテキスト入力フィールドが追加され、その値がデータベースに保存されます。保存された値はget_option('my_first_plugin_text_input')
で取得できます。
まとめ
今回環境を用意していなかったため実際の動作確認まで実行していませんが、 以前の ChatGPT 、 Bard に比べると格段に強化されたコードを返してくれていると感じます。
特に管理画面の作り方等はコールバック関数を用意したりと適切に作成できているのではないでしょうか。
2024/06/18 追記
検証環境が用意できたので実際にコードの検証を行ってみました。
どのコードもエラーなく実行でき、最終的にテキストボックスを作ったプラグイン画面が実装され実際に設定の保存も実行できたことを確認しています。実行可能なコードを書き出してくれることで実用性は増したことが分かりました。
-- 追記ここまで --
また、以前の ChatGPT ( GPT-3.5 )の場合はコードを追記してくれと頼んでもコード全体を書き直したりはしませんでしたが、適切にコードを追加して書き直し、全体のコードを返してくれるので初心者でもそれなりのコードを書くための参考にできるのではないかと感じています。ペアプロ役くらいには十分なれそうですね。
今後の成長が楽しみになってきたのは言うまでもありません。