Get database field content in WordPress and add theme settings menu _php tips

Source: Internet
Author: User
Tags wordpress blog

Get_option () function Use tips
get_option () This function is actually a function that we often use when we integrate back-office functionality, mainly to get a function of the field content we want from the WordPress Blog database option table, in most of the current topics, As long as the theme of the integration of the background control page, and most of the use of this function, although WP gives us a lot of ways to temporarily store our theme settings, but with a more stable database to store theme settings or most of the theme of the author's preferred.

Get_option () The interpretation of the function, the use of the internet there are many Chinese literature, including official documents The function also has the Chinese version of the explanation, because it is simple to use, the idea is more clear so here is not too much explanation.
Official api:get_option
Direct Memo Usage

Use Tips
Usually when we take the setup, we add a judgment, because we don't know if the subject is the first time to install it in the system,

 if (!get_option (' Xiangzi ')) {//To determine if there is
 a $default _xiangzi = array (//) first set a default value
 ' title ' => ' blog ',
 ' name ' = > ' Xiang Zi ',
 ' url ' => ' pangbu.com ');
 Update_option (' Xiangzi ', $default _xiangzi);
 Set the default value for option
}
$xiangzi = get_option (' Xiangzi ');//Get Settings

Coincidentally, for some reason, WP officially added the second parameter of the Get_option function to us.
So we can do this.

 $default _xiangzi = Array (//first set a default value
 ' title ' => ' blog ',
 ' name ' => ' Xiang Zi ',
 ' url ' => ' pangbu.com ')
$ Xiangzi = get_option (' Xiangzi ', $default _xiangzi);
Isn't that easy?

Add_theme_page () function-Add your Theme Settings menu
Perhaps you will feel, Wordpress official default theme, is a very egg pain theme, the style is ugly, and the function is simple, there is only a humble abode Ming this kind of inspirational article can encourage you to use down the feeling, You may feel that the subject is almost useless except for those who do not have the theme of the dish and vegetables, then you are wrong. The biggest use of the official default theme is that there is a lot of research value, at least my current WP topic knowledge is based on the study of the default theme, today is the Add_theme_page () function.

Describe
Add_theme_page () function, add page in WordPress background. Here we are generally in order to add Settings page to use this function, of course, if you are idle eggs pain, you can add such as personal description of the page in the background.

Use

<?php 
add_theme_page ($page _title, $menu _title, $capability, $menu _slug, $function); 
The contents of the Page_titile-title label 
//menu_title-The title that appears in the left menu in the background 
//capability-the permissions//menu_slug-alias required to access this page 
, Need to be unique OH 
//function-functions performed by 
?> 

$page _title, $menu _title, $capability, $menu _slug, $function These several parameters are more commonly used.

Because the Add_theme_page () function is simply too simple to use, let's go directly to the code.

Instance

 function Xz_theme_options_add_page () {
 $theme _page = add_theme_page (
 ' theme Settings ',//page Titlee
 ' theme Settings ',// The name shown in the Background menu '
 edit_theme_options ',//   options placed in the position
 ' theme_options ',    //alias, that is, get-transfer parameters
 ' Xz_theme_op _page '//calls functions that display content calls
 ;
}
function Xz_theme_op_page () {//content display functions
 echo "I am the topic edit page";
}
Add_action (' Admin_menu ', ' xz_theme_options_add_page ');

Effect

Add background Menu Effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.