Professional WordPress Plugin Development
Реклама. ООО «ЛитРес», ИНН: 7719571260.
Оглавление
Brad Williams. Professional WordPress Plugin Development
Table of Contents
List of Tables
List of Illustrations
Guide
Pages
PROFESSIONAL WORDPRESS® PLUGIN DEVELOPMENT
FOREWORD
INTRODUCTION
WHO THIS BOOK IS FOR
WHAT YOU NEED TO USE THIS BOOK
WHAT THIS BOOK COVERS
HOW THIS BOOK IS STRUCTURED
CONVENTIONS
SOURCE CODE
ERRATA
1 An Introduction to Plugins. WHAT'S IN THIS CHAPTER?
WHAT IS A PLUGIN?
How Plugins Interact with WordPress
When Are Plugins Loaded?
AVAILABLE PLUGINS
Official Plugin Directory
Popular Plugin Examples
Popular Plugin Tags
ADVANTAGES OF PLUGINS
Not Modifying Core
Why Reinvent the Wheel?
Separating Plugins and Themes
Easy Updates
Easier to Share and Reuse
Plugin Sandbox
Plugin Community
INSTALLING AND MANAGING PLUGINS
Installing a Plugin
Managing Plugins
Editing Plugins
Plugin Directories
Types of Plugins
SUMMARY
2 Plugin Framework. WHAT'S IN THIS CHAPTER?
CODE DOWNLOADS FOR THIS CHAPTER
REQUIREMENTS FOR PLUGINS
Naming Your Plugin
Using a Folder
BEST PRACTICES
Namespace Everything
File Organization
Folder Structure
PLUGIN HEADER
Creating the Header
Plugin License
DETERMINING PATHS
Plugin Paths
Local Paths
URL Paths
ACTIVATE/DEACTIVATE FUNCTIONS
Plugin Activation Function
Plugin Deactivation Function
Deactivate Is Not Uninstall
UNINSTALL METHODS
Why Uninstall Is Necessary
Uninstall.php
Uninstall Hook
CODING STANDARDS
Document Your Code
Naming Variables and Functions
Naming Classes and Methods
Naming Files
Single and Double Quotes
Indentation
Brace Style
Space Usage
Shorthand PHP
SQL Statements
SUMMARY
3 Dashboard and Settings. WHAT'S IN THIS CHAPTER?
CODE DOWNLOADS FOR THIS CHAPTER
ADDING MENUS AND SUBMENUS
Creating a Top‐Level Menu
Adding a Submenu
Adding a Menu Item to an Existing Menu
PLUGIN SETTINGS
THE OPTIONS API
Saving Options
Saving an Array of Options
Updating Options
Retrieving Options
Loading an Array of Options
Deleting Options
The Autoload Parameter
Segregating Plugin Options
Toggling the Autoload Parameter
THE SETTINGS API
Benefits of the Settings API
Settings API Functions
Creating the Plugin Administration Page
Registering New Settings
Defining Sections and Settings
Validating User Input
Rendering the Form
All Done!
Wrapping It Up: A Complete Plugin Management Page
Improving Feedback on Validation Errors
Expanding with Additional Field Types
Adding Fields to an Existing Page
How It Works
Adding a Section to an Existing Page
Adding Only Fields
WordPress’ Sections and Setting Fields
User Interface Concerns
Removing Settings
KEEPING IT CONSISTENT
Using the WordPress UI
Headings
Dashicons
Messages
Buttons
Form Fields
Tables
Pagination
SUMMARY
4 Security and Performance. WHAT'S IN THIS CHAPTER?
CODE DOWNLOADS FOR THIS CHAPTER
SECURITY OVERVIEW
What Securing Your Plugin Is
What Securing Your Plugin Is Not
USER PERMISSIONS
How to Check current_user_can()
Do Not Check Too Early
NONCES
Authority vs. Intention
What Is a Nonce?
How to Create and Verify Nonces
Creating a Nonce URL
Creating a Nonce Field
Creating and Verifying a Nonce in a Plugin
DATA VALIDATION AND SANITIZATION
The Need for Data Validation and Sanitization
Good Practice: Identifying Potentially Tainted Data
Validating or Sanitizing Input?
Validating and Sanitizing Cookbook
Integers
Arbitrary Text Strings
Key and Identifier Strings
Email Strings
URLs
URLs in HTML
URLs in a Database
URLs in Redirects
Escaping HTML and Attributes
HTML
Forcing Balanced Tags
Sanitizing HTML
JavaScript
Environment and Server Variables
Arrays of Data
Database Queries
FORMATTING SQL STATEMENTS
The $wpdb Object
Why wpdb Methods Are Superior
All‐in‐One Methods
$wpdb‐>update()
$wpdb‐>insert()
Common Methods
Select a Variable
Select a Row
Select a Column
Select Generic Results
Generic Queries
Protecting Queries against SQL Injections
SECURITY GOOD HABITS
PERFORMANCE OVERVIEW
CACHING
Saving Cached Data
Loading and Using Cached Data
Deleting Cached Data
Caching Data within a Plugin
TRANSIENTS
Saving an Expiring Option
Retrieving an Expiring Option
Deleting an Expiring Option
A Practical Example Using Transients
Technical Details
Transient Ideas
SUMMARY
5 Hooks. WHAT'S IN THIS CHAPTER?
CODE DOWNLOADS FOR THIS CHAPTER
UNDERSTANDING HOOKS
ACTIONS
What Is an Action?
Action Hook Functions
remove_action()
remove_all_actions()
do_action_ref_array
has_action
did_action()
current_action
register_activation_hook and register_deactivation_hook
Commonly Used Action Hooks
plugins_loaded
init
admin_menu
save_post
wp_head
FILTERS
What Is a Filter?
Filter Hook Functions
remove_filter
remove_all_filters
apply_filters_ref_array
has_filter
current_filter
Quick Return Functions
Commonly Used Filter Hooks
the_content
template_include
USING HOOKS FROM WITHIN A CLASS
USING HOOKS WITH ANONYMOUS FUNCTIONS
CREATING CUSTOM HOOKS
Benefits of Creating Custom Hooks
Custom Action Hook Example
Custom Filter Hook Example
FINDING HOOKS
Searching for Hooks in the Core Code
Variable Hooks
Hook Reference Lists
SUMMARY
6 JavaScript. WHAT'S IN THIS CHAPTER?
REGISTERING SCRIPTS
ENQUEUEING SCRIPTS
LIMITING SCOPE
LOCALIZING SCRIPTS
INLINE SCRIPTS
OVERVIEW OF BUNDLED SCRIPTS
jQuery UI and Other Scripts
The WP Global
a11y Speak
Escaping
i18n
Heartbeat
POLYFILLS
YOUR CUSTOM SCRIPTS
jQuery
Benefits of Using jQuery
jQuery Crash Course
The jQuery Object
Syntax and Chaining
No‐Conflict Mode in WordPress
Launching Code on Document Ready
Ajax
BACKBONE/UNDERSCORE
REACT
SUMMARY
7 Blocks and Gutenberg. WHAT'S IN THIS CHAPTER?
CODE DOWNLOADS FOR THIS CHAPTER
WHAT IS GUTENBERG?
GUTENBERG LINKS
TOURING GUTENBERG
GLOSSARY
PRACTICAL EXAMPLES
WooCommerce
The Events Calendar
Post Type Switcher
TECHNOLOGY STACK OF GUTENBERG
JavaScript
PHP
Node.js
webpack
Babel
React
JSX
ES6
“HELLO WORLD!” BLOCK
PHP
LISTING 7‐1 Enqueueing our JavaScript
JavaScript
LISTING 7‐2 Registering our block type
webpack
LISTING 7‐3 webpack configuration file
Command Line
Activation
Wrap‐Up
WP‐CLI SCAFFOLDING
Plugin
Blocks
Includes
Activation
Wrap‐Up
CREATE‐GUTEN‐BLOCK TOOLKIT
Installation
Activation
Wrap‐Up
BLOCK DIRECTORY
SUMMARY
8 Content. WHAT'S IN THIS CHAPTER?
CODE DOWNLOADS FOR THIS CHAPTER
CREATING CUSTOM POST TYPES
Post Type Possibilities
Registering a Post Type
register_post_type
Registering the Book Collection Post Type
Setting Post Type Labels
Using Custom Capabilities
Attaching Existing Taxonomies
POST METADATA
Registering Post Metadata
Adding Post Metadata
Retrieving Post Metadata
Updating Post Metadata
Deleting Post Metadata
META BOXES
What Is a Meta Box?
Adding a Custom Meta Box
Saving Meta Box Data
CREATING CUSTOM TAXONOMIES
Understanding Taxonomies
Registering a Custom Taxonomy
register_taxonomy
Registering the Genre Taxonomy
Assigning a Taxonomy to a Post Type
USING CUSTOM TAXONOMIES
Retrieving a Taxonomy
Using a Taxonomy with Posts
Taxonomy Conditional Tags
taxonomy_exists
is_taxonomy_hierarchical
is_tax
A POST TYPE, POST METADATA, AND TAXONOMY PLUGIN
SUMMARY
9 Users and User Data. WHAT'S IN THIS CHAPTER?
CODE DOWNLOADS FOR THIS CHAPTER
WORKING WITH USERS
User Functions
is_user_logged_in()
get_users()
count_users
Creating, Updating, and Deleting Users
Creating a New User
Updating an Existing User
Deleting an Existing User
User Data
Getting a User Object and Data
Getting the Current User Object
Getting User Post Counts
User Metadata
Adding User Metadata
Retrieving User Metadata
Updating User Metadata
Deleting User Metadata
Creating a Plugin with User Metadata
ROLES AND CAPABILITIES
What Are Roles and Capabilities?
Default Roles
Custom Roles
LIMITING ACCESS
Checking User Permissions
Is the User an Admin?
CUSTOMIZING ROLES
Creating a Role
Deleting a Role
Adding Capabilities to a Role
Removing Capabilities from a Role
A Custom Role and Capability Plugin
SUMMARY
10 Scheduled Tasks. WHAT'S IN THIS CHAPTER?
CODE DOWNLOADS FOR THIS CHAPTER
WHAT IS CRON?
How Is Cron Executed?
SCHEDULING CRON EVENTS
Scheduling a Recurring Event
Scheduling a Single Event
Unscheduling an Event
Specifying Your Own Cron Intervals
Viewing Scheduled Cron Events
TRUE CRON
PRACTICAL USE
The Blog Pester Plugin
Deleting Post Revisions Weekly
The Delete Comments Plugin
SUMMARY
11 Internationalization. WHAT'S IN THIS CHAPTER?
CODE DOWNLOADS FOR THIS CHAPTER
INTERNATIONALIZATION AND LOCALIZATION
Why Internationalize?
Understanding Internationalization in Professional Work
Getting Your Plugin Ready for Translation
Echoing and Returning Strings
The __() Function
The _e() Function
The esc_attr__() Function
The esc_attr_e() Function
The esc_html__() Function
The esc_html_e() Function
The _x() Function
The _ex() Function
The esc_attr_x() Function
The esc_html_x() Function
The _n() Function
The _nx() Function
The _n_noop() Function
The _nx_noop() Function
Using Placeholders
Internationalizing JavaScript
Developer Handbook Resource
CREATING TRANSLATION FILES
The MO and PO Files
Translation Tools
How to Create a POT File
Command Line
Where to Store Translation Files
SUMMARY
12 REST API. WHAT'S IN THIS CHAPTER?
CODE DOWNLOADS FOR THIS CHAPTER
WHAT THE REST API IS
WHAT YOU CAN DO WITH THE REST API
ACCESSING THE WORDPRESS REST API
Default Endpoints
REST API Clients
Insomnia
Postman
Authentication
Enhanced Authentication
Custom Endpoints
THE HTTP API
What Is an HTTP Request?
HTTP Request Concepts
Dissecting an HTTP Transaction
The Client Sends a Request
The Server Sends a Response
Some Caveats on Checking HTTP Responses
Possibilities for Crafting HTTP Requests
How to Make HTTP Requests in PHP
Using the HTTP Extension
Using fopen() Streams
Using a Standard fopen()
Using fsockopen()
Using the CURL Extension
Too Many Ways?
WORDPRESS’ HTTP FUNCTIONS
The wp_remote_ Functions
wp_remote_* Input Parameters
wp_remote_* Return Values
Remote API Request Example
Unsuccessful Requests
Successful Requests
wp_remote_ Companion Functions
Advanced Configuration and Tips
Proxy Support
Filtering Requests and Responses
Example: Modify a Default Parameter
Example: Log HTTP Requests and Responses
BRINGING IT ALL TOGETHER
Create
Update
Delete
Resources
SUMMARY
13 Multisite. WHAT'S IN THIS CHAPTER?
CODE DOWNLOADS FOR THIS CHAPTER
TERMINOLOGY
ADVANTAGES OF MULTISITE
ENABLING MULTISITE IN WORDPRESS
MULTISITE FUNCTIONS
The Site ID
Common Functions
Switching and Restoring Sites
Network Content Shortcode Examples
A Network Content Widget Example
Creating a New Site
Site Options
Network Options
Site Meta
Users and Roles
Super Admin
Checking the Site Owner
Network Stats
DATABASE SCHEMA
Multisite‐Specific Tables
Site‐Specific Tables
QUERY CLASSES
WP_Site_Query
WP_Network_Query
OBJECT CLASSES
WP_Site
WP_Network
SUMMARY
14 The Kitchen Sink. WHAT'S IN THIS CHAPTER?
CODE DOWNLOADS FOR THIS CHAPTER
QUERYING AND DISPLAYING POSTS
Use Case for Displaying Posts
WP_Query Overview
The Loop
SHORTCODES
What Shortcodes Are
Register Custom Shortcodes
Building a Simple Shortcode
Building a Shortcode with Parameters
Building a Shortcode with Content
Shortcode Tips
Think Simplicity for the User
Remember That Shortcodes Are Dynamic
Look under the Hood
remove_shortcode()
remove_all_shortcodes()
strip_shortcodes()
do_shortcode()
WIDGETS
Creating a Widget
DASHBOARD WIDGETS
Creating Dashboard Widgets
Creating a Dashboard Widget with Options
REWRITE RULES
Why Rewrite URLs
Permalink Principles
Search Engine Friendly
User Friendly
Apache's mod_rewrite
URL Rewriting in WordPress
How WordPress Handles Queries
Overview of the Query Process
The Rewrite Object
The Query Object
What Plugins Can Do
Practical Uses
Rewriting a URL to Create a List of Shops
Creating a New Permalink Structure and Integrating Non‐WordPress Pages
THE HEARTBEAT API
What Is the Heartbeat API?
Using the Heartbeat API
Sending Data
Receiving and Responding to Data
Processing the Response
Full Heartbeat API Plugin
SUMMARY
15 Debugging. WHAT'S IN THIS CHAPTER?
CODE DOWNLOADS FOR THIS CHAPTER
COMPATIBILITY
Supporting Many WordPress Versions
Playing Nicely with Other WordPress Plugins
Keeping Current with WordPress Development
Deprecation
Dealing with Obsolete Client Installs
DEBUGGING
Enabling Debugging
Displaying Debug Output
Understanding Debug Output
ERROR LOGGING
Enabling Error Logging
Setting Log File Location
Understanding the Log File
QUERY MONITOR
SUMMARY
16 The Developer Toolbox. WHAT'S IN THIS CHAPTER?
CORE AS REFERENCE
PHP Inline Documentation
JavaScript Inline Documentation
Finding Functions
Common Core Files
formatting.php
functions.php
pluggable.php
plugin.php
post.php
user.php
PLUGIN DEVELOPER HANDBOOK
Navigating the Handbook
Code Reference
CODEX
Searching the Codex
Function Reference
TOOL WEBSITES
PHPXref
Hooks Database
COMMUNITY RESOURCES
Make WordPress
Support Forums
WordPress Slack
WordPress Development Updates
WordPress Ideas
Community News Sites
WordPress News
WordPress Planet
Post Status
Know the Code
LinkedIn Learning
Local Events
TOOLS
Browser
Editor
NetBeans IDE
PhpStorm
Notepad++
TextMate
Sublime Text
Visual Studio Code
Deploying Files with FTP, SFTP, and SSH
phpMyAdmin
SUMMARY
INDEX
ABOUT THE AUTHORS
ACKNOWLEDGMENTS
WILEY END USER LICENSE AGREEMENT
Отрывок из книги
Second Edition
Brad Williams
.....
As you can see, the inline PHPDoc describes what the function does, what parameters it accepts, and what it returns. Another developer wouldn't even have to read your code to understand what it does.
.....