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

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

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

单选

  1. function zm_demo_settings_radio() {
  2. add_settings_section("section", "标题", null, "demo");
  3. 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");
  4. register_setting("section", "zm-demo-radio");
  5. }
  6. functi) 4 O E ,on zm_demo_radio_display() {
  7. ?>
  8. <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
  9. <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
  10. <?php
  11. }
  12. add_actio` o | m m 4 D Pn( "admin_init&qu_ ` { @ot;, "zm_demo_settings_radio" );
  13. function zm_demo_page() {
  14. ?>
  15. <a | H h – Zdiv class="wrap">
  16. <h1>单选设置</h1>
  17. <form method="post" action="options.php">
  18. <?php
  19. settings_fields( "section" );
  20. do_settings_sections( "demo" );
  21. submit_button();
  22. ?>
  23. </form>
  24. </div>
  25. <?php
  26. }
  27. function zm_menu_item() {
  28. add_submenu_page( "options-geK 5 h 8neral.php", "* w 8 # : ; X W L单选设置", &quod \ = 5 ` tt;单选设置", "manage_options", "demo", "zm_demo_page" );
  29. }
  30. add_action( "admin_menu", "zm_menu_item" );

列表选择

  1. // 列表选择
  2. function zm_demo_0 % ) 3 8 msettings_select() {
  3. add_settiG P H t h \ j mngs_section( "section", &_ c D % n | : – |quot;标题", null, "demo&quo, 0 ! %t; );
  4. 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 );
  5. re~ \ x q \gister_setting( "section", "demo-select&qu, c p Not; );
  6. }
  7. function zm_demo_select_display() {
  8. ?>
  9. <select name="demo-select">
  10. <option value="qscutter" <?php selected( get_option( ‘zm-demo-select’ ), "qscutter" ); ?>>QScutter</option>
  11. <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>
  12. <option value="qidea" <?php seley ~ – jcted(Q c 4 ! get_option( ‘zm-dem{ u S ! Wo-select’ &#04$ W h G ) # A & ~1;, "qidea" ); ?>>QIdea</option>
  13. <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’ ), &quot– ; K u I;qtrack" ); ?>>QTrack</option>
  14. </select>
  15. <?php
  16. &#12@ R _ [ A w p 1 o5;
  17. add_ac{ r ? 6 t P + ~tion( "admin_init", "zm0 G ~ \ W 9 p __demo_settings_select" );
  18. function zm_demo_page() {
  19. ?>
  20. <div class="wrap"&gtM _ Y o;
  21. <h1>列表选择</h1>
  22. <form methr r E 4 K h )od="post" action="options.php">
  23. <?php
  24. settings_fields( &y y G \ Kquot;section&qu9 ^ E U 6 | ^ot; );
  25. do_settings_sections( &o G d 1 6 s m wquot;demo" );
  26. submit_button(&#041x P W;;
  27. ?>
  28. </form>
  29. <O * q , 2 n A 2/div>
  30. <?php
  31. }
  32. function zm_menu_item_select() {
  33. add_submenu_paP M xge( "options-o ? U . % K Zgeneral.php", "列表选@ 9 W a X 0 0 *择", "列表选择", "manage_options", "demo", "zm_demo_page" );
  34. }
  35. add_action( "adq 5 = f x 6 / S hmin_menu&quot{ p );, "zm_menu_$ * Jitem_select" );

复选框

  1. // 复选框
  2. functionl d A e ? X zm_demo_settings_page_checkbox() {
  3. add_se$ a 0ttings_section( "section", "标题", null, "demo" );
  4. add_settings` ? f x 2 F 9 p 9_field( "demo-checkbox", "复选框", "zm_demo_checkbox_display", "demo", "section" );
  5. register_setting( "section", "demo-checkbox" );
  6. }
  7. function zM ( nm_demo_checkbox_display() {
  8. ?>
  9. <input type="checkbox&quotf 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 ); ?> />
  10. <?php
  11. }
  12. add_action( "admin_init", "zm_demo_settings_page_checkbox" );
  13. function za i ? Dm_demo_page() {
  14. ?>
  15. <div class="wrap">
  16. <h1>复选框</h1>
  17. <form method="post" action="options.php"&gtQ 6 [ q Q;
  18. <?php
  19. settings_fields( "section" );
  20. doY & p c ? ` q_settingg D [ ^ 3 {s_sections( "demo" );
  21. submit_button();
  22. ?>
  23. &l: r ^ L / Yt;/fk z p * c |orm>
  24. </div>
  25. <?p3 | 7hp
  26. }
  27. functio7 J rn zm_menu_item_checkbox() {
  28. 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;;
  29. }
  30. add_action( "admin_menuX # Q 3 W J y", "zm_menu_item_checkbox" );

媒体上传

  1. // 媒体上传
  2. function zm_demo_settings_page_fi2 G ! 8 5 ( g `le() {
  3. add_settings_section( "section", &quotf ? [ n 3 i g 9;标题", null, "demo" );
  4. add_settings_field( "demo-file", "媒体上传", "zm_demo_file_display", "demo", "section" );
  5. register_setting( &quot~ 5 4 U A R A ^ ;;sect! 6 U 0ion", "demo-file", "handle_file_upload" );
  6. }
  7. function handle_file_upload( $option ) {
  8. if( !empty( $_FILES["demo-file"]["tmp_name"] ) ) {* . | 8 g @ k
  9. $uu $ h c 4 Hrls = wps K @ = } $ l_handle_upload( $_FIX 6 ( K ^LES["demo-file"], array( ‘teS A h H &st_form’ => FALSE ) );
  10. $temp* } n : 2 2 = $urls["url"];
  11. return $temp;
  12. }
  13. return $option;
  14. }
  15. function zm_demo_file_display() {
  16. ?>
  17. <input type="file" name="demo-file" />
  18. <?php echo get_option&#04] / R d P &0; ‘demo-file’ ); ?&0 h k hgt;
  19. <?php
  20. }
  21. adz $ 5 ; !d_action( "ade H gmin_init", "zm_demo_settinN % \ ,gs_page_file" );
  22. functionG 0 J i 6 zm_demo_page_file() {
  23. ?>
  24. <div class="wrap">
  25. <h1>媒体上传</h1>
  26. <f4 5 Q u m S 1 borm method="post" action="o* b d V m Zptions.php"&gG M E ht;
  27. <?php
  28. settings_fields(&quot. L ; ,;sectt y K b t mion&quoB 2 i 4 Q ( #t;);
  29. do_settings_sections&J x f E#040;"+ G J W S N Fdemo");
  30. submit_button&#04i A z x #0;);
  31. ?>
  32. </form>
  33. </div>
  34. <?php
  35. }
  36. function zm_menu_item_file() {
  37. add_submenu_page&#o } N , d v h 0 \040; "optionsL j w O-general.php", "媒体上传", "媒体上传", "manage_options", "demo", "zm_demo_page_file" );
  38. &#1* M c N {25;
  39. add_action( "admin_men: l & n ! G . |u", "zm_menuS c I_item_file" );

设置存储在 wp_options 表中。

给TA打赏
共{{data.count}}人
人已打赏
WordPress教程

WordPress 导航菜单添加自定义文字信息

2022-5-7 13:52:32

WordPress教程

WordPress 后台直接复制文章并设置状态为草稿

2022-5-7 19:55:46

下载说明

  • 1、微码盒所提供的压缩包若无特别说明,解压密码均为weimahe.com
  • 2、下载后文件若为压缩包格式,请安装7Z软件或者其它压缩软件进行解压;
  • 3、文件比较大的时候,建议使用下载工具进行下载,浏览器下载有时候会自动中断,导致下载错误;
  • 4、资源可能会由于内容问题被和谐,导致下载链接不可用,遇到此问题,请到文章页面进行反馈,以便微码盒及时进行更新;
  • 5、其他下载问题请自行搜索教程,这里不一一讲解。

站长声明

本站大部分下载资源收集于网络,只做学习和交流使用,版权归原作者所有;若为付费资源,请在下载后24小时之内自觉删除;若作商业用途,请到原网站购买;由于未及时购买和付费发生的侵权行为,与本站无关。本站发布的内容若侵犯到您的权益,请联系本站删除,我们将及时处理!
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索