ТОП просматриваемых книг сайта:
Professional WordPress Plugin Development. Brad Williams
Читать онлайн.Название Professional WordPress Plugin Development
Год выпуска 0
isbn 9781119666936
Автор произведения Brad Williams
Жанр Программы
Издательство John Wiley & Sons Limited
Adding Fields to an Existing Page
You have seen how to create a new custom settings page for a plugin and its associated entry in the administration menus. Doing so makes sense if your plugin features a lot of settings and its administration page shows a lot of content.
Sometimes, though, it is not worth adding a new menu entry for just one or a few plugin options. Here again the Settings API will prove to be useful, allowing plugin setting fields to easily be added to the existing WordPress setting pages.
How It Works
Two internal functions, do_settings_sections()
and do_settings_fields()
, are triggered to draw sections and fields that have been previously registered, like you did in the example plugin.
Each core setting page calls these two functions, so you can hook into them if you know their slug name.
Adding a Section to an Existing Page
Your previous plugin was adding a whole new section and its input field on a stand‐alone page. You now modify it to insert this content into WordPress’ Privacy Settings page.
<?php $args = array( 'type' => 'string', 'sanitize_callback' => 'pdev_plugin_validate_options', 'default' => NULL ); register_setting( 'reading', 'pdev_plugin_options', $args ); add_settings_section( 'pdev_plugin_options', 'PDEV Plugin Settings', 'pdev_plugin_section_text', 'reading' ); add_settings_field( 'pdev_plugin_text_string', 'Your Name', 'pdev_plugin_setting_name', 'reading', 'pdev_plugin_options' ); ?>
Notice that the first parameter passed in the register_setting()
function call is set to reading
. This function now adds your custom section into the reading
section, which is located within the Reading Settings page, as shown in Figure 3‐6. Replace all reading
instances with media
, and your section will be appended at the end of the Media Settings page.
FIGURE 3‐6: Section appended
You still need to whitelist this setting, with register_setting()
. Omitting this step would make WordPress ignore the setting when submitting the form.
Adding Only Fields
Of course, it can even make sense to add just one field and no section header to an existing page. Now modify the function in the previous example as shown here:
<?php function pdev_plugin_admin_init(){ $args = array( 'type' => 'string', 'sanitize_callback' => 'pdev_plugin_validate_options', 'default' => NULL ); register_setting( 'reading', 'pdev_plugin_options', $args ); add_settings_field( 'pdev_plugin_text_string', 'Your Name', 'pdev_plugin_setting_name', 'reading', 'default' ); } ?>
Your singular field will be added to the default
field set of the reading
section, as shown in Figure 3‐7.
FIGURE 3‐7: Singular field
WordPress’ Sections and Setting Fields
To add a section to an existing page or a field to an existing section, all you need to know is the slug name of the page. Table 3‐1 includes every section and field set names found in the WordPress Settings pages.
TABLE 3-1: List of Core Sections and Fields
WORDPRESS’ SETTINGS PAGES | SECTION NAMES | FIELD SET NAMES |
General Settings (options‐general.php )
|
general
|
default
|
Writing Settings (options‐writing.php )
|
writing
|
default remote_publishing post_via_email
|
Reading Settings (options‐reading.php )
|
reading
|