www.it-ebooks.info WordPress 3 Plugin Development Essentials Create your own powerful, interactive plugins to extend and add features to your WordPress site Brian Bondari Everett Grifths www.it-ebooks.info WordPress 3 Plugin Development Essentials Copyright © 2011 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 authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be 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. First published: March 2011 Production Reference: 1180311 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849513-52-4 www.packtpub.com Cover Image by Rakesh Shejwal (shejwal.rakesh@gmail.com) www.it-ebooks.info Credits Authors Brian Bondari Everett Grifths Reviewers Srikanth AD Sam Rose Paul Thewlis Ezwan Aizat Bin Abdullah Faiz Acquisition Editor David Barnes Development Editor Hyacintha D'Souza Technical Editor Kavita Iyer Copy Editor Neha Shetty Indexers Hemangini Bari Tejal Daruwale Editorial Team Leader Akshara Aware Project Team Leader Ashwin Shetty Project Coordinators Michelle Quadros Zainab Bagasrawala Proofreader Aaron Nash Graphics Nilesh Mohite Production Coordinator Kruthika Bangera Cover Work Kruthika Bangera www.it-ebooks.info About the Authors Brian Bondari is a musician, composer, and teacher with equal love for both music and technology. His hobbies include reading, hiking, composing music, and playing with his pet rabbit. He also spends an exorbitant amount of time lying on the oor grading papers. Brian earned his doctorate from the University of Kansas in 2009 and is currently an Assistant Professor of Music Theory and Composition at Trinity University in San Antonio, TX. When he is not writing music or grading papers, he helps run the multi-author technology blog www.TipsFor.us. He is also the author of WordPress 2.9 E-Commerce, also published by Packt. This book would not have been possible without Everett's mad coding skills and utterly unyielding work ethic. Thanks for the partnership and friendship of many years. I'd also like to thank the team at Packt for helping to organize this project and get it off the ground. Finally, utmost thanks to my wife Katrina for her unending love, support, and patience. Everett Grifths is the owner of Fireproof Socks, a development company that specializes in web applications and content management systems including MODx, WordPress, and Expression Engine. Although, he has contributed many educational articles and screencasts to the blog he runs with Brian Bondari, TipsFor.us, this is his rst published book. He survives as a coder of fortune in the Los Angeles underground. If you have a problem, if no one else can help, and if you can nd him, maybe you can hire Everett's team. I'd like to thank Brian for being a steadfast and patient editor of practically every crazy word I've penned or spoken, Nui for the beautiful memories, and my parents for their constant support. I'd also like to thank all the people who didn't believe in me because all their attempts to keep me down only made me stronger. www.it-ebooks.info About the Reviewers Srikanth AD is a Web Developer and SEO Consultant. He is passionate about developing and optimizing websites for better search engine visibility and user experience. Sharing interesting tools and services pertaining to web development and SEO across technology blogs is one of his active hobbies. He has written articles for some of the popular blogs such as MakeUseOf, TheNextWeb, QuickOnlineTips, Lost in Technology, 1stWebDesigner, and others. Portfolio: http://www.adsrikanth.com Blog: http://www.readaboutseo.com Sam Rose is a 20 year old Computer Science student living in Wales, UK. He has recently entered his second year of his Computer Science degree at the University of Glamorgan in South Wales. Sam writes code primarily in Java, PHP and has intermediate knowledge in an array of other languages. In his spare time, Sam is usually playing pool, watching comedy produced by Chuck Lorre, writing code on his current favorite open source project, ThinkUp, managed by the lovely Gina Trapani, or writing on his blog, http://lbak.co.uk. This is my rst time as a technical reviewer for a book and I would really like to thank Erika from the Packt team for nding and giving me the opportunity to review this book and Michelle, also from the Packt team, for being a wonderfully happy and helpful point of contact throughout the review process. www.it-ebooks.info Paul Thewlis is seasoned web marketing professional. He is currently in charge of the Search Engine Marketing department at a leading full-service digital agency in the UK. Previously, he was the E-Communications Manager for a multinational transport company. He began his web career as a Technical Editor, working on web design books for a well-known publisher. He has extensive experience of many content management systems and blogging platforms. His rst book, WordPress For Business Bloggers, was published by Packt. He is an expert in the use of social media within corporate communications, and blogs about that subject, as well as WordPress, SEO, and the Web in general, at http://blog.paulthewlis.com. www.it-ebooks.info www.PacktPub.com Support les, eBooks, discount offers and more You might want to visit www.PacktPub.com for support les and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub les available? You can upgrade to the eBook version at www.PacktPub. com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. Why Subscribe? • Fully searchable across every book published by Packt • Copy and paste, print and bookmark content • On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. www.it-ebooks.info www.it-ebooks.info Table of Contents Preface 1 Chapter 1: Preparing for WordPress Development 7 WordPress background 7 Extending WordPress 8 Understanding WordPress architecture 8 Templating 9 Introducing plugins 9 Summarizing architecture 10 Tools for web development 11 WordPress 11 Mac 12 Windows 12 Text editor 12 Using an IDE 13 FTP client 14 MySQL client 14 Coding best practices 15 Basic organization 15 Isolate tasks into functions 16 Use classes 16 Use descriptive variable names 16 Use descriptive function names 17 Separate logic and display layers 17 Go modular, to a point 18 Avoid short tags 18 Planning ahead / starting development 18 Interfaces 19 Localization 19 www.it-ebooks.info [...]... www.it-ebooks.info 2 23 224 226 226 227 227 229 229 230 230 230 231 232 232 232 233 235 237 237 238 240 240 241 242 242 2 43 244 245 246 248 250 251 252 2 53 2 53 2 53 254 254 255 257 258 Table of Contents Appendix A: Recommended Resources 259 Appendix B: WordPress API Reference 2 63 PHP reference Function reference The WordPress forums WebDev Studios Viper007Bond Kovshenin SLTaylor XPlus3 WP Engineer Other plugins PHP... functionless plugin What just happened Omitting the closing "?>" PHP tag A better example: Adding functions Referencing hooks via add_action() and add_filter() Actions versus Filters Exercise—actions and filters Exercise—filters Reading more Summary The overall plan Proof of concept Avoiding conflicting function names [ ii ] www.it-ebooks.info 29 29 30 30 30 31 32 32 33 34 34 36 38 38 39 40 40 41 43 44 46... 129 131 134 135 135 137 138 138 139 139 140 1 43 145 149 155 157 158 158 159 161 1 63 164 165 Table of Contents Chapter 7: Custom Post Types 167 Chapter 8: Versioning Your Code with Subversion (SVN) 191 Chapter 9: Preparing Your Plugin for Distribution 2 13 Background: What's in a name? Understanding register_post_type() Customizing our post type Using shortcodes Testing our shortcode Customizing our plugin. .. for WordPress Development, provides an overview of the development process and discusses a number of tools and practices recommended for a successful WordPress development environment Chapter 2, Anatomy of a Plugin, breaks an existing plugin down into its component parts to see what makes it work, and what makes it break www.it-ebooks.info Preface Chapter 3, Social Bookmarking, walks through the development. .. Download from Wow! eBook Preparing for WordPress Development What is a plugin? The term "Plugin" is a bit ambiguous and WordPress' definition differs from other content management systems In general, a WordPress plugin is any bit of code that extends the core functionality of WordPress Unlike Expression Engine, Drupal, or MODx, WordPress does not use different terms such as "module",... tools you need to develop plugins for WordPress are essentially the same tools you need for developing almost any web application, specifically: • WordPress • Text editor • FTP client • MySQL client (optional) WordPress If you are going to develop plugins for WordPress, you need WordPress itself and an environment that can run it Download the latest version of WordPress from http:/ /wordpress. org/download... you as a plugin author Throughout this book, our goal is to teach you all aspects of modern WordPress development We will build a variety of WordPress plugins and follow their creation from the idea to the finishing touches You will discover how to deconstruct an existing plugin, use the WordPress API in typical scenarios, hook into the database, version your code with SVN, and deploy your new plugin. .. www.it-ebooks.info Preparing for WordPress Development Since you have picked up this book, you are likely to fall into one of two overall categories: developers who are new to WordPress, or WordPress users keen to start or improve their WordPress development skills No matter which camp you lie in, this book will help you down that path This book will show you how to customize WordPress using plugins by providing... install it WordPress 3 runs on a web server (most commonly Apache) that can run at least PHP 4 .3 and MySQL 4.1.2 WordPress 3. 2 requires PHP 5.2 and MySQL 5.0.15 Since both PHP and MySQL are widespread web technologies and WordPress is such a popular blogging tool, most hosting providers can support running WordPress on their servers If in doubt, consult your web host's FAQ Another option is to run WordPress. .. www.it-ebooks.info Preparing for WordPress Development Extending WordPress Like many systems, WordPress may not do everything you want right out of the box Instead, it focuses on a set of core features and allows for customizations in the form of plugins, so if the built-in functionality doesn't meet your needs, your options are to: • Find an existing third-party plugin • Write your own plugin • Look for another . Includes 32 Exercise – parse errors 32 Bonus for the curious 33 User-dened functions 34 Exercise—an evil functionless plugin 34 What just happened 36 Omitting the closing "?>" PHP tag 38 A. www.it-ebooks.info WordPress 3 Plugin Development Essentials Create your own powerful, interactive plugins to extend and add features to your WordPress site Brian Bondari Everett Grifths www.it-ebooks.info WordPress. page 131 Randomizing content from the database 134 Review of PHP functions used 135 Summary 135 Chapter 6: Standardized Custom Content 137 What WordPress does for you: custom elds 138 What WordPress