Professional WordPress Plugin Development. Brad Williams

Чтение книги онлайн.

Читать онлайн книгу Professional WordPress Plugin Development - Brad Williams страница 27

Professional WordPress Plugin Development - Brad Williams

Скачать книгу

      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.

Screenshot of the Reading Settings page displaying the search engine visibility of the PDEV Plugin Settings.

      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:

Screenshot of a singular field being added to the default field set of the reading section.

      WordPress’ Sections and Setting 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

      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

Скачать книгу