Home >Backend Development >PHP Tutorial >Get database field content and add theme settings menu in WordPress, wordpress field_PHP tutorial

Get database field content and add theme settings menu in WordPress, wordpress field_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:00:491064browse

Get database field content and add theme settings menu in WordPress, wordpress field

get_option() function usage tips
The get_option() function is actually a function we often use when integrating back-end functions. It is mainly used to obtain the field content we want from the WordPress blog database option table. Currently, it is In some themes, as long as the theme integrates the background control page, most of them use this function. Although WP gives us many methods to temporarily store our theme settings, most of them use a relatively stable database to store theme settings. Top choice for theme authors.

There are many Chinese documents on the explanation and usage of the get_option() function on the Internet, including official documents. This function also has a Chinese version of the explanation. Because it is simple to use and the idea is relatively clear, I will not explain too much here.
Official API: get_option
Direct memo usage

Tips
Usually when we get the settings, we will add a judgment, because we don’t know whether the theme is installed in the system for the first time,

 if (!get_option('xiangzi')){//判断是否存在
 $default_xiangzi = array (//先设置一个默认值
 'title'=>'博客',
 'name'=>'翔子',
 'url'=>'pangbu.com');
 update_option('xiangzi', $default_xiangzi);
 //设置option的默认值
}
$xiangzi = get_option('xiangzi');//获取设置

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

 $default_xiangzi = array (//先设置一个默认值
 'title'=>'博客',
 'name'=>'翔子',
 'url'=>'pangbu.com')
$xiangzi = get_option('xiangzi',$default_xiangzi);
//这样是不是很简单?

add_theme_page() function-add your theme settings menu
Perhaps you will feel that the official default theme of WordPress is a very painful theme with ugly style and poor functions. There is a feeling that only inspirational articles such as the inscription of the humble room can encourage you to continue using it. Maybe you will If you think that this theme is almost useless except for those newbies who don't know how to install the theme, you are wrong. The biggest use of the official default theme is that it has great research value. At least most of my current WP theme knowledge is obtained by studying the default theme. Today I am studying the add_theme_page() function.

Description
The add_theme_page() function adds a page in the WordPress backend. Here we usually use this function to add a setting page. Of course, if you are free, you can add a page such as a personal description in the background.

Use

<&#63;php 
add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function); 
//page_titile-title标签的内容 
//menu_title-显示在后台左边菜单的标题 
//capability-访问这个页面需要的权限 
//menu_slug-别名,需要独一无二哦 
//function-执行的函数 
&#63;> 

$page_title, $menu_title, $capability, $menu_slug, $function are commonly used parameters.

Because the add_theme_page() function is so simple to use, let’s go straight to the code.

Example

 function xz_theme_options_add_page() {
 $theme_page = add_theme_page(
 '主题设置', //页面Titlee
 '主题设置', // 在后台菜单中显示的名字
 'edit_theme_options',   // 选项放置的位置
 'theme_options',    // 别名,也就是get传送的参数
 'xz_theme_op_page' //调用显示内容调用的函数
 );
}
function xz_theme_op_page (){//内容显示函数
 echo "我是主题编辑页面";
}
add_action( 'admin_menu', 'xz_theme_options_add_page' );

Effect

2016111144058498.png (444×668)

Add background menu effect

Articles you may be interested in:

  • How to solve the problems that arise when using WordPress’s $wpdb class to read mysql database for ajax
  • WordPress importing database appears “Unknown collation” : 'utf8mb4_unicode_ci" error solution
  • Briefly understand the usage of update_option() function in WordPress development

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1091109.htmlTechArticleGet database field content and add theme settings menu in WordPress, wordpress field get_option() function usage tips get_option() With this function, we are actually integrating background functions...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn