本文介绍如何使用 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表中。
本站文章大部分为原创,用于个人学习记录,可能对您有所帮助,仅供参考!
我的微信
微信号已复制
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!