6 3 Dashboard and Settings 3 Dashboard and Settings WHAT'S IN THIS CHAPTER? Creating menus and submenus Saving and retrieving plugin settings Saving global and per‐user options Designing and styling your plugin
ADDING MENUS AND SUBMENUS ADDING MENUS AND SUBMENUS Many plugins you create need some type of menu item, which generally links to your plugin's settings page where users can configure your plugin options. WordPress features two methods for adding a plugin menu: a top‐level menu or a submenu item.
PLUGIN SETTINGS PLUGIN SETTINGS Now that you've learned how to create menus and submenus in the WordPress Dashboard, it's time to create a settings page for your plugin. WordPress enables easy access to the database to store and retrieve data, such as options end users can modify and save in settings pages or internal information plugins you need to know. You'll learn how to save and fetch this data using the Options API and internal WordPress functions.
THE OPTIONS API THE OPTIONS API The Options API is a set of functions that enable easy access to the database where WordPress, plugins, and themes save and fetch needed information. Options are stored in a database table named, by default, wp_options and can be text, integers, arrays, or objects. For example, WordPress keeps in this table the title of your blog, the list of active plugins, the news articles displayed on the Dashboard, or the time to check if a new version is available. You'll now learn how to use the functions to access, update, and save options: add_option() , update_option() , get_option() , and delete_option() .
THE SETTINGS API THE SETTINGS API Options can be internally created and updated by your plugin (for instance, storing the time stamp of the next iteration of a procedure). But they are also frequently used to store settings the end user will modify through your plugin administration page. When creating or updating user‐defined options for a plugin, relying on the Settings API can make your code both simpler and more efficient.
KEEPING IT CONSISTENT KEEPING IT CONSISTENT 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. Throughout this section, you'll modify the pdev_styling_settings() function.
SUMMARY SUMMARY This chapter covered many different methods for integrating your plugin in WordPress. You certainly won't use every method discussed in every plugin you develop, but it's essential to understand what's available for use in your plugin.
7 4 Security and Performance
SECURITY OVERVIEW
USER PERMISSIONS
NONCES
DATA VALIDATION AND SANITIZATION
FORMATTING SQL STATEMENTS
SECURITY GOOD HABITS
PERFORMANCE OVERVIEW
CACHING
TRANSIENTS
SUMMARY
8 5 Hooks
UNDERSTANDING HOOKS
ACTIONS
FILTERS
USING HOOKS FROM WITHIN A CLASS
USING HOOKS WITH ANONYMOUS FUNCTIONS
CREATING CUSTOM HOOKS
FINDING HOOKS
SUMMARY
9 6 JavaScript
REGISTERING SCRIPTS
ENQUEUEING SCRIPTS
LIMITING SCOPE
LOCALIZING SCRIPTS
INLINE SCRIPTS
OVERVIEW OF BUNDLED SCRIPTS
POLYFILLS
YOUR CUSTOM SCRIPTS
jQuery
BACKBONE/UNDERSCORE
REACT
SUMMARY
10 7 Blocks and Gutenberg
WHAT IS GUTENBERG?
TOURING GUTENBERG
PRACTICAL EXAMPLES
TECHNOLOGY STACK OF GUTENBERG
“HELLO WORLD!” BLOCK
WP‐CLI SCAFFOLDING
CREATE‐GUTEN‐BLOCK TOOLKIT
BLOCK DIRECTORY
SUMMARY
11 8 Content
CREATING CUSTOM POST TYPES
POST METADATA
META BOXES
CREATING CUSTOM TAXONOMIES
USING CUSTOM TAXONOMIES
A POST TYPE, POST METADATA, AND TAXONOMY PLUGIN
SUMMARY
12 9 Users and User Data
WORKING WITH USERS
ROLES AND CAPABILITIES
LIMITING ACCESS
CUSTOMIZING ROLES
SUMMARY
13 10 Scheduled Tasks
WHAT IS CRON?
SCHEDULING CRON EVENTS
TRUE CRON
PRACTICAL USE
SUMMARY
14 11 Internationalization
INTERNATIONALIZATION AND LOCALIZATION
CREATING TRANSLATION FILES
SUMMARY
15 12 REST API
WHAT THE REST API IS
WHAT YOU CAN DO WITH THE REST API
ACCESSING THE WORDPRESS REST API
THE HTTP API
WORDPRESS’ HTTP FUNCTIONS
BRINGING IT ALL TOGETHER
SUMMARY
16 13 Multisite
TERMINOLOGY
ADVANTAGES OF MULTISITE
ENABLING MULTISITE IN WORDPRESS
MULTISITE FUNCTIONS
DATABASE SCHEMA
QUERY CLASSES
OBJECT CLASSES
SUMMARY
17 14 The Kitchen Sink
QUERYING AND DISPLAYING POSTS
SHORTCODES
WIDGETS
DASHBOARD WIDGETS
REWRITE RULES
THE HEARTBEAT API
SUMMARY
18 15 Debugging
COMPATIBILITY
DEBUGGING
ERROR LOGGING
QUERY MONITOR
SUMMARY
19 16 The Developer Toolbox
CORE AS REFERENCE
PLUGIN DEVELOPER HANDBOOK
CODEX
TOOL WEBSITES
COMMUNITY RESOURCES
TOOLS
SUMMARY
20 INDEX
21 END USER LICENSE AGREEMENT
1 Chapter 3 TABLE 3-1: List of Core Sections and Fields 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
2 Chapter 12TABLE 12-1: Main HTTP Status CodesTABLE 12-2: HTTP Status Code ClassesTABLE 12-3: Default Settings of wp_remote_ Functions Optional Parameters
1 Chapter 1 FIGURE 1-1: Loading a page in WordPress FIGURE 1‐1 : Loading a page in WordPress The flow changes slightly when loading an admin page. The differences are minor and primarily concern what theme is loaded: admin theme versus your website theme.
FIGURE 1-2: Plugins menu FIGURE 1‐2 : Plugins menu
FIGURE 1-3: Install Now button FIGURE 1‐3 : Install Now button The third and final method to install a plugin in WordPress uses Secure (or SSH) File Transfer Protocol (SFTP). Using SFTP involves simply connecting to your web server using an SFTP client and manually uploading the plugin to your WordPress installation. To use this method, upload the uncompressed plugin folder or file to the wp‐content/plugins directory on your web server.
FIGURE 1-4: Types and statuses for plugins FIGURE 1‐4 : Types and statuses for plugins Active: Plugin is active and running in WordPress. Inactive: Plugin is installed but not active. No code from the plugin is executed. Recently Active: A temporary status given to any plugin that has been recently deactivated. Must‐Use: All plugins installed in the wp‐content/mu‐plugins directory. All Must‐Use, or MU, plugins are loaded automatically. The only way to deactivate an MU plugin is to remove it completely from the directory. Drop‐ins: Core functionality of WordPress can be replaced by Drop‐in plugins. These plugins are specifically named PHP files located in the wp‐content directory. If WordPress detects one of these files, it will be automatically loaded and listed under the Drop‐in filter on the Plugin screen. Currently ten Drop‐in plugins are available.advanced‐cache.php: Advanced caching plugindb.php: Custom database classdb‐error.php: Custom database error messageinstall.php: Custom installation scriptmaintenance.php: Custom maintenance messageobject‐cache.php: External object cachesunrise.php: Advanced domain mappingblog‐deleted.php: Custom blog deleted messageblog‐inactive.php: Custom blog inactive messageblog‐suspended.php: Custom blog suspended message The last four Drop‐in plugins are specific to the WordPress Multisite feature. A standard WordPress installation will have no use for these plugins. When developing a new plugin, determine what type of plugin you want to create before you start the development process. Most plugins will be standard WordPress plugins, but occasionally you might need to create a Must‐Use or Drop‐in plugin.
Читать дальше