Table Of ContentWordPress Plugin
Development
Cookbook
Third Edition
Explore the complete set of tools to craft powerful
plugins that extend the world's most popular CMS
Yannick Lefebvre
BIRMINGHAM—MUMBAI
WordPress Plugin Development Cookbook
Third Edition
Copyright © 2022 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in
any form or by any means, without the prior written permission of the publisher, except in the case of brief
quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information
presented. However, the information contained in this book is sold without warranty, either express or
implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any
damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products
mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the
accuracy of this information.
Group Product Manager: Pavan Ramchandani
Publishing Product Manager: Kaustubh Manglurkar
Senior Editor: Aamir Ahmed
Content Development Editor: Feza Shaikh
Technical Editor: Shubham Sharma
Copy Editor: Safis Editing
Project Coordinator: Manthan Patel
Proofreader: Safis Editing
Indexer: Manju Arasan
Production Designer: Prashant Ghare
Marketing Coordinator: Anamika Singh
First published: July 2012
Second edition: July 2017
Third edition: March 2022
Production reference: 1170222
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-80181-077-7
www.packt.com
To my wife, Andrée, for her everlasting love, her patience throughout the
writing process, and supporting all my quirks.
To my daughters, Évelyne and Gabrielle, for always making me smile and
becoming wonderful women.
To the users of my WordPress plugins, who are always pushing me to learn
new things and add great new features to my creations after all these years.
– Yannick Lefebvre
Contributors
About the author
Yannick Lefebvre is a plugin developer who has created multiple plugins on the official
WordPress repository. His first creation, Link Library, is used on thousands of sites
worldwide. With a background in computer science, he wrote his first plugin in 2004 and
quickly started sharing his creations with the community. He is actively involved in the
Montreal WordPress community, has presented multiple times at WordCamp Montreal,
and offers custom plugin development services. By day, Yannick works for CM Labs
Simulations, a company providing software tools and simulators for vehicles and
heavy equipment.
I would like to thank the WordCamp Montreal organizers for giving me the
chance to speak at multiple editions of the event, for creating great videos of
the presentations, and for giving me the opportunity to get involved in the
community. This project would not have existed without them.
I would also like to thank Richard Archambault for his great feedback
and encouragement while I wrote the first edition of this book, as well as
the entire Packt Publishing team for proposing this great project to me,
supporting me through the entire process, and allowing me to put together
this third edition.
Finally, I would like to thank my parents for always believing in me and
encouraging me in all my projects.
About the reviewers
Ali Demirci is a senior developer who has 15 years of experience in WordPress. He
started working on WordPress in 2007. He is the co-founder of 2035themes. He has
sold more than 6,000 WordPress themes on Themeforest. He became a freelancer as the
themes business wasn't providing a stable income. He is now a full-time freelancer on the
Codeable platform.
Stefan C. Densmore has been a WordPress developer since 2003 and a continuous
member of the WPMUDEV development community since 2011. He has been a speaker
at multiple WordCamps as a subject matter expert in WordPress Multisite development.
With a formal education in human factors engineering and performance psychology,
Stefan is passionate about making complex systems more user-friendly. He has a
fondness for applications and plugins that enhance information sharing and knowledge
management. Stefan has served as a technology broker and consultant for various
information management interests and lives in the United States. He is the founder of
both the United Resource Connection and Cybercasemanager enterprises, and can be
found on Twitter @StefanDensmore.
Fred Atkinson is a telecom/IT professional with interests in web and internet
technologies and website development. As an avid ham radio operator (WB4AEJ, for
47 years), he has been using WordPress for several years to build websites that facilitate
the propagation of operator callsigns. He studied at Johns Hopkins University, earning
a master's degree in Information and Telecommunications Systems Management in 1997.
He also has an associate's degree in Enterprise Network Management from Stratford
University. He is now retired and currently lives in Mesa, Arizona.
Table of Contents
Preface
1
Preparing a Local Development Environment
Technical requirements 2 How it works… 11
Installing a local web server on There's more… 12
your computer 2
Installing a dedicated code
How to do it… 3 editor/text editor 14
How it works… 5
Getting ready 15
There's more… 6
How to do it… 15
Creating a local version control How it works… 16
repository 7 There's more… 17
How to do it… 7
2
Plugin Framework Basics
Technical requirements 20 There's more... 27
Creating a plugin file and header 20 See also 28
Getting ready 21 Using WordPress path utility
How to do it... 21 functions to load external files
How it works... 23 and images 28
See also 23
How to do it... 28
Adding output content to page How it works... 30
headers using plugin actions 24 There's more... 31
See also 32
How to do it... 24
How it works... 26
viii Table of Contents
Modifying the site generator How it works... 50
meta tag using plugin filters 32 See also 51
How to do it... 32
Creating a new shortcode with
How it works... 33
parameters 51
There's more... 34
How to do it... 51
See also 35
How it works... 53
Adding text after each item's See also 54
content using plugin filters 35
Creating a new enclosing
How to do it... 35
shortcode 54
How it works... 38
How to do it... 54
There's more... 38
How it works... 56
See also 39
See also 56
Inserting link tracking code in
Loading a style sheet to format
the page body using plugin filters 39
plugin output 56
Getting ready 39
Getting ready 56
How to do it... 40
How to do it... 56
How it works... 42
How it works... 58
See also 42
See also 58
Troubleshooting coding errors
Writing plugins using object-
and printing variable content 42
oriented PHP 58
How to do it... 43
Getting ready 58
How it works... 46
How to do it... 59
There's more... 47
How it works... 60
See also 49
There's more… 60
Creating a new simple shortcode 49 See also 60
How to do it... 49
3
User Settings and Administration Pages
Technical requirements 62 There's more... 65
Creating default user settings See also 65
on plugin initialization 63
Storing user settings using arrays 66
How to do it... 63
Getting ready 66
How it works... 64
How to do it... 66
Table of Contents ix
How it works... 68 Displaying a confirmation
See also 69 message when options
are saved 87
Removing plugin data on
Getting ready 87
deletion 69
How to do it... 87
Getting ready 69
How it works... 88
How to do it... 69
See also 89
How it works... 71
See also 71 Adding custom help pages 89
Getting ready 89
Creating an administration
How to do it... 89
page menu item in the settings
How it works... 92
menu 71
See also 92
Getting ready 71
How to do it... 72 Rendering the admin page
How it works... 73 contents using the Settings API 92
There's more... 74 How to do it... 93
See also 75 How it works... 98
There's more... 100
Creating a multi-level
See also 102
administration menu 75
How to do it... 75 Accessing user settings from
How it works... 77 action and filter hooks 102
There's more… 78 Getting ready 102
See also 79 How to do it... 102
How it works... 104
Rendering the admin page
See also 104
contents using HTML 79
Getting ready 79 Formatting admin pages using
How to do it... 80 meta boxes 104
How it works... 81 Getting ready 104
There's more... 82 How to do it... 105
See also 83 How it works... 109
See also 110
Processing and storing plugin
configuration data 83 Splitting admin code from the
Getting ready 83 main plugin file to optimize site
How to do it... 83 performance 111
How it works... 86 Getting ready 111
See also 87 How to do it... 111