Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WordPress Settings API

WordPress Settings API

- Settings API Overview
- Why is needed ?
- Advantage of Settings API
- What is used of it.
- Settings API mechanism (Where we need to write code, where store Setting API data. etc..)
- Explain with example
- Live demo with code.

Tweet

More Decks by Ahmedabad WordPress Meetup

Other Decks in Programming

Transcript

  1. Can WordPress is the best choice for the project ?

    • Easy To Use. The most vibrant feature of WordPress is its ease. ... • Multiple Theme Options. ... • Plugins ... • WooCommerce for E-commerce. ... • Build any type of websites ... • Community Support. ... • SEO-Friendly.
  2. Basic Things of Setting API 1. WordPress 2.7, Dec 10

    2008 2. The functions are found in wp-admin/includes/plugin.php and wp-admin/includes/template.php 3. allows admin pages containing settings forms to be managed semi-automatically. 4. settings pages, sections within those pages and fields within the sections, all are the part of API 5. using the Settings API, the form posts to wp-admin/options.php 6. Users will need 'manage_options' capability 7. MultiSite will have to be a Super Admin 8. Function Reference : register_setting(), add_settings_field(), add_settings_section()
  3. Why should we use the Settings API? • Communicates with

    WordPress Core • Free Stuff Out-Of-The-Box • More Secure, More Resilient • Consistent with other Wordpress Settings
  4. Pages • Add settings to existing pages – General, Writing,

    Reading, Discussion, Media, Permalinks • Add settings to new pages – Create own page (theme option)
  5. Sections Settings Sections are the groups of settings you see

    on WordPress settings pages with a shared heading
  6. Wow. No coding involved? • Semi-automatic! • You still need

    to: ◦ Write HTML for your fields ◦ Fetch existing values ◦ Write validations • Saving to wp_options is done automatically
  7. Add Section add_settings_section() add_settings_section( $id, $title, $callback, $page ) •

    $id - String for use in the 'id' attribute of tags. • $title - Title of the section. • $callback - Function that fills the section with the desired content. The function should echo its output. • $page - The type of settings page on which to show the section (general, reading, writing, media etc.)
  8. Adding Setting Fields add_settings_field( $id, $title, $callback, $page, $section =

    'default', $args = array() ) • $id - String for use in the 'id' attribute of tags. • $title - Title of the field. • $callback - Function that fills the field with the desired inputs as part of the larger form. Name and id of the input should match the $id given to this function. The function should echo its output. • $page - The type of settings page on which to show the field (general, reading, writing, ...). • $section - The section of the settings page in which to show the box (default or a section you added with add_settings_section, look at the page in the source to see what the existing ones are.) • $args - Extra arguments passed into the callback function
  9. Options Form Rendering To display the sections assigned to the

    page and the settings contained within, the Settings API provides the do_settings_sections() function. do_settings_sections( $page ); $page (string) (required) The slug name of the page whose settings sections you want to output. This should match the page name used in add_settings_section(). Default: None
  10. HTML Code Finally, you need to output the HTML <form>

    tag defining the action destination of options.php and method of POST. Here is an example options form code to generate all the sections and fields added to a page who's slug name is 'my-page': <form method="POST" action="options.php"> <?php settings_fields( 'my-page' ); //pass slug name of page, also referred //to in Settings API as option group name do_settings_sections( 'my-page' ); //pass slug name of page submit_button(); ?> </form> Note: our options form also needs a submit button. You can use the submit_button() function to do this.