Php 从自定义字段检索数据
我在我的WP站点中创建了一个选项插件,因此我将此代码写入我的文件Php 从自定义字段检索数据,php,wordpress,Php,Wordpress,我在我的WP站点中创建了一个选项插件,因此我将此代码写入我的文件options.php: class MySettingsPage { /** * Holds the values to be used in the fields callbacks */ private $options; /** * Start up */ public function __construct() { add_
options.php
:
class MySettingsPage
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'Champs personnalisés',
'manage_options',
'my-setting-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'my_option_name' );
?>
<div class="wrap">
<h1>Champs personnalisés</h1>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'my_option_group' );
do_settings_sections( 'my-setting-admin' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'my_option_group', // Option group
'my_option_name', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'setting_section_id', // ID
'Mes champs personnalisés', // Title
array( $this, 'print_section_info' ), // Callback
'my-setting-admin' // Page
);
add_settings_field(
'num_tel', // ID
'Numéro de téléphone', // Title
array( $this, 'num_tel_callback' ), // Callback
'my-setting-admin', // Page
'setting_section_id' // Section
);
}
/**
* Print the Section text
*/
public function print_section_info()
{
print 'Renseignez les champs';
}
/**
* Get the settings option array and print one of its values
*/
public function num_tel_callback()
{
printf(
'<input type="text" id="num_tel" name="my_option_name[num_tel]" value="%s" />',
isset( $this->options['num_tel'] ) ? esc_attr( $this->options['num_tel']) : ''
);
}
}
$my_settings_page = new MySettingsPage();
classmysettingspage
{
/**
*保存要在字段回调中使用的值
*/
私人美元期权;
/**
*启动
*/
公共函数构造()
{
添加操作('admin_menu',array('add_plugin_page');
添加操作('admin_init',数组($this,'page_init'));
}
/**
*添加选项页面
*/
公共函数添加插件页面()
{
//此页面将位于“设置”下
添加选项页面(
“设置管理员”,
“个人冠军”,
“管理选项”,
“我的设置管理员”,
数组('创建管理页面')
);
}
/**
*选项页回调
*/
公共函数创建\管理\页面()
{
//设置类属性
$this->options=get_option('my_option_name');
?>
个人冠军
您只需要使用设置字段的ID调用get_option函数
例如:
echo get_option('num_tel');
您只需要使用设置字段的ID调用get_option函数
例如:
echo get_option('num_tel');
我只是找到了解决办法:
$options = get_option('my_option_name');
echo $options['num_tel']
我只是找到了解决办法:
$options = get_option('my_option_name');
echo $options['num_tel']
哦,是的。将所有设置组作为数组获取。哦,是的。将所有设置组作为数组获取。