まず、素朴で愚かなことをしたことを先制的に謝罪したいと思います。私は初心者のプログラマーですが、学ぼうとしています。現在、インターンシップの一環として wordpress 用のプラグインを開発しようとしています。プラグインは、ほとんどの場合必要なことを行いますが、設定ページを希望どおりに機能させることができないようです。プラグインの初期化部分が正しく機能していることはわかっていますが、設定ページの使用に問題があります。具体的には、情報を保存するために_POSTをどこでどのように使用するかはわかりません。また、設定ページから情報を受け取った後、 update_option関数呼び出しを配置する場所もわかりません。ここに私のコードがあります:
add_action( 'admin_menu', 'menu' );
add_action( 'admin_init', 'plugin_admin_init' );
function plugin_admin_init() { // whitelist options
register_setting( 'plugin_options', 'plugin_options', 'plugin_options_validate' );
add_settings_section('plugin_main', 'Main Settings', 'plugin_section_text', 'plugin');
add_settings_field('plugin_text_string', 'REE View Count Settings', 'plugin_setting_string', 'plugin', 'plugin_main');
}
function menu() {
add_options_page( 'REE View Count Options', 'REE View Count', 'manage_options', 'plugin', 'plugin_options_page' );
}
// generates the settings web page
function plugin_options_page() {
echo '<div class="wrap">';
echo '<h2>Options</h2>';
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
// In the line above, I don't know what to put. The file name of the plugin?
// The URL of the settings page on the admin page? Or what I have currently?
// Both the first and last options bring up a 404 page.
settings_fields('plugin_options');
do_settings_sections('plugin');
echo '<input name="Submit" type="submit" value="' . esc_attr_e('Save Changes') . '" />';
echo '</form>';
//update_option('plugin_options', $_POST['plugin_options']);
// Where should I try to update the option? And how?
echo '</div>';
}
//from add_settings_section parameter 3, in plugin_admin_init
function plugin_section_text() {
echo '<p>Main description of this section here.</p>';
}
//Function from 3 paramter of add_settings_field in plugin_admin_init that outputs input HTML
function plugin_setting_string() {
$options = get_option('plugin_options');
echo 'value of options is: ' . $options;
echo "<input id='plugin_text_string' name='plugin_options' size='40' type='text'
value='{$options}' />";
}
//Validation function from register_setting in plugin_admin_init
function plugin_options_validate($input) {
echo "this is input: " . $input;
$newinput = trim($input);
if(!preg_match('/^[0-9]+$/', $newinput)) {
$newinput = 10;
}
update_option('plugin_options', $newinput);
return $newinput;
}
注:実際には、これらの正確な関数/変数名を使用していません。実際のプラグイン名を前に付けています。
手伝って頂けますか?管理者が設定ページに入力した情報を取得し、その情報 (この場合は単一の数字) でデータベースを更新したいと考えています。PHP _POST を使用する必要があることは理解していますが、方法がわかりません。さらに、action="the_file_name.php" を使用すると、送信時に 404 エラーが発生するため、投稿先がわかりません。管理者の送信から取得した情報を後で使用できるようにするには、フォームのアクションはどうすればよいですか? その後、設定を更新するにはどうすればよいですか?update_option をどこに配置すればよいのでしょうか。これがとりとめのない、または漠然としているように思われる場合は、お詫び申し上げます。
それが役立つ場合は、このガイドの助けを借りて、プラグイン自体と同じファイルにこの設定ページを作成しています: http://ottopress.com/2009/wordpress-settings-api-tutorial/
残念ながら、そのガイドには、情報の更新について述べているものは何も見当たらず、ページ自体を作成しているだけです。