WordPress Plugin Development Cookbook - Second Edition

Book description

Learn to create plugins for WordPress 4.x to deliver custom projects or share with the community through detailed step-by-step recipes and code examples

About This Book

  • Learn how to change and extend WordPress to perform virtually any task
  • Explore the plugin API through approachable examples and detailed explanations
  • Mold WordPress to your project's needs or transform it to benefit the entire community

Who This Book Is For

If you are a WordPress user, developer, or a site integrator with basic knowledge of PHP and an interest to create new plugins to address your personal needs, client needs, or share with the community, then this book is for you.

What You Will Learn

  • Discover how to register user callbacks with WordPress, forming the basis of plugin creation
  • Explore the creation of administration pages and adding new content management sections through custom post types and custom database tables
  • Improve your plugins by customizing the post and page editors, categories and user profiles, and creating visitor-facing forms
  • Make your pages dynamic using Javascript, AJAX and adding new widgets to the platform
  • Learn how to add support for plugin translation and distribute your work to the WordPress community

In Detail

WordPress is a popular, powerful, and open Content Management System. Learning how to extend its capabilities allows you to unleash its full potential, whether you're an administrator trying to find the right extension, a developer with a great idea to enhance the platform for the community, or a website developer working to fulfill a client's needs. This book shows readers how to navigate WordPress' vast set of API functions to create high-quality plugins with easy-to-configure administration interfaces.

With new recipes and materials updated for the latest versions of WordPress 4.x, this second edition teaches you how to create plugins of varying complexity ranging from a few lines of code to complex extensions that provide intricate new capabilities.

You'll start by using the basic mechanisms provided in WordPress to create plugins and execute custom user code. You will then see how to design administration panels, enhance the post editor with custom fields, store custom data, and modify site behavior based on the value of custom fields. You'll safely incorporate dynamic elements on web pages using scripting languages, and build new widgets that users will be able to add to WordPress sidebars and widget areas.

By the end of this book, you will be able to create WordPress plugins to perform any task you can imagine.

Style and approach

This cookbook will take you through the creation of your first simple plugin to adding entirely new sections and widgets in the administration interface, so you can learn how to change and extend WordPress to perform virtually any task. Each topic is illustrated through realistic examples showing how to solve common problems, followed by detailed explanations of all concepts used

Publisher resources

Download Example Code

