Php Wordpress-在管理选项页面中提交表单时将值保存到数组
我几乎完成了一个成功的第一次插件,但不幸的是,我无法将更新后的值提交到我在register\u activation\u hook上创建的数组 我需要一些关于Wordpress如何处理所有脏活的见解——或者一篇$\u文章会是一种更好的方式吗 下面您将看到我的admin.php,它处理所有与管理员相关的内容Php Wordpress-在管理选项页面中提交表单时将值保存到数组,php,forms,wordpress,submit,Php,Forms,Wordpress,Submit,我几乎完成了一个成功的第一次插件,但不幸的是,我无法将更新后的值提交到我在register\u activation\u hook上创建的数组 我需要一些关于Wordpress如何处理所有脏活的见解——或者一篇$\u文章会是一种更好的方式吗 下面您将看到我的admin.php,它处理所有与管理员相关的内容 <?php // Meaning of abbreviations: // clsc = Custom login shortcode // Runs when plugin is
<?php
// Meaning of abbreviations:
// clsc = Custom login shortcode
// Runs when plugin is activated
register_activation_hook( PLUGIN_MAIN_FILE, 'clsc_install');
// Create new database fields
function clsc_install() {
$clsc_options = array(
'Login_link' => '/log-in/',
'Login_string' => __('Log in', 'clsc'),
'Login_class' => '', // Default is empty to inherit theme styles
'Logout_link' => wp_logout_url( home_url()),
'Logout_string' => __('Log out', 'clsc'),
'Logout_class' => '', // Default is empty to inherit theme styles
'Account_link' => '/my-account/',
'Account_string' => __('My Account', 'clsc'),
'Account_class' => '' // Default is empty to inherit theme styles
);
add_option('clsc_options_array', $clsc_options, '', 'yes');
}
// Create admin option page
function add_clsc_option_page() {
add_options_page(
'Custom Login', // The text to be displayed in the title tag
'Custom Login', // The text to be used for the menu
'administrator', // The capability required to display this menu
'custom-login-shortcodes', // The unique slug name to refer to this menu
'clsc_html_page'); // The function to output the page content
}
/* Call the html code */
add_action('admin_menu', 'add_clsc_option_page');
// Enqueue admin styles and scripts
function clsc_enqueue_scripts() {
global $wpdb;
$screen = get_current_screen();
if ( $screen->id != 'settings_page_custom-login-shortcodes' ) {
return; // exit if incorrect screen id
}
wp_enqueue_style( 'brokenfruit-shortcodes-styles', plugins_url( 'admin/css/admin_styles.css', dirname(__FILE__) ) );
wp_enqueue_style( 'bootstrap', plugins_url('admin/css/bootstrap.css', dirname(__FILE__) ) );
wp_enqueue_script('admin_js_bootstrap_hack', plugins_url('admin/scripts/bootstrap-hack.js', dirname(__FILE__) ) );
}
add_action('admin_enqueue_scripts', 'clsc_enqueue_scripts' );
// Build admin interface
function clsc_html_page(){
$options = get_option('clsc_options_array');
?>
<form method="post" action="options.php">
<?php wp_nonce_field('update-options'); ?>
<div class="bootstrap-wrapper">
<div class="row">
<div class="col-md-12">
<h1><?php _e('Custom Login Shortcode','clsc'); ?></h1>
<p><?php _e('To use for shortcode:','clsc'); ?><br/><span class="shortcode-preview">[custom_login]</span></p>
</div>
</div>
<div class="row" id="login-content">
<div class="col-md-4">
<h5><?php _e('Log in link:','clsc'); ?></h5>
<input placeholder="<?php _e('Example: /log-in/', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Login_link']; ?>" />
</div>
<div class="col-md-4">
<h5><?php _e('Log in string:','clsc'); ?></h5>
<input placeholder="<?php _e('Example: Log in', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Login_string']; ?>" />
</div>
<div class="col-md-4">
<h5><?php _e('Log in class:','clsc'); ?></h5>
<input placeholder="<?php _e('Example: login_style', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Login_class']; ?>" />
</div>
</div>
<div class="row top-buffer" id="logout-content">
<div class="col-md-4">
<h5><?php _e('Log out link:','clsc'); ?></h5>
<input placeholder="<?php _e('Example: /log-out/', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Logout_link']; ?>" />
</div>
<div class="col-md-4">
<h5><?php _e('Log out string:','clsc'); ?></h5>
<input placeholder="<?php _e('Example: Log out', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Logout_string']; ?>" />
</div>
<div class="col-md-4">
<h5><?php _e('Log out class:','clsc'); ?></h5>
<input placeholder="<?php _e('Example: logout_style', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Logout_class']; ?>" />
</div>
</div>
<div class="row top-buffer" id="account-content">
<div class="col-md-4">
<h5><?php _e('Account link:','clsc'); ?></h5>
<input placeholder="<?php _e('Example: /my-account/', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Account_link']; ?>" />
</div>
<div class="col-md-4">
<h5><?php _e('Account string:','clsc'); ?></h5>
<input placeholder="<?php _e('Example: My Account', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Account_string']; ?>" />
</div>
<div class="col-md-4">
<h5><?php _e('Account class:','clsc'); ?></h5>
<input placeholder="<?php _e('Example: account_style', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Account_class']; ?>" />
</div>
</div>
<div class="row top-buffer">
<div class="col-md-12">
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="clsc_options_array" />
<input class="btn btn-primary top-buffer" type="submit" value="<?php _e('Save Changes') ?>" />
</div>
</div>
</div>
</form>
<?php
}
?>
[自定义登录]
您可以在使用表单时继续使用POST方法,因此在提交时使用update\u option
更新您的选项值。
参考网址:
如何使用更新选项
:
function my_option_function() {
update_option('option_value','none');
}
add_action('update_option', 'my_option_function'); ?>
有关更多指导,请参阅:
我有更新功能
clsc\u html\u page()
并添加了操作'admin\u init'
来保存注册设置字段
/**
Plugin Name: clsc
Plugin URI: http://google.com/
Description: this is description
Version: 1.0
Author: authourname
Author URI: http://authoururri.com/
License: GPLv2 or later
Text Domain: text domain
*/
// Runs when plugin is activated
register_activation_hook( PLUGIN_MAIN_FILE, 'clsc_install');
// Create new database fields
function clsc_install() {
$clsc_options = array(
'Login_link' => '/log-in/',
'Login_string' => __('Log in', 'clsc'),
'Login_class' => '', // Default is empty to inherit theme styles
'Logout_link' => wp_logout_url( home_url()),
'Logout_string' => __('Log out', 'clsc'),
'Logout_class' => '', // Default is empty to inherit theme styles
'Account_link' => '/my-account/',
'Account_string' => __('My Account', 'clsc'),
'Account_class' => '' // Default is empty to inherit theme styles
);
add_option('clsc_options_array', $clsc_options, '', 'yes');
}
add_action('admin_init','admin_init_register_setting');
function admin_init_register_setting()
{
// register your plugins settings
/*register_setting('wp_plugin_template-group', 'Account_class');
register_setting('wp_plugin_template-group', 'Account_string');
register_setting('wp_plugin_template-group', 'Account_link');
register_setting('wp_plugin_template-group', 'Logout_class');
register_setting('wp_plugin_template-group', 'Logout_string');
register_setting('wp_plugin_template-group', 'Logout_link');
register_setting('wp_plugin_template-group', 'Login_class');
register_setting('wp_plugin_template-group', 'Login_string');
register_setting('wp_plugin_template-group', 'Login_link'); */
register_setting('wp_plugin_template-group', 'clsc_options_array');
}
// Create admin option page
function add_clsc_option_page() {
add_options_page(
'Custom Login', // The text to be displayed in the title tag
'Custom Login', // The text to be used for the menu
'administrator', // The capability required to display this menu
'custom-login-shortcodes', // The unique slug name to refer to this menu
'clsc_html_page'); // The function to output the page content
}
/* Call the html code */
add_action('admin_menu', 'add_clsc_option_page');
// Enqueue admin styles and scripts
function clsc_enqueue_scripts() {
global $wpdb;
$screen = get_current_screen();
if ( $screen->id != 'settings_page_custom-login-shortcodes' ) {
return; // exit if incorrect screen id
}
wp_enqueue_style( 'brokenfruit-shortcodes-styles', plugins_url( 'admin/css/admin_styles.css', dirname(__FILE__) ) );
wp_enqueue_style( 'bootstrap', plugins_url('admin/css/bootstrap.css', dirname(__FILE__) ) );
wp_enqueue_script('admin_js_bootstrap_hack', plugins_url('admin/scripts/bootstrap-hack.js', dirname(__FILE__) ) );
}
add_action('admin_enqueue_scripts', 'clsc_enqueue_scripts' );
function clsc_html_page()
{
if(!current_user_can('manage_options'))
{
wp_die(__('You do not have sufficient permissions to access this page.'));
}
?>
<div class="wrap">
<form method="post" action="options.php">
<?php
$options = get_option('clsc_options_array');
@settings_fields('wp_plugin_template-group'); ?>
<?php @do_settings_fields('wp_plugin_template-group'); ?>
<div class="bootstrap-wrapper">
<div class="row">
<div class="col-md-12">
<h1><?php _e('Custom Login Shortcode','clsc'); ?></h1>
<p><?php _e('To use for shortcode:','clsc'); ?><br/><span class="shortcode-preview">[custom_login]</span></p>
</div>
</div>
<div class="row" id="login-content">
<div class="col-md-4">
<h5><?php _e('Log in link:','clsc'); ?></h5>
<input name="clsc_options_array[Login_link]" placeholder="<?php _e('Example: /log-in/', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Login_link']; ?>" />
</div>
<div class="col-md-4">
<h5><?php _e('Log in string:','clsc'); ?></h5>
<input name="clsc_options_array[Login_string]" placeholder="<?php _e('Example: Log in', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Login_string']; ?>" />
</div>
<div class="col-md-4">
<h5><?php _e('Log in class:','clsc'); ?></h5>
<input name="clsc_options_array[Login_class]" placeholder="<?php _e('Example: login_style', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Login_class']; ?>" />
</div>
</div>
<div class="row top-buffer" id="logout-content">
<div class="col-md-4">
<h5><?php _e('Log out link:','clsc'); ?></h5>
<input name="clsc_options_array[Logout_link]" placeholder="<?php _e('Example: /log-out/', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Logout_link']; ?>" />
</div>
<div class="col-md-4">
<h5><?php _e('Log out string:','clsc'); ?></h5>
<input name="clsc_options_array[Logout_string]" placeholder="<?php _e('Example: Log out', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Logout_string']; ?>" />
</div>
<div class="col-md-4">
<h5><?php _e('Log out class:','clsc'); ?></h5>
<input name="clsc_options_array[Logout_class]" placeholder="<?php _e('Example: logout_style', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Logout_class']; ?>" />
</div>
</div>
<div class="row top-buffer" id="account-content">
<div class="col-md-4">
<h5><?php _e('Account link:','clsc'); ?></h5>
<input name="clsc_options_array[Account_link]" placeholder="<?php _e('Example: /my-account/', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Account_link']; ?>" />
</div>
<div class="col-md-4">
<h5><?php _e('Account string:','clsc'); ?></h5>
<input name="clsc_options_array[Account_string]" placeholder="<?php _e('Example: My Account', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Account_string']; ?>" />
</div>
<div class="col-md-4">
<h5><?php _e('Account class:','clsc'); ?></h5>
<input name="clsc_options_array[Account_class]" placeholder="<?php _e('Example: account_style', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Account_class']; ?>" />
</div>
</div>
</div>
<?php @submit_button(); ?>
</form>
</div>
<?php
}
/**
插件名称:clsc
插件URI:http://google.com/
描述:这是描述
版本:1.0
作者:authourname
作者URI:http://authoururri.com/
许可证:GPLv2或更高版本
文本域:文本域
*/
//当插件被激活时运行
注册激活挂钩(插件主文件“clsc安装”);
//创建新的数据库字段
函数clsc_install(){
$clsc_选项=阵列(
“登录链接”=>“/登录/”,
'Login_string'=>'Login'、'clsc'),
'Login_class'=>'',//默认为空以继承主题样式
'Logout\u link'=>wp\u Logout\u url(home\u url()),
“注销字符串”=>“‘注销’、‘clsc’”,
'Logout_class'=>'',//默认为空以继承主题样式
“帐户链接”=>“/my Account/”,
“帐户字符串”=>“‘我的帐户’、‘clsc’”,
“Account_class”=>“”//默认为空以继承主题样式
);
添加_选项('clsc_选项_数组',$clsc_选项','yes');
}
添加操作('admin_init','admin_init_register_setting');
函数管理\初始化\寄存器\设置()
{
//注册你的插件设置
/*注册设置(“wp插件模板组”、“帐户类”);
注册设置(“wp插件模板组”、“帐户字符串”);
注册设置(“wp插件模板组”、“帐户链接”);
注册设置(“wp插件模板组”、“注销类”);
注册设置(“wp插件模板组”、“注销字符串”);
注册设置(“wp插件模板组”、“注销链接”);
注册设置(“wp插件模板组”、“登录类”);
注册设置(“wp插件模板组”、“登录字符串”);
注册设置(“wp插件模板组”、“登录链接”)*/
注册设置(“wp插件模板组”、“clsc选项数组”);
}
//创建管理选项页
函数添加\ clsc\选项\页面(){
添加选项页面(
'自定义登录',//要在标题标记中显示的文本
'自定义登录',//用于菜单的文本
“administrator”,//显示此菜单所需的功能
'自定义登录快捷码',//引用此菜单的唯一slug名称
'clsc_html_page');//输出页面内容的函数
}
/*调用html代码*/
添加操作(“管理”菜单、“添加选项”页面);
//排队管理样式和脚本
函数clsc_enqueue_scripts(){
全球$wpdb;
$screen=获取当前屏幕();
如果($screen->id!='settings\u page\u custom-login-shortcode'){
return;//如果屏幕id不正确,则退出
}
wp_enqueue_style('brokenfruit shortcodes style',plugins_url('admin/css/admin_styles.css',dirname(uu文件));
wp_-enqueue_样式('bootstrap',plugins_-url('admin/css/bootstrap.css',dirname('uuuu-FILE_uu));
wp_enqueue_script('admin_js_bootstrap_hack',plugins_url('admin/scripts/bootstrap hack.js',dirname(uu文件));
}
添加操作(“管理队列脚本”、“clsc队列脚本”);
函数clsc_html_page()
{
如果(!当前用户可以('manage_options'))
{
wp_die(_uu('您没有足够的权限访问此页面');
}
?>
[自定义登录]
这真是太好了!这真的表明Wordpress可以做我所说的脏活!我对设置字段和do_设置字段使用的“@”很好奇。此外,为什么@settings_字段不够?-为什么我们需要做“do_设置_字段”?:)你可以删除“@”。详情请访问