質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

1回答

1179閲覧

WordPressの自作プラグインで、管理画面でDB情報を編集するときのページ遷移でエラー

mikeko0901

総合スコア227

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2022/02/20 14:34

編集2022/02/20 14:41

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()を呼び出す というようにページの中身を変えようと思いました。
・・・方法についてご教示いただけますと幸いです。

よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

/* 管理画面表示部:編集ページ */ if(isset($_GET["edit"])) { function testCrudEdit() { add_action('load_toplevel_page_test-crud-plugin', 'testCrudEdit'); } }

部分の処理を、function testCrudPluginPage()中に記載したらうまくいきました。
以下、test-crud-plugin.phpの全コードです。

test-crud-plugin>test-crud-plugin.php

<?php /* Plugin Name: TestCrudPlugin Plugin URI: Description: 試験用のアプリ Version: 1.0 Author: AyakoIto */ require_once(plugin_dir_path(__FILE__) . 'test-crud-edit.php'); add_action('admin_menu', 'testCrudPluginMenu'); /* 管理画面呼び出し manage_options→administratorにすると権限でアクセスできなくなる*/ function testCrudPluginMenu() { add_menu_page('テストメニューだよ','テストメニュー','manage_options', 'test-crud-plugin', 'testCrudPluginPage' , 'dashicons-buddicons-replies'); } /* 管理画面表示部 */ function testCrudPluginPage() { //editページ if(isset($_GET["edit"])) { testCrudEdit(); } else { //リストページ //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> <?php print($_REQUEST['page']); ?> <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=<?php print($record['id']); ?>'">編集</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 } }

投稿2022/02/20 23:49

mikeko0901

総合スコア227

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問