Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
WordPress Plugin Development Cookbook
WordPress Plugin Development Cookbook

WordPress Plugin Development Cookbook: Create powerful plugins to extend the world's most popular CMS , Second Edition

eBook
$24.99 $35.99
Paperback
$43.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

WordPress Plugin Development Cookbook

Plugin Framework Basics

In this chapter, we will cover the following topics:

  • Creating a plugin file and header
  • Adding output content to page headers using plugin actions
  • Using WordPress path utility functions to load external files and images
  • Modifying the site generator meta tag using plugin filters
  • Adding text after each item's content using plugin filters
  • Inserting link statistics tracking code in page body using plugin filters
  • Troubleshooting coding errors and printing variable content
  • Creating a new simple shortcode
  • Creating a new shortcode with parameters
  • Creating a new enclosing shortcode
  • Loading a style sheet to format plugin output
  • Writing plugins using object-oriented PHP

Introduction

From its very first versions, WordPress has always been designed as a very open platform. This openness has been exemplified not only through its open source licensing and distribution model, but also its open plugin architecture, providing developers with the ability to deliver an even richer experience to its users.

While a basic WordPress installation provides a great amount of functionality that continues to expand from one release to the next, users often have the need to add one more feature to make it the perfect website management system. This is where the plugins come into play. They can fill this gap by augmenting or manipulating virtually any aspect of a WordPress website's display and administrative tasks.

Just like WordPress, plugins are written in the PHP programming language, which is structurally similar to more traditional languages such as C...

Creating a plugin file and header

The first step of creating a WordPress plugin is to create a PHP file inside of the plugins directory and add the necessary information to have it recognized by the system. This first recipe shows you how to create a basic plugin file for WordPress and how to see and activate this new extension from the administration interface.

Getting ready

You should have access to a WordPress development environment, either on your local computer or a remote server, where you will be able to load your new plugin files.

How to do it...

  1. Navigate to...

Adding output content to page headers using plugin actions

A common action performed by plugins is to add extra content to the header of visitor-facing pages generated by WordPress. This recipe shows you how to register an action hook function to be able to add such additional content. To make this example more concrete, we will use the Google Analytics page header JavaScript code that so many people use to get good page view statistics for their site.

How to do it...

  1. Navigate to the WordPress plugin directory of your development installation.
  2. Create a new directory called ch2-page-header-output.
  3. Navigate to this directory and create a new text file called ch2-page-header-output.php.
  4. Open the new file in a code editor and...

Using WordPress path utility functions to load external files and images

On occasion, plugins need to refer to external files (for example, images, JavaScript, or jQuery script files) that are stored in the plugin directory. Since users are free to rename a plugin's folder or even install plugin files straight into the WordPress plugin directory, paths to any external files must be built dynamically based on the actual plugin location. Thankfully, a number of utility functions are present to simplify this task. In this recipe, we will write a simple plugin that will add a favicon meta tag to a website's header, pointing to an image file located in the plugin directory.

How to do it...

  1. Navigate to the WordPress plugin...

Modifying the site generator meta tag using plugin filters

Beyond adding functionality or content to a site, the other major task commonly performed by plugins is to augment, modify, or reduce information before it is displayed on the screen. This is done by using WordPress filter hooks, which allow plugins to register a custom function through the WordPress API to be called, since content is prepared before it is sent to the browser. In this recipe, you will learn how to implement your first filter callback function to modify the contents of the generator meta tag that is output as part of the site header.

How to do it...

  1. Navigate to the WordPress plugin directory of your development installation.
  2. Create a new directory...

Adding text after each item's content using plugin filters

After making a number of changes to the page header, the generator meta tag, and the site favicon, this recipe takes a more active role by adding a link to each post or page, allowing visitors to email a link to the item that they are currently viewing. This functionality is implemented using a filter hook attached to the page and post content, allowing our custom function to append custom output code to all entries that get displayed on the screen.

How to do it...

  1. Navigate to the WordPress plugin directory of your development installation.
  2. Create a new directory called ch2-email-page-link.
  3. Navigate to this directory and create a new text file called ch2-email...

Inserting link statistics tracking code in page body using plugin filters

After creating two filter functions that append text to the existing content, this recipe shows you how to modify the page content before it is displayed on the screen. More specifically, the following plugin will expand on the Google Analytics header plugin created earlier and add a JavaScript function to all the links that are included in posts and pages to track when they are clicked by visitors.

Getting ready

You should have already followed the Adding output content to page headers using plugin actions recipe to have a starting point for this recipe and the resulting plugin should be active in your development site. Alternatively, you can download...

Troubleshooting coding errors and printing variable content

As you transcribe code segments from the pages of this book or start writing your own plugins, there is a strong chance that you will have to troubleshoot problems with your code or have trouble working with data that your plugin is meant to manipulate. This recipe shows the basic techniques to identify and quickly resolve these errors while creating a plugin that will hide an item from the navigation menu for users who are not logged in to your site.

How to do it...

  1. Navigate to the WordPress plugin directory of your development installation.
  2. Create a new directory called ch2-nav-menu-filter.
  3. Navigate to this directory and create a new text file called ch2-nav-menu...

Creating a new simple shortcode

Shortcodes are a very popular tool in WordPress that allow users to easily add content generated by plugins or themes to any page or post without needing to be familiar with PHP code and editing theme template files. As they are very simple to create, shortcodes can also be used to easily automate the output of content that repeatedly needs to be included in your site's content.

