5
2

More than 1 year has passed since last update.

Bard に WordPress のプラグインを書いてもらってみた

Posted at

Google Bard が使えるようになったので、 ChatGPT にやってもらった同じ内容を投げてみた

前回 ChatGPT に WordPress のプラグインを作ってとお願いしてやってみました。

今回 Bard が使えるようになったので、まったく同じ質問でプラグインを作ってもらってみました。

以下、実際のやり取り

WordPress のプラグインを新規作成する際のテンプレートのコードを書いてくれませんか。

はい、WordPress プラグインの新規作成のテンプレートコードは次のとおりです。

<?php
/**
 * Plugin Name: Your Plugin Name
 * Description: A short description of your plugin.
 * Version: 1.0.0
 * Author: Your Name
 * Author URI: https://yourwebsite.com
 * License: GPLv2 or later
 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Register the plugin activation hook.
 */
register_activation_hook( __FILE__, 'your_plugin_activate' );

/**
 * Register the plugin deactivation hook.
 */
register_deactivation_hook( __FILE__, 'your_plugin_deactivate' );

/**
 * Register the plugin uninstall hook.
 */
register_uninstall_hook( __FILE__, 'your_plugin_uninstall' );

/**
 * Plugin activation hook callback.
 */
function your_plugin_activate() {
    // Do something when the plugin is activated.
}

/**
 * Plugin deactivation hook callback.
 */
function your_plugin_deactivate() {
    // Do something when the plugin is deactivated.
}

/**
 * Plugin uninstall hook callback.
 */
function your_plugin_uninstall() {
    // Do something when the plugin is uninstalled.
}

/**
 * Plugin init hook callback.
 */
function your_plugin_init() {
    // Do something when WordPress is initialized.
}

/**
 * Plugin admin_enqueue_scripts hook callback.
 */
function your_plugin_admin_enqueue_scripts() {
    // Enqueue scripts and styles for the admin area.
}

/**
 * Plugin front_enqueue_scripts hook callback.
 */
function your_plugin_front_enqueue_scripts() {
    // Enqueue scripts and styles for the front end.
}

/**
 * Plugin wp_loaded hook callback.
 */
function your_plugin_wp_loaded() {
    // Do something when WordPress is loaded.
}

これは単なるテンプレートであり、独自のニーズに合わせて変更する必要がある場合があります。

このコードにトップメニューに管理画面を追加するコードを追加して頂けませんか。

はい、コードにトップメニューに管理画面を追加するコードを追加しました。

<?php
/**
 * Plugin Name: Your Plugin Name
 * Description: A short description of your plugin.
 * Version: 1.0.0
 * Author: Your Name
 * Author URI: https://yourwebsite.com
 * License: GPLv2 or later
 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Register the plugin activation hook.
 */
register_activation_hook( __FILE__, 'your_plugin_activate' );

/**
 * Register the plugin deactivation hook.
 */
register_deactivation_hook( __FILE__, 'your_plugin_deactivate' );

/**
 * Register the plugin uninstall hook.
 */
register_uninstall_hook( __FILE__, 'your_plugin_uninstall' );

/**
 * Plugin activation hook callback.
 */
function your_plugin_activate() {
    // Do something when the plugin is activated.
}

/**
 * Plugin deactivation hook callback.
 */
function your_plugin_deactivate() {
    // Do something when the plugin is deactivated.
}

/**
 * Plugin uninstall hook callback.
 */
function your_plugin_uninstall() {
    // Do something when the plugin is uninstalled.
}

/**
 * Plugin init hook callback.
 */
function your_plugin_init() {
    // Do something when WordPress is initialized.
}

/**
 * Plugin admin_enqueue_scripts hook callback.
 */
function your_plugin_admin_enqueue_scripts() {
    // Enqueue scripts and styles for the admin area.
}

/**
 * Plugin front_enqueue_scripts hook callback.
 */
function your_plugin_front_enqueue_scripts() {
    // Enqueue scripts and styles for the front end.
}

/**
 * Plugin wp_loaded hook callback.
 */
function your_plugin_wp_loaded() {
    // Do something when WordPress is loaded.

    // トップメニューに管理画面を追加する
    add_menu_page(
        'あなたのプラグインのタイトル',
        'あなたのプラグインのタイトル',
        'manage_options',
        'your_plugin_slug',
        'your_plugin_admin_page'
    );
}

/**
 * 管理画面のページを出力する関数
 */
function your_plugin_admin_page() {
    ?>
    <h1>あなたのプラグインのタイトル</h1>
    <p>これは、あなたのプラグインの管理画面です。</p>
    <?php
}

