为了实现自动化,而不是像这样列出一长串代码
<td valign="top"width="50%"><p><label for="Facebook"><strong>Facebook URL</strong></label><input type="text" name="Facebook" id="Facebook" size="52" value="<?php echo get_option(\'Facebook_url\'); ?>"/></p></td></tr><tr>
我想从PHP中回显所有这些内容以生成页面。我已经构建了一个包含所有社交网络、推特/脸书等的数组。截至目前,它可以读取保存的设置,但不允许保存新的输入。
function display_settings_fields($value,$key){
$keyurl = $key.\'_url\';
echo "The key $key has the value $value<br />";
echo \'<td valign="top"width="50%"><p><label for="\'.$key.\'"><strong>\'.$key .\'URL</strong></label><input type="text" name="\'.$key.\'" id="\'.$key.\'" size="52" value="\';
echo get_option(\'\'.$keyurl.\'\');
echo \'"/></p></td></tr><tr>\';
}
$social_icon_set = array(
"Twitter" =>"$twitter_url",
"Facebook" =>"$facebook_url",
"GooglePlus" =>"$googleplus_url",
"Yelp" =>"$yelp_url",
"Youtube" =>"$youtube_url",
"Vimeo" =>"$vimeo_url",
"Flickr" =>"$flickr_url",
"Tumblr" =>"$tumblr_url",
"Picplz" =>"$picplz_url",
"Foursquare" =>"$foursquare_url",
"RSS" =>"$rss_url",
"Behance" =>"$behance_url",
"Goodreads" =>"$goodreads_url",
"Github" =>"$github_url",
"Formspring" =>"$formspring_url",
"Myspace" =>"$mypsace_url",
"Klout" =>"$klout_url",
"Diaspora" =>"$diaspora_url",
"LinkedIn" =>"$linkedin_url"
);
array_walk($social_icon_set,"display_settings_fields");
?>
“echo get\\u选项(\'.$keyurl.\');”会出现问题。现在,它会回显正确的值,即为其保存的值,但它不允许保存在文本字段中编辑的任何内容。这甚至可以在其可保存的位置回显/打印出来吗。
(这主要是PHP,但如果有其他解决方法,因为它使用wordpress的get\\u选项,我愿意接受这些想法)
SO网友:fuxia
使用Settings API. 一看my latest plugin 可以帮助您了解如何注册save
字段的函数。最相关摘录:
/**
* Register custom settings for the fields.
*
* @see save_settings()
* @see print_input_field()
* @return void
*/
public function add_contact_fields()
{
register_setting(
\'general\',
$this->option_name,
array ( $this, \'save_settings\' )
);
foreach ( $this->fields as $type => $desc )
{
$handle = $this->option_name . "_$type";
$args = array (
\'label_for\' => $handle,
\'type\' => $type
);
$callback = array ( $this, \'print_input_field\' );
add_settings_field(
$handle,
$desc,
$callback,
\'general\',
\'default\',
$args
);
}
}
/**
* Callback for \'register_setting()\'.
*
* @see add_contact_fields()
* @param array $settings
* @return array $settings
*/
public function save_settings( array $settings = array () )
{
$default = get_option( $this->option_name );
$settings = array_map( \'trim\', $settings );
$settings = $this->prepare_mail_save( $settings, $default );
$settings = $this->prepare_phone_save( $settings );
return $settings;
}
还有…欢迎使用WordPress堆栈交换!