Professional WordPress Plugin Development. Brad Williams
Чтение книги онлайн.
Читать онлайн книгу Professional WordPress Plugin Development - Brad Williams страница 27
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
|
default
|
Discussion Settings (options‐discussion.php )
|
discussion
|
default avatars
|
Media Settings (options‐media.php )
|
media
|
default embeds uploads
|
Permalink Settings (options‐permalink.php )
|
permalink
|
optional
|
User Interface Concerns
Electing to add your plugin settings to a separate page or to a core WordPress page is often a matter of choosing the right user interface for the right end user.
When working on a site for a client, you may focus on delivering a key‐in‐hand CMS solution and not on explaining what is WordPress and what is a plugin extending its features. Adding your plugin settings to a core Settings page can enhance its integration into WordPress’ backend because it won't appear different from other core settings. From the client's point of view, your plugin is a core element just as any other built‐in feature.
On the contrary, if you intend to make your plugin available for download, you can target people who probably understand the concept of adding new features to a core set. These people will naturally search for a custom menu where they can manage your plugin. If you opt for adding fields to an existing page, be sure to explicitly tell users about it, for instance in the plugin documentation.
Removing Settings
As a professional plugin developer, it's important to create a high‐quality experience for your users, which includes tasks that are rarely noticed. In this case you're going to learn how to remove settings that your plugin has created when it is uninstalled in WordPress.
You're going to use register_uninstall_hook()
, which was covered in Chapter 2, “Plugin Framework.” First you need to register your uninstall function as shown here:
register_uninstall_hook( __FILE__, 'pdev_plugin_uninstall' );
Next you'll use the unregister_setting()
function of the Settings API. The unregister_setting()
function accepts the following parameters:
option_group: Settings group name used during registration
option_name: Name of the option