This recipe explains how to create a new custom shortcode that will be used to quickly add a link to a Twitter page in any post or page, automating a repetitive task.

How to do it...

  1. Navigate to the WordPress plugin directory of your development installation.
  2. Create a new directory called ch2-twitter-shortcode...

Creating a new shortcode with parameters

While simple shortcodes already provide a lot of potential to output complex content to a page by entering a few characters in the post editor, shortcodes become even more useful when they are coupled with parameters that will be passed to their associated processing function. Using this technique, it becomes very easy to create a shortcode that accelerates the insertion of external content in WordPress posts or pages by only needing to specify the shortcode and the unique identifier of the source element to be displayed.

We will illustrate this concept in this recipe by creating a shortcode that will be used to quickly add Twitter feeds to posts or pages.

How to do it...

  1. Navigate...

Creating a new enclosing shortcode

A different type of shortcode is available in WordPress that encloses content in posts and pages. Using a syntax similar to HTML tags, enclosing shortcodes can be used to identify parts of an item's content that need to be treated in a special way. For example, it is possible to use this type of shortcode to style a part of the post.

As an example of how to create enclosing shortcodes, this recipe shows you how to create a set of tags that will identify part of a post or page that should only be shown to visitors that are logged in to a site. In this way, the shortcode acts similarly to a filter hook, with the added bonus that you do not need to parse for instances of these tags, as would normally be done in a filter.

How to do it...

...

Loading a style sheet to format plugin output

When a plugin adds custom content or inserts styling tags to a post or page's existing content, as was done in the previous recipe showing how to create an enclosing shortcode, it usually needs to load a custom style sheet to style these new elements. This recipe shows how to add a style sheet in the WordPress style queue to format the private output created in the previous recipe. This queue is processed when the page header is rendered, listing all the style sheets that need to be loaded to display the site correctly.

Getting ready

You should have already followed the Creating a new enclosing shortcode recipe to have a starting point for this recipe and the resulting plugin...

Writing plugins using object-oriented PHP

So far, all plugin examples that have been covered in this chapter have been written using the procedural PHP programming style, with all the functions declared directly in the main body of the plugin and the hook registration functions having direct access to these functions.

WordPress can also be written using an object-oriented PHP approach. This recipe explains how to convert the code from the previous recipe into a class-based version of the same functionality.

Getting ready

You should have already followed the Loading a style sheet to format plugin output recipe to have a starting point for this recipe. Alternatively, you can download the resulting code (Chapter 2/ch2-private...

Left arrow icon Right arrow icon

Key benefits

  • 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

Description

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.

Who is this book 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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 26, 2017
Length: 386 pages
Edition : 2nd
Language : English
ISBN-13 : 9781788291187
Vendor :
WordPress Foundation
Languages :
Concepts :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jul 26, 2017
Length: 386 pages
Edition : 2nd
Language : English
ISBN-13 : 9781788291187
Vendor :
WordPress Foundation
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 141.97
WordPress Plugin Development Cookbook
$43.99
WordPress Complete, Sixth Edition
$48.99
Wordpress Web Application  Development
$48.99
Total $ 141.97 Stars icon
Banner background image

Table of Contents

12 Chapters
Preparing a Local Development Environment Chevron down icon Chevron up icon
Plugin Framework Basics Chevron down icon Chevron up icon
User Settings and Administration Pages Chevron down icon Chevron up icon
The Power of Custom Post Types Chevron down icon Chevron up icon
Customizing Post and Page Editors Chevron down icon Chevron up icon
Accepting User Content Submissions Chevron down icon Chevron up icon
Customizing User Data Chevron down icon Chevron up icon
Creating Custom MySQL Database Tables Chevron down icon Chevron up icon
Leveraging JavaScript, jQuery, and AJAX Scripts Chevron down icon Chevron up icon
Adding New Widgets to the WordPress Library Chevron down icon Chevron up icon
Enabling Plugin Internationalization Chevron down icon Chevron up icon
Distributing Your Plugin on wordpress.org Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4
(12 Ratings)
5 star 58.3%
4 star 33.3%
3 star 0%
2 star 8.3%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Toddd L. Tomlinson Sep 25, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The switch from Drupal to Wordpress has been fairly painless thanks to this book! I was off and running and building plugins on the first day of my Wordpress adventures. A great book with just the right information in the right format.
Amazon Verified review Amazon
patrick kellogg Aug 10, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is super. It is packed with info and from it I quickly wrote a certificate tracker for LearnDash LMS based on the bug tracker. The author explains everything in detail and the code is organized properly so its a snap to activate the next plugin in the series as you ramp up. Highly recommended.
Amazon Verified review Amazon
Maxime Jobin May 04, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I usually never use books/ebooks when reading about software development. I have to admit that the way the book is structured and how easy the examples are built makes it a "reference book" I really enjoyed. I loved how many examples there is and the fact they are linked to practical use cases. Any developer that starts developing plugins for WordPress should buy it.
Amazon Verified review Amazon
Bruno P. Aug 10, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
good seller, good product
Amazon Verified review Amazon
Mark Mar 14, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is for developers who know at least some PHP. After my company abruptly decided to switch to Wordpress for their news site this book gave me a quick ramp up to be able to show I know something about plugins quickly. It's a good intro for developers to WordPress Plugins by showing some real-world examples.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.