我想保存输入数据的值并检索它。我刚开始创建WordPress插件,但我不知道如何从表单中获取值。我看过一些插件开发教程,但我无法正确开发它。这是我的代码:
/*
Plugin Name: My Custorm Form
Plugin URI: https://abcd.com
Description: New plugin
Version: 0.1
Author: RB
*/
function my_plugin_settings()
{
add_menu_page( \'My Plugin\',
\'myCust Form\',
\'administrator\',
\'insert-my-plugin_bro\',
\'my_plugin_settings_page\',
\'dashicons-translation\',
\'60\'
);
}
add_action(\'admin_menu\', \'my_plugin_settings\');
function my_plugin_options()
{
register_setting(\'my-form-group\',\'user_input_name\');
}
add_action(\'admin_init\', \'my_plugin_options\');
function my_plugin_settings_page()
{
echo "<h1>My Plugin Settings</h1>";
?>
<div class="wrap">
<form action="options.php" method="post">
<?php settings_fields(\'my-form-group\'); ?>
<b>Enter a name to display:</b><br/>
<input type="text" name="default-image-url" value="" >
<?php submit_button(); ?>
</form>
</div>
<?php
}
?>
SO网友:hashtagerrors
你就快到了。我修改了你的代码。您可以尝试以下操作:
<?php
add_action(\'admin_menu\', \'my_plugin_create_menu\');
function my_plugin_create_menu() {
//create new top-level menu
add_menu_page( \'My Plugin\',
\'myCust Form\',
\'administrator\',
\'insert-my-plugin_bro\',
\'my_plugin_settings_page\',
\'dashicons-translation\',
\'60\'
);
//call register settings function
add_action( \'admin_init\', \'register_my_plugin_settings\' );
}
function register_my_plugin_settings() {
//register our settings
register_setting( \'my-plugin-settings-group\', \'display_name\' );
}
function my_plugin_settings_page() {
?>
<div class="wrap">
<h1>My Plugin Settings</h1>
<form method="post" action="options.php">
<?php settings_fields( \'my-plugin-settings-group\' ); ?>
<?php do_settings_sections( \'my-plugin-settings-group\' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Enter a name to display</th>
<td><input type="text" name="display_name" value="<?php echo esc_attr( get_option(\'display_name\') ); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php } ?>
Wordpress还有一个文档
this . 应该对你有帮助