Book Image

WordPress Plugin Development Cookbook - Third Edition

By : Yannick Lefebvre
Book Image

WordPress Plugin Development Cookbook - Third Edition

By: Yannick Lefebvre

Overview of this book

WordPress 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.
Table of Contents (15 chapters)

Creating a server-side rendering block that leverages an existing shortcode

As mentioned at the start of this chapter, block creation uses a different programming pipeline by leaning more heavily on JavaScript. This means that providing a simple block to complement an existing simple shortcode, as we've done in this chapter's first recipe, results in code duplication in our plugin. It's also not always easy for developers who are more accustomed to PHP and HTML to create complex blocks using pure JavaScript code. To alleviate these issues, WordPress offers a method by which plugins can use a server-side PHP function to render a block's content. This allows developers to offer plugins with shortcodes and blocks that share the same code base.

Getting ready

You should have already followed the Creating a new WordPress REST endpoint recipe to have a starting point for this recipe. The resulting plugin should still be active on your development site. Alternatively...