This article mainly introduces how to get the database field content in WordPress and add the topic settings menu. it explains the usage of the get_option () function and the add_theme_page () function respectively, for more information, see
Tips for using the get_option () function
The get_option () function is a function that we often use when integrating background functions. it is mainly used to obtain the desired field content from the option table of the WordPress blog database, in most of the current themes, as long as the theme is integrated with the background control page, most of them use this function. although WP has given us a lot of ways to temporarily store our settings for the theme, however, using a stable database to store theme settings is still the first choice for most topic authors.
The get_option () function is interpreted and used in many Chinese documents on the Internet, including official documents. This function is also interpreted in Chinese, because it is easy to use, the idea is clear, so there is no more explanation here.
Official API: get_option
Direct memo usage
Tips
We usually add a judgment when taking the settings, because we do not know whether the topic is installed in the system for the first time,
If (! Get_option ('xiangzi') {// determines whether $ default_xiangzi = array exists (// first set a default value 'title' => 'blog ', 'name' => 'xiangz', 'URL' => 'pangbu. com '); update_option ('xiangzi', $ default_xiangzi); // set the default value of option} $ xiangzi = get_option ('xiangzi'); // get the settings
Unfortunately, for some reason, WP officially added the second parameter of the get_option function to us.
So we can.
$ Default_xiangzi = array (// first set a default value 'title' => 'blog ', 'name' => 'xiangzi', 'URL' => 'pangbu. com ') $ xiangzi = get_option ('xiangzi', $ default_xiangzi); // is this simple?
Add_theme_page () function-add your topic Settings menu
You may think that Wordpress's official default theme is a tough topic, with ugly styles and simple functions. there is a kind of inspirational article that can only encourage you to use it, maybe you will think that this topic is of almost no use except for those who will not install the topic, and you will be wrong. The biggest use of the official default topic is its great research value. at least most of my current WP topic knowledge is obtained by studying the default topic. today I am studying the add_theme_page () function.
Description
Add_theme_page () function to add a page in the WordPress background. This function is generally used to add a page. if you are idle, you can add pages such as personal instructions to the background.
Use
<? Php add_theme_page ($ page_title, $ menu_title, $ capability, $ menu_slug, $ function ); // content of the page_titile-title label // menu_title-the title of the menu displayed on the left of the background // capability-the permissions required to access this page // menu_slug-alias, need to be unique // function-executed function?>
$ Page_title, $ menu_title, $ capability, $ menu_slug, $ function are commonly used.
Because the add_theme_page () function is too simple to use, we should directly go to the code.
Instance
Function xz_theme_options_add_page () {$ theme_page = add_theme_page ('theme_page settings', // page Titlee 'theme_options', // name displayed in the background menu 'edit _ theme_options ', // the position of the option 'theme _ options', // alias, that is, the parameter 'xz _ theme_op_page 'transmitted by get // call the function called to display the content );} function xz_theme_op_page () {// content display function echo "I am a topic editing page";} add_action ('admin _ menu ', 'xz _ theme_options_add_page ');
Effect
Add background menu effects