Professional WordPress Plugin Development. Brad Williams

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

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

Professional WordPress Plugin Development - Brad Williams

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

      // Deregister our settings group and delete all options function pdev_plugin_uninstall() { // Clean de-registration of registered setting unregister_setting( 'pdev_plugin_options', 'pdev_plugin_options' ); // Remove saved options from the database delete_option( 'pdev_plugin_options' ); }

      The unregister_setting() function will unregister your settings group from WordPress. You also need to remove the value from the database using delete_option(). Remember you are using the uninstall hook, which means this code will be executed only if the plugin is uninstalled. If the plugin is deactivated, the options will remain and be available should the user decide to reactivate the plugin.

      They say consistency is one of the principles of good UI design. Creating a plugin for WordPress is no different, and it's a best practice to make your plugin match the WordPress user interface as closely as possible. This helps keep the interface consistent for end users and can make your plugin more professional by providing a solid user experience from the start.

      One of the primary advantages to using the WordPress Settings API is that the UI design elements are handled for you. The headers, description text, form fields, buttons, and notices are all styled exactly as the rest of the WordPress Dashboard. It's also future‐proof, meaning if the WordPress admin design and styles are updated in a future version, your plugin will automatically use the updated styling.

      WordPress features many different styles that you can easily use in your plugin. In this section, you'll learn how to use the styling available in WordPress for your plugins. To demonstrate, create a simple plugin with a settings page.

      <?php add_action( 'admin_menu', 'pdev_styling_create_menu' ); function pdev_styling_create_menu() { //create custom top-level menu add_menu_page( 'Testing Plugin Styles', 'Plugin Styling', 'manage_options', __FILE__, 'pdev_styling_settings' ); }?>

      Throughout this section, you'll modify the pdev_styling_settings() function.

      Using the WordPress UI

      The most important part of using the WordPress styles is to wrap your plugin in the class wrap div.

      <div class="wrap"> Plugin Page </div>

      This class sets the stage for all admin styling.

      Headings

      WordPress has custom styles available for all heading tags. Now look at how those heading tags display:

      <?php function pdev_styling_settings() { ?> <div class="wrap"> <h1>My Plugin</h1> <h2>My Plugin</h2> <h3>My Plugin</h3> <h4>My Plugin</h4> <h5>My Plugin</h5> <h6>My Plugin</h6> </div> <?php } ?>

Illustration depicting the hierarchy of the various heading levels, where each heading is slightly smaller than the previous ones.

      Dashicons

      WordPress features its own open source font icon library called Dashicons. These icons (or dashicons) are used throughout the WordPress Dashboard and are available to use easily in your plugins.

      As an example, let's add some fun dashicons to your various plugin headers.

Illustration displaying custom dashicons class added to the <span> tag to identify the dashicon to be displayed.

      It's important to note that dashicons are automatically loaded within the WordPress Dashboard, but not on the frontend of the website. If you'd like to use dashicons on the public side, you'll need to enqueue the dashicon script, as shown here:

      <?php add_action( 'wp_enqueue_scripts', 'pdev_load_dashicons_front_end' ); function pdev_load_dashicons_front_end() { wp_enqueue_style( 'dashicons' ); } ?>

      Now your plugin has a clean header and uses the Plug icon.

      For more information and a complete list of all Dashicons available, see the official resource at https://developer.wordpress.org/resource/dashicons.

      Messages

      When an action occurs in your plugin, such as saving settings, it's important to display a message to the user verifying whether the update was successful. WordPress features some different styles for displaying these messages.

      As you can see, there are four different types of notices supported: error, warning, success, and info. Notice the class, is‐dismissible, included in the outer <div> element. This class makes the admin notice “dismissable,” meaning a small x is displayed, and when it's clicked, the notice will disappear. When an admin notice is not dismissible, the only way to remove it is to reload the page.

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