WordPressの管理画面に、独自で追加したDB情報を編集するメニューを追加したく、
自作プラグインを作成しました。
DB情報の一覧を、WordPressの管理画面内に表示するところまではできたのですが、
編集用の画面を表示させるところでうまくできません…
調べながら試行錯誤していますので、見当違いのことをしているかもしれませんが、
アドバイスいただけますと幸いです。
やりたいこと
・管理画面に「テストメニュー」を追加しました。
・「テストメニュー」をクリックすると、申込一覧が出てきて、独自で追加したテーブルの一覧を表示。(wp-admin/admin.php?page=test-crud-plugin)
・「編集」をクリックすると編集ページを表示させる
(wp-admin/admin.php?page=test-crud-plugin&edit)
出ているエラー
しかし、「編集」をクリックすると以下のエラーが出ます。
『
Fatal error: Cannot redeclare testCrudEdit() (previously declared in /var/www/html/wp-content/plugins/test-crud-plugin/test-crud-edit.php:5) in /var/www/html/wp-content/plugins/test-crud-plugin/test-crud-plugin.php on line 68 Notice: is_embed が誤って呼び出されました。条件付きクエリータグは、クエリーが実行される前には機能しません。それ以前では、常に false を返します。
』
フォルダ構成
wp-content └plugins └test-crud-plugin ←自作プラグイン └test-crud-plugin.php └test-crud-edit.php
コード
test-crud-plugin>test-crud-plugin.php
<?php /* Plugin Name: TestCrudPlugin Plugin URI: Description: 試験用のアプリ Version: 1.0 Author: Ito */ require_once(plugin_dir_path(__FILE__) . 'test-crud-edit.php'); add_action('admin_menu', 'testCrudPluginMenu'); /* 管理画面呼び出し */ function testCrudPluginMenu() { add_menu_page('テストメニューだよ','テストメニュー','manage_options', 'test-crud-plugin', 'testCrudPluginPage' , 'dashicons-buddicons-replies'); } /* 管理画面表示部:リストページ */ function testCrudPluginPage() { //wp_testテーブルの商品数を取得 global $wpdb; $sql = "SELECT * FROM wp_test;"; $records = $wpdb->get_results($sql, ARRAY_A); ?> <div class="wrap"> <h2>申込一覧</h2> <div class="postbox "> <button>新規追加</button> <button onclick="location.href='?page=test-crud-plugin'">申込一覧TOP</button> <div class="inside"> <div class="main"> <?php var_dump($records); ?> <table class="wp-list-table widefat fixed striped table-view-list posts"> <thead> <tr> <th></th> <th>id</th> <th>お名前</th> <th>メール</th> <th>日時</th> </tr> </thead> <tbody> <?php if($records): foreach($records as $record): ?> <tr> <td><button onclick="location.href='?page=test-crud-plugin&edit'">編集</button></td> <td><?php print($record["id"]); ?></td> <td><?php print($record["name"]); ?></td> <td><?php print($record["email"]); ?></td> <td><?php print($record["created_at"]); ?></td> </tr> <?php endforeach; endif; ?> </tbody> </table> </div> </div> </div> </div> <?php } /* 管理画面表示部:編集ページ */ if(isset($_GET["edit"])) { function testCrudEdit() { add_action('load_toplevel_page_test-crud-plugin', 'testCrudEdit'); } }
test-crud-plugin>test-crud-edit.php
<?php /* 管理画面表示部 */ function testCrudEdit() { ?> <div class="wrap"> <h2>編集ページ</h2> </div> <?php }
編集ページは「location.href='?page=test-crud-plugin&edit」というように、
editというパラメータを付け、editパラメータがついていたら、testCrudEdit()を呼び出す というようにページの中身を変えようと思いました。
・・・方法についてご教示いただけますと幸いです。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。