Add theme management options to WordPress

Source: Internet
Author: User
Tags php file

When creating a WordPress topic, you often need to add management options to the topic to provide theme users with custom settings, so as not to change the source code of the topic. Many people have shared related tutorials on the internet. This feature was suddenly used when I wrote the code last night. However, I searched for many versions and found that the neotify version was the best, in view of the fact that neotify websites often cannot be opened and sometimes cannot be viewed, this article is purely a record for future reference.

The neogram version is a simple version. It only supports simple text boxes, options, and other functions. If you need to set up an option page with powerful functions such as cool drag, you can refer to the topic management option framework recommended at the end of this article.

The following content is transferred from neokeeper

It is not difficult to add some management options for the topic. You only need to finish the first option processing, and the processing of other options will be solved. This time we use WordPress's built-in theme classic as an example to create a management option for the homepage announcement bar. This tutorial requires some HTML basics and a preliminary understanding of PHP.

Background processing

First, we need to modify function. php. The main processing work is in this file. If the topic does not have this file, create one. (No function. php indicates that the theme does not support widgets. It is not a good habit to create a theme. If you want the theme to support widgets, see here: let the theme support widgets)

My processing includes three parts: Getting options, initializing, and tab operation interface. Only one announcement board is created here, which includes two options (whether to display the announcement board and announcement board content ). To add more options, you only need to append some code to the three TODO locations in the code. Of course, you also need to change the option name to replace Classic and classic.

<? Php/*** sequence group type */class ClassicOptions {/* -- get Sequence Group -- */function getOptions () {// Obtain the Sequence Group $ options = get_option ('classic _ options') in the database; // if the sequence group does not exist in the database, set the default values of these options, and insert them into the database if (! Is_array ($ options) {$ options ['notice '] = false; $ options ['notice _ content'] = ''; // TODO: append other options update_option ('classic _ options', $ options);} // return the response group return $ options;}/* -- initialize -- */function init () {// if the data is submitted by POST, restrict the data and update it to the database if (isset ($ _ POST ['classic _ save']) {// Obtain the Sequence Group, because it is possible to modify only some options, take the entire option and then change $ options = ClassicOptions: getOptions (); // data restriction if ($ _ POST ['notice']) {$ options ['not Ice '] = (bool) true;} else {$ options ['notice'] = (bool) false ;} $ options ['notice _ content'] = stripslashes ($ _ POST ['notice _ content']); // TODO: append other options here for processing restrictions // update_option ('classic _ options', $ options) for updating data; // otherwise, obtain the quota group again, that is, initialize the data} else {ClassicOptions: getOptions () ;}// append a tab on the background Design page, called Current Theme Options add_theme_page ("Current Theme Options ", "Current Theme Options", 'edit _ theme S', basename (_ FILE _), array ('classicoptions ', 'display');}/* -- tab -- */function display () {$ options = ClassicOptions: getOptions ();?> <Form action = "#" method = "post" enctype = "multipart/form-data" name = "classic_form" id = "classic_form"> <div class = "wrap"> 

Frontend processing

 

To display the announcement board on the home page, you need to modify the index. php, this is relatively simple. It is only through some judgment statements that determine whether something should be displayed. of course, you can perform other operations. The key is to get the options and process them.

There are two steps:

Obtain the option (for each PHP file, you can obtain it once and execute it at the top of the file)
Process the options (the announcement content will be displayed if it is determined to be true)

<! -- Get option --> <? Php $ options = get_option ('classic _ options');?> <! -- If you select to display the announcement board and the announcement board contains content, the display will be --> <? Php if ($ options ['notice'] & $ options ['notice _ content']):?> <Div id = "notice"> <div class = "content"> <? Php echo ($ options ['notice _ content']);?> </Div> <? Php endif;?>


You can use the background option Development Framework to add more complex and powerful option settings.

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.