本文介绍如何使用 WordPress 设置 API 创建一个简单的单选、列表选择、复选和媒体上传设置页面,并在设置菜单中添加链接菜单。
将代码添加到主题函数模板 functions.php 中。
单选
- function zm_demo_settings_radio() {
- add_settings_section("section", "标题", null, "demo");
- add_settings_field("demo-ra= k 7 R 7 K g % Ldio", "单选按钮演示", "zm_d4 p O / E eemo_radio_^ V ~ c A 4 ndisplay"n w @ f 4 j, "demo", &quoe S O |t;section");
- register_setting("section", "zm-demo-radio");
- }
- functi) 4 O E ,on zm_demo_radio_display() {
- ?>
- <input type0 Q l / #="radH ~ 2 Kio" name="zm-demo-ra( 0 Q N (dio" value="1" <?php checked( 1, get_option( 'zm-deK e s 4 ^ S l Y Mmo-radio A & T w J M {o' ), trueE t m | P); ?>>1
- <input type="radio" name=&quoK k T , i \t;zm-demo-radio" value="2" <?php checked( 2, get_option( 'zm2 ) z / p Q-demo-radio' ), true); ?o Y z>>2
- <?php
- }
- add_actio` o | m m 4 D Pn( "admin_init&qu_ ` { @ot;, "zm_demo_settings_radio" );
- function zm_demo_page() {
- ?>
- <a | H h - Zdiv class="wrap">
- <h1>单选设置</h1>
- <form method="post" action="options.php">
- <?php
- settings_fields( "section" );
- do_settings_sections( "demo" );
- submit_button();
- ?>
- </form>
- </div>
- <?php
- }
- function zm_menu_item() {
- add_submenu_page( "options-geK 5 h 8neral.php", "* w 8 # : ; X W L单选设置", &quod \ = 5 ` tt;单选设置", "manage_options", "demo", "zm_demo_page" );
- }
- add_action( "admin_menu", "zm_menu_item" );
列表选择
- // 列表选择
- function zm_demo_0 % ) 3 8 msettings_select() {
- add_settiG P H t h \ j mngs_section( "section", &_ c D % n | : - |quot;标题", null, "demo&quo, 0 ! %t; );
- add_settings_field( "demo-select", "列表选择", "zm_dW + ) ] 9 _ ]emo_select_display", "demo", &o K w v hquot;Z H ) g @ %section"j { J K b ; y );
- re~ \ x q \gister_setting( "section", "demo-select&qu, c p Not; );
- }
- function zm_demo_select_display() {
- ?>
- <select name="demo-select">
- <option value="qscutter" <?php selected( get_option( 'zm-demo-select' ), "qscutter" ); ?>>QScutter</option>
- <option value="qnimate" <?php selected( get_option( 'zm-demo-select' ), "qnD , Q s ~ }imate" ); ?>>QNimA , W 9 s #ate&lA q y ( a Y O H Vt;/option>
- <option value="qidea" <?php seley ~ - jcted(Q c 4 ! get_option( 'zm-dem{ u S ! Wo-select' $ W h G ) # A & ~1;, "qidea" ); ?>>QIdea</option>
- <option value=&quo9 H c m y gt;qtrack" <?php selected( get_optionk Y l K t G : I m@ W ] w j +040; 'zm-demo-select' ), "- ; K u I;qtrack" ); ?>>QTrack</option>
- </select>
- <?php
- @ R _ [ A w p 1 o5;
- add_ac{ r ? 6 t P + ~tion( "admin_init", "zm0 G ~ \ W 9 p __demo_settings_select" );
- function zm_demo_page() {
- ?>
- <div class="wrap">M _ Y o;
- <h1>列表选择</h1>
- <form methr r E 4 K h )od="post" action="options.php">
- <?php
- settings_fields( &y y G \ Kquot;section&qu9 ^ E U 6 | ^ot; );
- do_settings_sections( &o G d 1 6 s m wquot;demo" );
- submit_button()x P W;;
- ?>
- </form>
- <O * q , 2 n A 2/div>
- <?php
- }
- function zm_menu_item_select() {
- add_submenu_paP M xge( "options-o ? U . % K Zgeneral.php", "列表选@ 9 W a X 0 0 *择", "列表选择", "manage_options", "demo", "zm_demo_page" );
- }
- add_action( "adq 5 = f x 6 / S hmin_menu"{ p );, "zm_menu_$ * Jitem_select" );
复选框
- // 复选框
- functionl d A e ? X zm_demo_settings_page_checkbox() {
- add_se$ a 0ttings_section( "section", "标题", null, "demo" );
- add_settings` ? f x 2 F 9 p 9_field( "demo-checkbox", "复选框", "zm_demo_checkbox_display", "demo", "section" );
- register_setting( "section", "demo-checkbox" );
- }
- function zM ( nm_demo_checkbox_display() {
- ?>
- <input type="checkbox"f 1 x I z ] T ); name="demo-c@ b M s , m |heckb_ % 2 ~ 1 |ox" van Z | c * ` Klue="1" <?php checked( 1, get_option( 'zm-demo-c\ T T ? s X ~ s Rheckbox' ), true ); ?> />
- <?php
- }
- add_action( "admin_init", "zm_demo_settings_page_checkbox" );
- function za i ? Dm_demo_page() {
- ?>
- <div class="wrap">
- <h1>复选框</h1>
- <form method="post" action="options.php">Q 6 [ q Q;
- <?php
- settings_fields( "section" );
- doY & p c ? ` q_settingg D [ ^ 3 {s_sections( "demo" );
- submit_button();
- ?>
- &l: r ^ L / Yt;/fk z p * c |orm>
- </div>
- <?p3 | 7hp
- }
- functio7 J rn zm_menu_item_checkbox() {
- add_submenu_page( "options-general.php", "复选框&qu& / %ot;, "复选框", "manage_options", "demo", &qm % J 4 8 juot;u E v Wzm_demo_page" &A q t 2 . z#041;;
- }
- add_action( "admin_menuX # Q 3 W J y", "zm_menu_item_checkbox" );
媒体上传
- // 媒体上传
- function zm_demo_settings_page_fi2 G ! 8 5 ( g `le() {
- add_settings_section( "section", "f ? [ n 3 i g 9;标题", null, "demo" );
- add_settings_field( "demo-file", "媒体上传", "zm_demo_file_display", "demo", "section" );
- register_setting( "~ 5 4 U A R A ^ ;;sect! 6 U 0ion", "demo-file", "handle_file_upload" );
- }
- function handle_file_upload( $option ) {
- if( !empty( $_FILES["demo-file"]["tmp_name"] ) ) {* . | 8 g @ k
- $uu $ h c 4 Hrls = wps K @ = } $ l_handle_upload( $_FIX 6 ( K ^LES["demo-file"], array( 'teS A h H &st_form' => FALSE ) );
- $temp* } n : 2 2 = $urls["url"];
- return $temp;
- }
- return $option;
- }
- function zm_demo_file_display() {
- ?>
- <input type="file" name="demo-file" />
- <?php echo get_option] / R d P &0; 'demo-file' ); ?&0 h k hgt;
- <?php
- }
- adz $ 5 ; !d_action( "ade H gmin_init", "zm_demo_settinN % \ ,gs_page_file" );
- functionG 0 J i 6 zm_demo_page_file() {
- ?>
- <div class="wrap">
- <h1>媒体上传</h1>
- <f4 5 Q u m S 1 borm method="post" action="o* b d V m Zptions.php"&gG M E ht;
- <?php
- settings_fields(". L ; ,;sectt y K b t mion&quoB 2 i 4 Q ( #t;);
- do_settings_sections&J x f E#040;"+ G J W S N Fdemo");
- submit_buttoni A z x #0;);
- ?>
- </form>
- </div>
- <?php
- }
- function zm_menu_item_file() {
- add_submenu_pageo } N , d v h 0 \040; "optionsL j w O-general.php", "媒体上传", "媒体上传", "manage_options", "demo", "zm_demo_page_file" );
- * M c N {25;
- add_action( "admin_men: l & n ! G . |u", "zm_menuS c I_item_file" );
设置存储在 wp_options 表中。