wordpress在设置的常规里面添加自定义设置
function set_global_seo() {
	$global_seo = new GlobalSeo();
	$global_seo->setting_fields();
}
add_action( 'admin_init', 'set_global_seo' );
 
class GlobalSeo
{
	public function setting_fields()
	{
		$text_input = [
			'contact' => '联系人',
			'email' => '邮箱',
			'address' => '地址',
			'phone' => '电话',
			'wechat' => '微信',
			'qq' => 'QQ',
			'copyright' => '备案号',
			'seo_title' => 'seo标题',
			'seo_keywords' => 'seo关键字',
		];
		$textarea_input = [ 'seo_description' => 'seo描述' ];
		foreach($text_input as $key => $val)
		{
			$this->sonliss_settings_field($key, $val, 'sonliss_textbox_callback', [$key]);
			$this->sonliss_register_setting($key);
		}
		foreach($textarea_input as $key => $val)
		{
			$this->sonliss_settings_field($key, $val, 'sonliss_textareabox_callback', [$key]);
			$this->sonliss_register_setting($key);
		}
	}
	/**
	 * id ID
	 * title 显示在页面的标题即label
	 * callback 回调
	 * args [] 
	 */
	public function sonliss_settings_field($id, $title, $callback, $args)
	{
		add_settings_field(
			$id,
			$title,
			[ $this, $callback ],
			'general', // general, reading, writing, discussion, media
			'default', // 块,对应add_settings_section的id
			$args
		);
	}
 
	/**
	 * id ID
	 */
	public function sonliss_register_setting($id)
	{
		register_setting('general', $id);
	}
 
	/**
	 * input text
	 */
	public function sonliss_textbox_callback($args) {
		$option = get_option($args[0]);
		echo '<input type="text" id="'. $args[0] .'" name="'. $args[0] .'" value="' . $option . '" class="regular-text" />';
	}
 
	/**
	 * area
	 */
	public function sonliss_textareabox_callback($args) {
		$option = get_option($args[0]);
		echo '<textarea id="'. $args[0] .'" name="'. $args[0] .'" class="regular-text">'. $option .'</textarea>';
	}
}
暂无评论

发送评论 编辑评论


				
上一篇
下一篇