本文介绍如何使用 WordPress 设置 API 创建一个简单的单选、列表选择、复选和媒体上传设置页面,并在设置菜单中添加链接菜单。
将代码添加到主题函数模板 functions.php 中。
单选
- function zm_demo_settings_radio() {
- add_settings_section("section", "标题", null, "demo");
- add_settings_field("demo-radio", "单选按钮演示", "zm_demo_radio_display", "demo", "section");
- register_setting("section", "zm-demo-radio");
- }
- function zm_demo_radio_display() {
- ?>
- <input type="radio" name="zm-demo-radio" value="1" <?php checked( 1, get_option( 'zm-demo-radio' ), true); ?>>1
- <input type="radio" name="zm-demo-radio" value="2" <?php checked( 2, get_option( 'zm-demo-radio' ), true); ?>>2
- <?php
- }
- add_action( "admin_init", "zm_demo_settings_radio" );
- function zm_demo_page() {
- ?>
- <div 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-general.php", "单选设置", "单选设置", "manage_options", "demo", "zm_demo_page" );
- }
- add_action( "admin_menu", "zm_menu_item" );
列表选择
- // 列表选择
- function zm_demo_settings_select() {
- add_settings_section( "section", "标题", null, "demo" );
- add_settings_field( "demo-select", "列表选择", "zm_demo_select_display", "demo", "section" );
- register_setting( "section", "demo-select" );
- }
- 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' ), "qnimate" ); ?>>QNimate</option>
- <option value="qidea" <?php selected( get_option( 'zm-demo-select' ), "qidea" ); ?>>QIdea</option>
- <option value="qtrack" <?php selected( get_option( 'zm-demo-select' ), "qtrack" ); ?>>QTrack</option>
- </select>
- <?php
- }
- add_action( "admin_init", "zm_demo_settings_select" );
- function zm_demo_page() {
- ?>
- <div 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_select() {
- add_submenu_page( "options-general.php", "列表选择", "列表选择", "manage_options", "demo", "zm_demo_page" );
- }
- add_action( "admin_menu", "zm_menu_item_select" );
复选框
- // 复选框
- function zm_demo_settings_page_checkbox() {
- add_settings_section( "section", "标题", null, "demo" );
- add_settings_field( "demo-checkbox", "复选框", "zm_demo_checkbox_display", "demo", "section" );
- register_setting( "section", "demo-checkbox" );
- }
- function zm_demo_checkbox_display() {
- ?>
- <input type="checkbox" name="demo-checkbox" value="1" <?php checked( 1, get_option( 'zm-demo-checkbox' ), true ); ?> />
- <?php
- }
- add_action( "admin_init", "zm_demo_settings_page_checkbox" );
- function zm_demo_page() {
- ?>
- <div 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_checkbox() {
- add_submenu_page( "options-general.php", "复选框", "复选框", "manage_options", "demo", "zm_demo_page" );
- }
- add_action( "admin_menu", "zm_menu_item_checkbox" );
媒体上传
- // 媒体上传
- function zm_demo_settings_page_file() {
- add_settings_section( "section", "标题", null, "demo" );
- add_settings_field( "demo-file", "媒体上传", "zm_demo_file_display", "demo", "section" );
- register_setting( "section", "demo-file", "handle_file_upload" );
- }
- function handle_file_upload( $option ) {
- if( !empty( $_FILES["demo-file"]["tmp_name"] ) ) {
- $urls = wp_handle_upload( $_FILES["demo-file"], array( 'test_form' => FALSE ) );
- $temp = $urls["url"];
- return $temp;
- }
- return $option;
- }
- function zm_demo_file_display() {
- ?>
- <input type="file" name="demo-file" />
- <?php echo get_option( 'demo-file' ); ?>
- <?php
- }
- add_action( "admin_init", "zm_demo_settings_page_file" );
- function zm_demo_page_file() {
- ?>
- <div 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_file() {
- add_submenu_page( "options-general.php", "媒体上传", "媒体上传", "manage_options", "demo", "zm_demo_page_file" );
- }
- add_action( "admin_menu", "zm_menu_item_file" );
设置存储在 wp_options 表中。
文章標題:利用 WordPress 设置 API 添加选择设置页面
文章連結:https://www.wuyanshuo.cn/795.html
更新時間:2022年4月30日
1、本站所有資源均不添加推廣檔案或浮水印,壓縮包內若有廣告檔案和浮水印請勿輕易相信。
2、本站資源均為兩層壓縮,第一層7z(尾碼若為wys,請自行修改為7z)有解壓密碼; 第二層zip或cbz,無解壓密碼,可直接使用漫畫類軟件程式查看; 詳情可參攷解壓教程。
3、本站大部分內容均收集於網絡! 若內容侵犯到您的權益,請發送郵件至:admin#wysacg.top我們將第一時間處理! 資源所需價格並非資源售賣價格,是收集、整理、編輯詳情以及本站運營的適當補貼,並且本站不提供任何免費技術支援。 所有資源僅限於參攷和學習,版權歸原作者所有!