Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Using existing options and option editing pages in WordPress

From: WordPress: Creating Custom Widgets and Plugins with PHP

Video: Using existing options and option editing pages in WordPress

The WordPress options database has a number of options stored in it. Some of these are built into WordPress, and they might be handy to use in your own applications. In addition, you might want to be saving your own settings in here, and having them displayed in the built- in setting pages in the admin. So we're going to talk about how to integrate with these options. Let's go into the WordPress admin and take a look. There is a page you can link to-- you have to go there directly; there is not actually a link--in the admin menu called options.php, which contains all the settings in the database.

Using existing options and option editing pages in WordPress

The WordPress options database has a number of options stored in it. Some of these are built into WordPress, and they might be handy to use in your own applications. In addition, you might want to be saving your own settings in here, and having them displayed in the built- in setting pages in the admin. So we're going to talk about how to integrate with these options. Let's go into the WordPress admin and take a look. There is a page you can link to-- you have to go there directly; there is not actually a link--in the admin menu called options.php, which contains all the settings in the database.

So you can see this is all here. Some of these are key, and you may want to access at different points in your application. You can always use get_option to refer to these just like we referred to our own options. So you can see the admin_e-mail is a good one. The blogdescription can be helpful. The blogname can be helpful. We also have information about the web site, what the URL is, stylesheet, all kinds of good information. In this page itself, you can actually edit this data directly.

So if you ever want to just quick change an option, you can do it right from here. If you want to integrate your own options into the built-in Settings pages, you can do that, if you remember our cc_ comment plugin that we created allows us to edit a cccomm_cc_e-mail. Let's move this into the settings page. So first we can delete our existing form page. We won't need any longer. Then we can delete what's in the existing function to handle adding it to the menu, because it won't need to be in the menu any longer.

The first thing we're going to need to do is create a function that will generate an input field to handle this setting. So we'll create this, and we'll call it cccomm_setting_field. In here we're simply going to create input type = "text" name = "cccomm_cc_e-mail" id = "cccomm_cc_e-mail," and the value we're going to use get_option.

So echo get_option cccomm_cc_e-mail, and then close a tag, and then we'll want to of course go back into php. So we don't need to do anything else with this. You just need to make sure that the ID and the name field match the option. Then to add this into the Settings page, we're going to use the special add_settings_field. You can specify an id, cccomm_cc_e-mail.

You're going to specify what you want to read for the label of that field. Then you want to specify the function that's going to generate the field. So this is going to be cccomm_setting_ field, and finally the section it's going to be, which will be the general section. Notice we're already adding this into the admin_menu hook, so that should put it into our menu for us.

So if we go back to our General Settings and we refresh it and we scroll down, we should see our CC Comments has now been added to this page. If you want to have multiple settings and create different sections, there is another function that enables you to create an entirely new section heading. So you'll create a function that outputs the section, and here you can just do something simple Settings.

Then we'll use this add_settings _section to add it directly in. So we say cccomm, which is an ID of the section. We have a name. We have the function that's going to generate. It's going to run once, essentially, at the beginning of this heading. So this will be cccomm_setting_section, what we just wrote, and where you want it to appear, in this case in the general section.

The only other thing you need to do at this point is specify if your fields are going to fall under that section. So there is another option here where you can specify that, and we just use the ID that we created for that section. Now if we go back to our Settings page, we can reload, and you'll see we have our CC Comments section. Notice it outputs the name and then whatever you had in your code, and then we would have whatever fields afterwards. So we can access any of these settings that are stored in the options database.

We also, since WordPress 2.7, have the ability to add our own items directly into the Settings pages.

Show transcript

This video is part of

Image for WordPress: Creating Custom Widgets and Plugins with PHP
 
Expand all | Collapse all
  1. 1m 49s
    1. Welcome
      1m 16s
    2. Using the exercise files
      33s
  2. 23m 29s
    1. WordPress overview
      2m 32s
    2. Installing WPI for Windows
      3m 42s
    3. Installing MAMP for the Mac
      3m 25s
    4. Installing and configuring WordPress
      5m 51s
    5. Comparing WordPress 3.0 with previous versions
      2m 57s
    6. Setting up a PHP/WordPress development environment
      5m 2s
  3. 14m 47s
    1. Exploring WordPress plugins
      3m 42s
    2. Administering plugins from the WordPress admin
      5m 23s
    3. Exploring where plugins reside
      2m 51s
    4. Introduction to hooks
      2m 51s
  4. 39m 28s
    1. Creating the plugin PHP file(s)
      3m 12s
    2. More on hooks: Actions and filters
      3m 15s
    3. Installation and activation
      4m 6s
    4. Writing activation code
      3m 45s
    5. Writing an action
      5m 12s
    6. Writing a filter
      4m 15s
    7. About pluggable functions
      2m 1s
    8. Writing a pluggable function
      5m 30s
    9. Using template tags
      2m 46s
    10. Introducing shortcode
      5m 26s
  5. 26m 2s
    1. Widgets and the WordPress Widgets SubPanel
      2m 54s
    2. Comparing widgets and plugins
      1m 8s
    3. Using and customizing built-in widgets
      3m 18s
    4. Creating a new widget
      7m 21s
    5. Writing the constructor and registering widgets
      5m 20s
    6. Enabling configuration of widgets
      6m 1s
  6. 44m 59s
    1. Creating an admin interface
      5m 25s
    2. Saving data to the database
      5m 39s
    3. Securing form submission with nonces
      2m 25s
    4. Options editing post-WordPress 2.7
      4m 8s
    5. Integrating with the WordPress admin menus
      3m 34s
    6. WordPress admin dashboard API
      4m 5s
    7. Using existing options and option editing pages in WordPress
      5m 19s
    8. Using jQuery and AJAX for administration
      14m 24s
  7. 27m 13s
    1. Accessing the WordPress database
      5m 45s
    2. Using the built-in schema
      2m 21s
    3. Accessing data using $wpdb
      5m 15s
    4. Creating new tables
      7m 18s
    5. Inserting data
      6m 34s
  8. 26m 27s
    1. Introducing the Loop
      6m 22s
    2. Using WP_Query()
      3m 11s
    3. Custom filtering and sticky posts
      4m 58s
    4. Using jQuery and AJAX for posts and pages
      11m 56s
  9. 12m 9s
    1. Registering and promoting plugins
      2m 28s
    2. Creating an uninstall function
      5m 53s
    3. Backward compatibility issues
      3m 48s
  10. 15m 3s
    1. Understanding security issues
      11m 20s
    2. Internationalizing your plugin
      3m 43s
  11. 18s
    1. Goodbye
      18s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed WordPress: Creating Custom Widgets and Plugins with PHP.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.