Quick Tip: Create A WordPress Global Options Page

WordPress is a great platform to build customized websites in a very efficient way. WordPress manages all this magic in only 11 database tables. wp_options is one of the tables and it acts as the mind of a WordPress powered website.

This table stores all the information related to your website like site name, site description slogan, site URL and lots of other things. All this information can be grabbed easily using the get_option() function, you just need to pass the field name which you want to get. For example: get_option('home') will return the URL of the home page.

But WordPress doesn't give the opportunity to add new options to this table which can be grabbed easily. So here is a tutorial which creates a new page in your WordPress admin and on that page you can save your custom global options.


What Will We Get?

If you want to store your Twitter ID then just follow this tutorial and you will get your Twitter ID using get_option('twitterid') in the active theme of your WordPress installation.


Code

Here is the code block, where I will go through each step of the code. You just need to place this code block in your functions.php file of your active theme and you are done.


Step 1 Add Admin Menu

This step will just add a new menu for the admin with which we can view our page.


Step 2 Assign a Function Which Parses the Admin Form

Here we will assign the custom function which will create a form.


Step 3 Create a Function Which Generates the Form

Please note that this form is for only one field for now. If you want to make it for more fields the you just have to follow the below two steps for each.

1 – Place a new textbox with a unique name. For example if you want to store a Facebook page link then it would be like below:

2 – You need to update the value of the hidden field with the name "page_options", in this case it should be updated to the below value.

You can see that all option names are added here separated by a comma(,). Without this step nothing will work as expected.


How to Use?

After placing the above code in functions.php have a look at the admin page. You will find a new link in your Admin Menu called "Global Custom Options".

Just enter your values in that form and you are good to go for using those values in your theme files like "get_option('twitterid')".


Future Enhancement

This is just a sample code block with which you will come to understand how this feature could work. If you wanted, you could then extend this functionality to code a plugin with which you could create these fields dynamically and generate the form accordingly.

Do you think you'll find this code useful in your themes? Let us know what you think of it in the comments.


Update: While this article shows a quick way to achieve a particular result, it does not strictly adhere to best practices. Recommended reading on this topic: Using The Settings API: Part 1 – Create A Theme Options Page

Tags:

Comments

Related Articles