WordPress教程

利用 WordPress 设置 API 添加选择设置页面

阿里云

本文介绍如何使用 WordPress 设置 API 创建一个简单的单选、列表选择、复选和媒体上传设置页面,并在设置菜单中添加链接菜单。

将代码添加到主题函数模板 functions.php 中。

单选

  1.     function zm_demo_settings_radio() {
  2.     	add_settings_section("section", "标题", null, "demo");
  3.     	add_settings_field("demo-radio", "单选按钮演示", "zm_demo_radio_display", "demo", "section");  
  4.     	register_setting("section", "zm-demo-radio");
  5.     }
  6.  
  7.     function zm_demo_radio_display() {
  8.     	?>
  9.     		<input type="radio" name="zm-demo-radio" value="1" <?php checked( 1, get_option( 'zm-demo-radio' ), true); ?>>1
  10.     		<input type="radio" name="zm-demo-radio" value="2" <?php checked( 2, get_option( 'zm-demo-radio' ), true); ?>>2
  11.     	<?php
  12.     }
  13.  
  14.     add_action( "admin_init", "zm_demo_settings_radio" );
  15.  
  16.     function zm_demo_page() {
  17.     	?>
  18.     	<div class="wrap">
  19.     		<h1>单选设置</h1>
  20.     		<form method="post" action="options.php">
  21.     			<?php
  22.     				settings_fields( "section" );
  23.      				do_settings_sections( "demo" );
  24.     				submit_button();
  25.     			?>
  26.     		</form>
  27.     	</div>
  28.     	<?php
  29.     }
  30.  
  31.     function zm_menu_item() {
  32.     	add_submenu_page( "options-general.php", "单选设置", "单选设置", "manage_options", "demo", "zm_demo_page" );
  33.     }
  34.  
  35.     add_action( "admin_menu", "zm_menu_item" );
也想出现在这里?联系我们
创客主机

列表选择

  1.     // 列表选择
  2.     function zm_demo_settings_select() {
  3.     	add_settings_section( "section", "标题", null, "demo" );
  4.     	add_settings_field( "demo-select", "列表选择", "zm_demo_select_display", "demo", "section" );
  5.     	register_setting( "section", "demo-select" );
  6.     }
  7.  
  8.     function zm_demo_select_display() {
  9.     	?>
  10.     		<select name="demo-select">
  11.     			<option value="qscutter" <?php selected( get_option( 'zm-demo-select' ), "qscutter" ); ?>>QScutter</option>
  12.     			<option value="qnimate" <?php selected( get_option( 'zm-demo-select' ), "qnimate" ); ?>>QNimate</option>
  13.     			<option value="qidea" <?php selected( get_option( 'zm-demo-select' ), "qidea" ); ?>>QIdea</option>
  14.     			<option value="qtrack" <?php selected( get_option( 'zm-demo-select' ), "qtrack" ); ?>>QTrack</option>
  15.     		</select>
  16.     	<?php
  17.     }
  18.  
  19.     add_action( "admin_init", "zm_demo_settings_select" );
  20.  
  21.     function zm_demo_page() {
  22.     	?>
  23.     		<div class="wrap">
  24.     			<h1>列表选择</h1>
  25.     			<form method="post" action="options.php">
  26.     				<?php
  27.     					settings_fields( "section" ); 
  28.     					do_settings_sections( "demo" );
  29.     					submit_button();
  30.     				?>
  31.     			</form>
  32.     		</div>
  33.     	<?php
  34.     }
  35.  
  36.     function zm_menu_item_select() {
  37.     	add_submenu_page( "options-general.php", "列表选择", "列表选择", "manage_options", "demo", "zm_demo_page" );
  38.     }
  39.  
  40.     add_action( "admin_menu", "zm_menu_item_select" );

复选框

  1.     // 复选框
  2.     function zm_demo_settings_page_checkbox() {
  3.     	add_settings_section( "section", "标题", null, "demo" );
  4.     	add_settings_field( "demo-checkbox", "复选框", "zm_demo_checkbox_display", "demo", "section" );
  5.     	register_setting( "section", "demo-checkbox" );
  6.     }
  7.  
  8.     function zm_demo_checkbox_display() {
  9.     	?>
  10.     		<input type="checkbox" name="demo-checkbox" value="1" <?php checked( 1, get_option( 'zm-demo-checkbox' ), true ); ?> />
  11.     	<?php
  12.     }
  13.  
  14.     add_action( "admin_init", "zm_demo_settings_page_checkbox" );
  15.  
  16.     function zm_demo_page() {
  17.     	?>
  18.     		<div class="wrap">
  19.     			<h1>复选框</h1>
  20.     			<form method="post" action="options.php">
  21.     				<?php
  22.     					settings_fields( "section" );
  23.     					do_settings_sections( "demo" );
  24.     					submit_button();
  25.     				?>
  26.     			</form>
  27.     		</div>
  28.     	<?php
  29.     }
  30.  
  31.     function zm_menu_item_checkbox() {
  32.     	add_submenu_page( "options-general.php", "复选框", "复选框", "manage_options", "demo", "zm_demo_page" );
  33.     }
  34.  
  35.     add_action( "admin_menu", "zm_menu_item_checkbox" );

媒体上传

  1.     // 媒体上传
  2.     function zm_demo_settings_page_file() {
  3.     	add_settings_section( "section", "标题", null, "demo" );
  4.     	add_settings_field( "demo-file", "媒体上传", "zm_demo_file_display", "demo", "section" );  
  5.     	register_setting( "section", "demo-file", "handle_file_upload" );
  6.     }
  7.  
  8.     function handle_file_upload( $option ) {
  9.     	if( !empty( $_FILES["demo-file"]["tmp_name"] ) ) {
  10.     		$urls = wp_handle_upload( $_FILES["demo-file"], array( 'test_form' => FALSE ) );
  11.     		$temp = $urls["url"];
  12.     		return $temp;
  13.     	}
  14.     	return $option;
  15.     }
  16.  
  17.     function zm_demo_file_display() {
  18.     	?>
  19.     		<input type="file" name="demo-file" />
  20.     		<?php echo get_option( 'demo-file' ); ?>
  21.     	<?php
  22.     }
  23.  
  24.     add_action( "admin_init", "zm_demo_settings_page_file" );
  25.  
  26.     function zm_demo_page_file() {
  27.     	?>
  28.     		<div class="wrap">
  29.     			<h1>媒体上传</h1>
  30.     			<form method="post" action="options.php">
  31.     				<?php
  32.     					settings_fields("section");
  33.     					do_settings_sections("demo");
  34.     					submit_button();
  35.     				?>
  36.     			</form>
  37.     		</div>
  38.     	<?php
  39.     }
  40.  
  41.     function zm_menu_item_file() {
  42.     	add_submenu_page( "options-general.php", "媒体上传", "媒体上传", "manage_options", "demo", "zm_demo_page_file" );
  43.     }
  44.     add_action( "admin_menu", "zm_menu_item_file" );

设置存储在 wp_options 表中。

利用 WordPress 设置 API 添加选择设置页面

已有 262 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!