Table of contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Preparing a Local Development Environment
    1. Introduction
    2. Installing a web server on your computer
      1. How to do it...
      2. How it works...
      3. There's more...
        1. Creating a remote web development environment
      4. See also
    3. Downloading and configuring a local WordPress installation
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Creating a local Subversion repository
      1. How to do it...
      2. How it works...
      3. There's more...
        1. Manual repository creation
        2. Other version control systems
      4. See also
    5. Importing initial files to a local Subversion repository
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Checking out files from a Subversion repository
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Subversion file statuses
      5. See also
    7. Committing changes to a Subversion repository
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Viewing the differences in modified files
        2. Updating files to latest repository version
        3. Reverting uncommitted file changes
        4. Viewing file history
    8. Installing a dedicated code editor/text editor
      1. Getting ready
      2. How to do it...
      3. How it works...
  3. Plugin Framework Basics
    1. Introduction
    2. Creating a plugin file and header
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Adding output content to page headers using plugin actions
      1. How to do it...
      2. How it works...
      3. There's more...
        1. Action hooks online listings
        2. Searching for hooks in the WordPress source code
      4. See also
    4. Using WordPress path utility functions to load external files and images
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    5. Modifying the site generator meta tag using plugin filters
      1. How to do it...
      2. How it works...
      3. There's more...
        1. preg_replace function
        2. Filter hooks online listings and the apply_filters function
      4. See also
    6. Adding text after each item's content using plugin filters
      1. How to do it...
      2. How it works...
      3. There's more...
        1. get_the_title and get_permalink functions
      4. See also
    7. Inserting link statistics tracking code in page body using plugin filters
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Troubleshooting coding errors and printing variable content
      1. How to do it...
      2. How it works...
      3. There's more...
        1. Built-in WordPress debugging features
      4. See also
    9. Creating a new simple shortcode
      1. How to do it...
      2. How it works...
      3. See also
    10. Creating a new shortcode with parameters
      1. How to do it...
      2. How it works...
      3. See also
    11. Creating a new enclosing shortcode
      1. How to do it...
      2. How it works...
      3. See also
    12. Loading a style sheet to format plugin output
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    13. Writing plugins using object-oriented PHP
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  4. User Settings and Administration Pages
    1. Introduction
    2. Creating default user settings on plugin initialization
      1. How to do it...
      2. How it works...
      3. There's more...
        1. Deactivation function
      4. See also
    3. Storing user settings using arrays
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Removing plugin data on deletion
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Creating an administration page menu item in the settings menu
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Settings hook priority to determine menu order
      5. See also
    6. Creating a multi-level administration menu
      1. How to do it...
      2. How it works...
      3. See also
    7. Adding menu items leading to external pages
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Hiding items which users should not access from the default menu
      1. How to do it...
      2. How it works...
    9. Rendering the admin page contents using HTML
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. wp_nonce_field
      5. See also
    10. Processing and storing plugin configuration data
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. Displaying a confirmation message when options are saved
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    12. Adding custom help pages
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    13. Rendering the admin page contents using the Settings API
      1. How to do it...
      2. How it works...
      3. There's more...
        1. Rendering a drop-down list settings field
        2. Rendering a text area settings field
      4. See also
    14. Accessing user settings from action and filter hooks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    15. Formatting admin pages using meta boxes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    16. Splitting admin code from the main plugin file to optimize site performance
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    17. Storing style sheet data in user settings
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    18. Managing multiple sets of user settings from a single admin page
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    19. Creating network-level admin pages
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  5. The Power of Custom Post Types
    1. Introduction
    2. Creating a custom post type
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Changing the custom post type permalinks slug
    3. Adding a new section to the custom post type editor
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Displaying single custom post type items using a custom layout
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Displaying custom post type data in shortcodes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. do_shortcode function
    6. Adding custom categories for custom post types
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Adding custom fields to categories
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Hiding the category editor from the custom post type editor
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Displaying additional columns in the custom post list page
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Adding filters for custom categories to the custom post list page
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. Adding Quick Edit fields for custom categories
      1. Getting ready
      2. How to do it...
      3. How it works...
    12. Updating page title to include custom post data using plugin filters
      1. Getting ready
      2. How to do it...
      3. How it works...
  6. Customizing Post and Page Editors
    1. Introduction
    2. Capturing and displaying information using custom meta boxes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Adding a new meta box to all post types (including custom ones)
    3. Displaying custom post data using filter functions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Hiding the Custom Field section in the post editor
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Extending the post editor to allow users to upload files directly
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  7. Accepting User Content Submissions
    1. Introduction
    2. Creating a client-side content submission form
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Saving user-submitted content in custom post types
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Moderating user-submitted content
      5. See also
    4. Sending email notifications upon new submissions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Implementing a CAPTCHA on user forms using an online service
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Using a local library to implement a CAPTCHA on user forms
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  8. Customizing User Data
    1. Introduction
    2. Adding custom fields to the user editor
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Processing and storing user custom data
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Displaying new user data in user list page
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Using custom user data in containing shortcode
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  9. Creating Custom MySQL Database Tables
    1. Introduction
    2. Creating new database tables
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Using phpMyAdmin to simplify code creation
        2. Create tables in network installation
    3. Deleting custom tables on plugin removal
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Updating custom table structure on plugin upgrade
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Displaying custom table data on an admin page
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Inserting and updating records in custom tables
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Deleting records from custom tables
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Displaying custom database table data in shortcodes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Implementing a search function to retrieve custom table data
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Importing data from a user file into custom tables
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  10. Leveraging JavaScript, jQuery, and AJAX Scripts
    1. Introduction
    2. Safely loading jQuery onto WordPress web pages
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. jQuery noconflict mode
    3. Displaying a pop-up dialog using the built-in ThickBox plugin
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Removing the dialog close button
        2. Displaying pop-up dialogs on select pages
    4. Controlling pop-up dialog display using shortcodes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Displaying a calendar day selector using the Datepicker plugin
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Adding tooltips to admin page form fields using the TipTip plugin
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Using AJAX to dynamically update partial page contents
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  11. Adding New Widgets to the WordPress Library
    1. Introduction
    2. Creating a new widget in WordPress
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Plugins extending other plugins
      5. See also
    3. Displaying configuration options
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Validating configuration options
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Implementing the widget display function
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Adding a custom dashboard widget
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Adding a custom widget to the network dashboard
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  12. Enabling Plugin Internationalization
    1. Introduction
    2. Changing the WordPress language configuration
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Adapting default user settings for translation
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Making admin page code ready for translation
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Modifying shortcode output for translation
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Translating text strings using Poedit
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Translation template file
      5. See also
    7. Loading a language file in the plugin initialization
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Updating a translation file
        2. Advanced translation functions
        3. Localizing JavaScript files
      5. See also
  13. Distributing Your Plugin on wordpress.org
    1. Introduction
    2. Creating a README file for your plugin
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Releasing specific plugin versions using tags
    3. Applying for your plugin to be hosted on WordPress.org
      1. How to do it...
      2. How it works...
      3. See also
    4. Uploading your plugin using Subversion
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Checking out plugins to your development installation
      5. See also
    5. Providing plugin banner and thumbnail images
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also

Product information

  • Title: WordPress Plugin Development Cookbook - Second Edition
  • Author(s): Yannick Lefebvre
  • Release date: July 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781788291187