WordPress Plugin Development Cookbook
eBook - ePub

WordPress Plugin Development Cookbook

  1. 420 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

WordPress Plugin Development Cookbook

About this book

Create plugins for WordPress through detailed recipes that cover the creation of shortcodes, custom post types, and custom blocks, integrate data from external sources, and do much more with this recipe-based guideKey Featuresโ€ข Create plugins that change and extend WordPress to perform virtually any taskโ€ข Extend the WordPress Block Editor (Gutenberg) and other components with the help of detailed examples and explanationsโ€ข Customize WordPress to meet your project's needs and create plugins that benefit the entire communityBook DescriptionWordPress is one of the most widely used, powerful, and open content management systems (CMSs). Whether you're a site owner trying to find the right extension, a developer who wants to contribute to the community, or a website developer working to fulfill a client's needs, learning how to extend WordPress' capabilities will help you to unleash its full potential. This book will help you become familiar with API functions to create secure plugins with easy-to-use administration interfaces.This third edition contains new recipes and up-to-date code samples, including new chapters on creating custom blocks for the block editor and integrating data from external sources. From one chapter to the next, you'll learn 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, followed by recipes covering how to design administration panels, enhance the post editor with custom fields, store custom data, and even create custom blocks. You'll safely incorporate dynamic elements into web pages using scripting languages, learn how to integrate data from external sources, 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.What you will learnโ€ข Discover action and filter hooks, which form the basis of plugin creationโ€ข Explore the creation of administration pages and add new content management sections through custom post types and custom fieldsโ€ข Add new components to the block editor libraryโ€ข Fetch, cache, and regularly update data from external sourcesโ€ข Bring in external data sources to enhance your contentโ€ข Make your pages dynamic by using JavaScript, jQuery, and AJAX and adding new widgets to the platformโ€ข Add support for plugin translation and distributing your work to the WordPress communityWho this book is forThis book is for WordPress users, developers, and site integrators interested in creating new plugins to address their personal needs, fulfill client requirements, and bring new capabilities to the WordPress community. Basic knowledge of PHP and WordPress is expected.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, weโ€™ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere โ€” even offline. Perfect for commutes or when youโ€™re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access WordPress Plugin Development Cookbook by Yannick Lefebvre in PDF and/or ePUB format, as well as other popular books in Computer Science & Digital Marketing. We have over one million books available in our catalogue for you to explore.

Information

Chapter 1: Preparing a Local Development Environment

Before you start writing your first WordPress plugin, it is important to have a good set of tools in place that will allow you to work locally on your computer and be more efficient in your work. While it is possible to perform some development tasks with the built-in tools that are provided with the operating system, creating a solid local development environment will help you develop plugins quickly and have full control over your server settings to be able to test different configurations.
This chapter proposes a set of free tools that can easily be installed on your computer, regardless of your preferred operating system, to facilitate the development of your future WordPress plugins. These tools include a local web server to speed up page access and avoid sending files constantly to a remote server, a version control system to keep incremental backups of your work, and a code editor to enhance your editing capabilities.
We will cover the following recipes in this chapter:
  • Installing a local web server on your computer
  • Creating a local version control repository
  • Installing a dedicated code editor/text editor

Technical requirements

You can find complete code examples on GitHub under the following repository: https://github.com/PacktPublishing/WordPress-Plugin-Development-Cookbook-Third-Edition.

Installing a local web server on your computer

The first step to preparing a local development environment is to install a local web server on your computer. This will transform your computer into a system capable of running the core WordPress code and performing all tasks related to rendering a WordPress website locally.
Having a local web server has many benefits:
  • It provides a quick response to the frequent page refreshes that are made as plugin code is written, tested, and refined, since all information is processed locally.
  • It removes the need to constantly upload new plugin file versions to a remote web server to validate code changes.
  • It allows development to take place when no internet connection is available (for example, when traveling on an airplane). This is only possible if your plugin code does not interact with external servers.
  • It offers a worry-free programming environment where you cannot bring down a live website with a programming error or an infinite loop.
There are many free packages available online that contain all of the web server components necessary to run a WordPress installation.

How to do itโ€ฆ

Follow these steps to easily install the aptly named Local application from Flywheel:
  1. Visit the Local website (https://www.localwp.com/) and download the appropriate package for your computer.
    Note
    Local is free and available for Windows, macOS, and Linux platforms. The screenshots and instructions in this recipe are based on Local version 6.1.5 running on Windows 11. The installation steps and exact dialog content may vary based on your choice of platform.
  2. Launch the Local installer.
  3. On Windows, select whether the application should be available only to the user performing the application or to all users (requires administrator privileges), and then leave all the other options at their default values.
  4. Also on Windows, make sure that the Run Local checkbox is checked at the end of the installation process and click on Finish to exit the installer and start Local.
Figure 1.1 โ€“ Last page of the Local installation program on Windows
Figure 1.1 โ€“ Last page of the Local installation program on Windows
  1. On macOS, open the installation package and drag the Local program to the Applications folder. Once this is done, search for Local and run it.
  2. When Local first starts, you will need to accept the application's Terms of Service and will also be given the option to activate Error Reporting to automatically report problems back to the Local team. You can activate the error reporting feature at your discretion.
  3. On the next screen, you will be prompted to create a free Local account. This is optional and can be bypassed by closing the account creation screen, bringing you to the main Local interface.
Figure 1.2 โ€“ Main Local interface
Figure 1.2 โ€“ Main Local interface
  1. Click on + CREATE A NEW SITE to start the site creation process.
  2. Name your new site Plugin Development Site and click on CONTINUE.
  3. Select the Preferred option to create a site running the latest versions of PHP and MySQL, along with the nginx web server, and then click on CONTINUE.
  4. Specify a username, password, and email to create an account on your development WordPress site and then click on ADD SITE. The Local application will download and install the required components and start all of the required applications to have a working local WordPress installation. On Windows, you may need to allow multiple applications through the computer's firewall.
Figure 1.3 โ€“ Newly created local development site
Figure 1.3 โ€“ Newly created local development site
  1. Visit your local development site by clicking on the OPEN SITE button in the top-right corner of the Local interface. The default address of your site is http://plugin-development-site.local/.
  2. Back in the Local interface, click on the ADMIN button to visit your site's login page and connect using the credentials you provided during the site creation process.
  3. Make sure that the active theme is Twenty Twenty-Two. If not, install it and activate it.
  4. Still in the Local interface, click on the arrow next to the site path under the site name to access the site's local files on your computer. The WordPress software files will be found under the app\public directory within the site directory.

How i...

Table of contents

  1. WordPress Plugin Development Cookbook
  2. Third Edition
  3. Contributors
  4. About the author
  5. About the reviewers
  6. Preface
  7. Chapter 1: Preparing a Local Development Environment
  8. Chapter 2: Plugin Framework Basics
  9. Chapter 3: User Settings and Administration Pages
  10. Chapter 4: The Power of Custom Post Types
  11. Chapter 5: Customizing Post and Page Editors
  12. Chapter 6: Extending the Block Editor
  13. Chapter 7: Accepting User Content Submissions
  14. Chapter 8: Customizing User Data
  15. Chapter 9: Leveraging JavaScript, jQuery, and AJAX Scripts
  16. Chapter 10: Adding New Widgets to the WordPress Library
  17. Chapter 11: Fetching, Caching, and Regularly Updating External Site Data
  18. Chapter 12: Enabling Plugin Internationalization
  19. Chapter 13: Distributing Your Plugin on WordPress.org
  20. Other Books You May Enjoy