As we write this, WordPress powers 32% of all sites on the Internet, and that number is growing. Many developers want to do more with their WordPress sites but feel that they need to jump ship to a more traditional application framework like Ruby on Rails, Symfony, Yii, or Laravel to build “real” web apps. This sentiment is wrong, and we’re here to fix it.
Despite starting out as blogging software and currently existing primarily as a content management system, WordPress has grown into a flexible and capable platform for building web apps. This book will show you how to use WordPress as an application framework to build any web app, large or small.
This book will be most useful for WordPress developers looking to work on heavier applications and PHP developers with some WordPress experience looking for a PHP-based application framework.
Commercial plugin and theme developers, or anyone working on large distributed WordPress projects, will also find the concepts and techniques of this book useful.
If you are a PHP or language-agnostic developer using another framework and jealous of the large library of WordPress plugins and themes, you may be surprised to learn how well WordPress can work as a general application framework. Reading and applying the lessons in this book could change your work life for the better.
We assume that readers have an intermediate understanding of general PHP programming. You should also have a basic understanding of HTML and CSS, and familiarity with MySQL and SQL queries. Basic understanding of JavaScript and jQuery programming will help with the JavaScript and AJAX chapter and related examples.
This book is not for people who want to learn how to use WordPress as an end user. There will be brief introductions to standard WordPress functionality, but we assume that readers have already experienced WordPress from a user’s perspective.
This book is not meant for non-programmers. While it is possible to build very functional web applications by simply combining and configuring the many plugins available for WordPress, this book is written for developers building their own plugins and themes to power new web apps.
This book will not teach you how to program but will teach you how to program “the WordPress way.”
Our hope with this book is that you will learn the programming and organizational techniques and best practices for developing complex applications using WordPress.
Chapter 1 defines what we mean by “web app” and also covers why or why not to use WordPress for building web apps and how to compare WordPress to other application frameworks. We also introduce SchoolPress, the WordPress app that we use as an example throughout the book.
Chapter 2 covers the basics of WordPress. We go over the various directories of the core WordPress install and what goes where. We also explain each database table created by WordPress, what data each holds, and which WordPress functions map to those tables. Even experienced WordPress developers can learn something from this chapter and are encouraged to read it.
Chapter 3 is all about plugins. What are they? How do you make your own plugins? How should you structure your app’s main plugin? When should you leverage third-party plugins or roll your own?
Chapter 4 is all about themes. How do themes works? How do themes map to views in a typical model-view-controller (MVC) framework? What code should go into your theme, and what code should go into plugins? We also cover using theme frameworks and UI frameworks and the basics of responsive design.
Chapter 5 covers custom post types and taxonomies. We go over the default post types built into WordPress, why you might need to build your own, and then how to go about doing that. We also cover post meta and taxonomies, what each is appropriate for, and how to build custom taxonomies and map them to your post types. Finally, we show how to build wrapper classes for your post types to organize your code utilizing object-oriented programming (OOP).
Chapter 6 covers users, roles, and capabilities. We show how to add, update, and delete users programmatically, and how to work with user meta, roles, and capabilities. We also show how to extend the WP_User class for your user archetypes like “customers” and “teachers” to better organize your code using OOP techniques.
Chapter 7 covers a few of the more useful WordPress APIs and helper functions that didn’t fit into the rest of the book but are still important for developers building web apps with WordPress.
Chapter 8 is all about securing your WordPress apps, plugins, and themes.
Chapter 9 covers using JavaScript and AJAX in your WordPress application. We go over the correct way to enqueue JavaScript into WordPress and how to build asynchronous behaviors in your app.
Chapter 10 covers the REST API for WordPress and how to use it to integrate WordPress with outside apps.
Chapter 11 covers WordPress multisite networks, including how to set them up and things to keep in mind when developing for multisite.
Chapter 12 covers localizing your WordPress plugins and themes, including how to prep your code for translation and how to create and use translation files.
Chapter 13 covers how to optimize and scale WordPress for high-volume web apps. We go over how to test the performance of your WordPress app and the most popular techniques for speeding up and scaling sites running WordPress.
[Link to Come] covers search functionality in WordPress apps and how to extend it, including tools like Elasticsearch.
Chapter 14 covers ecommerce. We go over the various types of ecommerce plugins available and how to choose between them. We then go into detail on how to use WordPress to handle payments and account management for software as a service (SaaS) web apps.
[Link to Come] covers social networks, including how to integrate with Twitter and Facebook.
[Link to Come] covers how to use WordPress to power native apps on mobile devices by creating app wrappers for iOS and Android.
[Link to Come] covers WP-CLI, the command-line interface for WordPress, showing how to do complex updates and useful recipes using the command-line.
[Link to Come] covers platform migrations.
[Link to Come] covers some third-party PHP libraries, services, and APIs that are often used in web apps and how to integrate them with WordPress.
[Link to Come] discusses the future of WordPress, what kinds of apps we expect to see running on WordPress, what kinds of updates we see coming for WordPress, and which tools and frameworks to keep an eye on for the future.
All examples in this book can be found at https://github.com/bwawwp. Please note that these code examples were written to most clearly convey the concepts we cover in the book. To improve readability, we often ignored best practices for coding standards[https://make.wordpress.org/core/handbook/best-practices/coding-standards/], security, and localization (which we cover in Chapter 8 and Chapter 12 of this book) or ignored certain edge cases. You will want to keep this in mind before using any examples in production code.
The sample app SchoolPress can be found at http://schoolpress.me, with any open sourced code for that site available at https://github.com/bwawwp/schoolpress.
The following typographical conventions are used in this book:
Indicates new terms, URLs, email addresses, filenames, and file extensions.
Constant widthUsed for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, datatypes, environment variables, statements, and keywords.
Constant width boldShows commands or other text that should be typed literally by the user.
Constant width italicShows text that should be replaced with user-supplied values or by values determined by context.
This element signifies a tip, suggestion, or general note.
This element indicates a warning or caution.
This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Building Web Apps with WordPress by Brian Messenlehner and Jason Coleman (O’Reilly). Copyright 2014 Brian Messenlehner and Jason Coleman, 978-1-449-36407-6.”
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com.
Safari (formerly Safari Books Online) is a membership-based training and reference platform for enterprise, government, educators, and individuals.
Members have access to thousands of books, training videos, Learning Paths, interactive tutorials, and curated playlists from over 250 publishers, including O’Reilly Media, Harvard Business Review, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Adobe, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, and Course Technology, among others.
For more information, please visit http://oreilly.com/safari.
Please address comments and questions concerning this book to the publisher:
We have a web page for this book, where we list errata and any additional information. You can access this page at http://oreil.ly/building-apps-wp.
To comment or ask technical questions about this book, send email to contact@bwawwp.com.
For more information about our book and online examples see our website at http://bwawwp.com.
Find us on Facebook: http://facebook.com/bwawwp
Follow us on Twitter: http://twitter.com/bwawwp
Follow us on Instagram: http://instagram.com/bwawwp
Thanks to Jason Coleman and Matt Mullenweg; I could not have written this book without them. I would like to thank Meghan Blanchette, Allyson MacDonald, and especially Alicia Young for staying on top of things at O’Reilly Media. A very special thanks to our technical reviewers for making sure everything in the book is legit. Thanks to Scott Bolinger from AppPresser and Jaffe Worley from AlphaWeb for putting up with me. Shout out to family and friends that have always been there for me and never stopped believing in me. Most of all, I’m thankful for my children Dalya, Brian Jr., Nina, and Cam Messenlehner, they give me a purpose and without them I probably wouldn’t even know what WordPress was.
— Brian Messenlehner
Thanks to my coauthor Brian for asking me to write this book with him. Thanks to our original editors Meghan and Allyson for keeping us on track and helping us to stay true to our original vision. Thanks to Alicia Young for editing the second revision of this book and sitting through our explanations of “WP Drama”. Thanks to our great technical editors Sam Hotchkiss, John James Jacoby, Peter MacIntyre and Pippin Williamson for reviewing our code and writing and providing valuable feedback. Thanks to Frederick Townes for his feedback and contributions to our chapter on optimization and scaling. Thanks to Chris Lema for his feedback on the book and advice in general. Thanks to everyone in the WordPress community who answered all of my random tweets and may or may not have known they were helping me to write this book. Thanks to my wife, Kim, for supporting me as always during yet another adventure in our life. Thanks to my daughter, Marin, for missing me when I was away to write, and my son, Isaac, for constantly asking me if I had “finished the book yet.” Last but not least, thanks to my family who have always supported my writing: Mom, Dad, Jeremy, and Nana Men are all excited to be the first non-programmers to read Building Web Apps with WordPress.
— Jason Coleman