これで、トップメニューに「あなたのプラグインのタイトル」という管理画面が追加されました。

このコードの「your_plugin_admin_page()」関数内に、テキストが入力できるフォームをひとつと、それをデータベースに保存するコードを追記して頂けませんか。

はい、コードに「your_plugin_admin_page()」関数内にテキストが入力できるフォームをひとつと、それをデータベースに保存するコードを追加しました。

<?php
/**
 * Plugin Name: Your Plugin Name
 * Description: A short description of your plugin.
 * Version: 1.0.0
 * Author: Your Name
 * Author URI: https://yourwebsite.com
 * License: GPLv2 or later
 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Register the plugin activation hook.
 */
register_activation_hook( __FILE__, 'your_plugin_activate' );

/**
 * Register the plugin deactivation hook.
 */
register_deactivation_hook( __FILE__, 'your_plugin_deactivate' );

/**
 * Register the plugin uninstall hook.
 */
register_uninstall_hook( __FILE__, 'your_plugin_uninstall' );

/**
 * Plugin activation hook callback.
 */
function your_plugin_activate() {
    // Do something when the plugin is activated.
}

/**
 * Plugin deactivation hook callback.
 */
function your_plugin_deactivate() {
    // Do something when the plugin is deactivated.
}

/**
 * Plugin uninstall hook callback.
 */
function your_plugin_uninstall() {
    // Do something when the plugin is uninstalled.
}

/**
 * Plugin init hook callback.
 */
function your_plugin_init() {
    // Do something when WordPress is initialized.
}

/**
 * Plugin admin_enqueue_scripts hook callback.
 */
function your_plugin_admin_enqueue_scripts() {
    // Enqueue scripts and styles for the admin area.
}

/**
 * Plugin front_enqueue_scripts hook callback.
 */
function your_plugin_front_enqueue_scripts() {
    // Enqueue scripts and styles for the front end.
}

/**
 * Plugin wp_loaded hook callback.
 */
function your_plugin_wp_loaded() {
    // Do something when WordPress is loaded.

    // トップメニューに管理画面を追加する
    add_menu_page(
        'あなたのプラグインのタイトル',
        'あなたのプラグインのタイトル',
        'manage_options',
        'your_plugin_slug',
        'your_plugin_admin_page'
    );
}

/**
 * 管理画面のページを出力する関数
 */
function your_plugin_admin_page() {
    ?>
    <h1>あなたのプラグインのタイトル</h1>
    <p>これは、あなたのプラグインの管理画面です。</p>
    <form action="<?php echo admin_url( 'admin-post.php?action=your_plugin_action' ); ?>" method="post">
        <input type="text" name="your_plugin_text_field">
        <input type="submit" value="送信">
    </form>
    <?php
}

/**
 * テキストをデータベースに保存する関数
 */
function your_plugin_save_text() {
    if ( isset( $_POST['your_plugin_text_field'] ) ) {
        $text = esc_attr( $_POST['your_plugin_text_field'] );
        $args = [
            'post_title' => 'あなたのプラグインのタイトル',
            'post_content' => $text,
            'post_status' => 'publish',
        ];
        $post_id = wp_insert_post( $args );
    }
}

add_action( 'admin_post_your_plugin_action', 'your_plugin_save_text' );

これで、管理画面にテキストを入力できるフォームが追加され、入力されたテキストはデータベースに保存されます。

まとめ

まず、管理画面にメニューを追加するadd_actionがないため実際にはメニューが追加されませんでした。

ChatGPT との違いはプラグインのテンプレートの作成ではあるが、 Bard の方はactivatedeactivateなど必要と思われる関数を最初から組み込んで生成しています。 ChatGPT の場合 WordPress プラグインの基本の基本となるコードのみの生成でしたね。
もうひとつの違いは、 ChatGPT の場合生成されたコードに対し WordPress プラグインのコードを作成する時のコードの説明や注意事項等を文章で提示してくれました。 Bard の場合簡易的な説明や注意事項が提示されるだけですので、まだコードを書くことにそれほど慣れていないエンジニアの場合は ChatGPT 、すでにバリバリコードを書いていて補完的に AI を利用したい場合は Bard でもいいかなと言う感じですかね。

なお、 Bard に追加でコードの修正をお願いしましたが、initadd_menu_pageを組み込んだりトップメニューに表示されていないのにサブメニューを追加してみたりと完全なコード生成には至りませんでした。何度も修正を依頼していると元のコードを忘れてfunctions.phpに追加とかになってしまってそれ以上修正もできなくなりましたので、何度もやり取りを続けながらプラグインを完成形まで持っていくのは難しいかなと思います。

5
2
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
5
2