Create your own powerful, interactive plugins to extend and add features to your WordPress site * Everything you need to know to develop your own plugins for WordPress * Walk through the development of five plugins from ground up * Prepare and release your plugins to the WordPress community * Avoid common mistakes made by most plugin developers by learning more advanced development patterns **In Detail** WordPress is one of the most popular platforms for building blogs and general websites. By learning how to develop and integrate your own plugins, you can add functionality and extend WordPress in any way imaginable. By tapping into the additional power and functionality that plugins provide, you can make your site easier to administer, add new features, or even alter the very nature of how WordPress works. Covering WordPress version 3, this book makes it super easy for you to build a variety of plugins. WordPress 3 Plugin Development Essentials is a practical hands-on tutorial for learning how to create your own plugins for WordPress. Using best coding practices, this book will walk you through the design and creation of a variety of original plugins. WordPress 3 Plugin Development Essentials focuses on teaching you all aspects of modern WordPress development. The book uses real and published WordPress plugins and follows their creation from the idea to the finishing touches in a series of easy-to-follow and informative steps. 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 to the world. Each new chapter introduces different features of WordPress and how to put them to good use, allowing you to gradually advance your knowledge. WordPress 3 Plugin Development Essentials is packed with information, tips, and examples that will help you gain comfort and confidence in your ability to harness and extend the power of WordPress via plugins. A step-by-step guide for creating feature-rich plug-ins for WordPress **What you will learn from this book** * Get to know the WordPress code base, WordPress' plugin architecture, and the application programming interface (API) * Integrate with third-party APIs and web services * Hook into the WordPress internals using actions and filters * Customize your WordPress Dashboard with menus and administration panels for your plugin * Streamline user interactions by using jQuery to make AJAX requests * Expand your understanding of WordPress as a CMS * Implement localization support for users of other languages * Maintain and manage your plugin using SVN and publish it to the WordPress Plugin Repository * Write tests into your code to ensure that it is always deployed properly **Approach** This book will provide an easy to understand, step-by-step approach to building your own WordPress plugins from scratch. We will construct a variety of plugins from the group up, demonstrating how to tap into a number of WordPress' development aspects, as well as how to prepare and release your plugin(s) to the public. **Who this book is written for** This book is for WordPress users who want to learn how to create their own plugins and for developers who are new to the WordPress platform. Basic knowledge of PHP and HTML is expected, as well as a functional knowledge of how WordPress works from a user standpoint. Cover 1 Copyright 3 Credits 4 About the Authors 5 About the Reviewers 6 www.PacktPub.com 8 Table of Contents 10 Preface 18 Chapter 1: Preparing for WordPress Development 24 WordPress background 24 Extending WordPress 25 Understanding WordPress architecture 25 Templating 26 Introducing plugins 26 Summarizing architecture 27 Tools for web development 28 WordPress 28 Mac 29 Windows 29 Text editor 29 Using an IDE 30 FTP client 31 MySQL client 31 Coding best practices 32 Basic organization 32 Isolate tasks into functions 33 Use classes 33 Use descriptive variable names 33 Use descriptive function names 34 Separate logic and display layers 34 Go modular, to a point 35 Avoid short tags 35 Planning ahead / starting development 35 Interfaces 36 Localization 36 Documentation for the developer 36 Version control 37 Environment 37 Tests 37 Security 38 Printing user-supplied data to a page 38 Using user-supplied data to construct database queries 39 Debugging 39 Clearing your browser cache 40 Updating your php.ini file 40 Configuring your wp-config.php file 40 Checking your syntax 41 Checking values 41 Exercise 42 Summary 43 Chaoter 2: Anatomy of a Plugin 46 Deconstructing an existing plugin: "Hello Dolly" 46 Activating the plugin 46 Examining the hello.php file 47 Information header 47 Exercise—breaking the header 47 Location, name, and format 48 Understanding the Includes 49 Exercise – parse errors 49 Bonus for the curious 50 User-defined functions 51 Exercise—an evil functionless plugin 51 What just happened 53 Omitting the closing "?>" PHP tag 55 A better example: Adding functions 55 Referencing hooks via add_action() and add_filter() 56 Actions versus Filters 57 Exercise—actions and filters 57 Exercise—filters 58 Reading more 60 Summary 61 Chapter 3: Social Bookmarking 62 The overall plan 63 Proof of concept 63 Avoiding conflicting function names 64 The master plugin outline 65 The plugin information header 67 In your browser—information header 68 Adding a link to the post content 68 Documenting our functions 69 In your browser—linking to the post content 69 Adding JavaScript to the head 69 Making our link dynamic 72 Adding a button template 74 Getting the post URL 75 In your browser—getting the post URL 77 Getting the post title 77 Getting the description 77 Getting the media type 79 Getting the post topic 79 In your browser—title, description, and topic 81 Checking WordPress versions 81 Summary 83 Chapter 4: Ajax Search 84 What is Ajax? 84 The overall plan 87 The proof of concept mock up 88 Hooking up jQuery 91 Test that jQuery has loaded 91 What happened? 92 Using the FireBug console directly 92 Writing HTML dynamically to a target div 93 Multi-line strings 94 Viewing the generated page 95 Anonymous functions 96 Adding a div on the fly 96 Create a listener 97 Fetching data from another page 98 Creating our plugin 100 Creating index.php and activating the plugin 101 Creating our first PHP class 102 Updating index.php 103 Testing your version of PHP 104 Testing for searchable pages 106 Adding your own CSS files 107 Adding your search handler 109 Adding your own JavaScript 109 Handling Ajax search requests 113 Formatting your search results 116 Summary 119 Chapter 5: Content Rotator 122 The plan 122 Widget overview 123 Preparation 123 Activating your plugin 127 Activating the widget 127 Having problems? 128 Parents and children: extending classes 129 Objects vs. libraries: when to use static functions 131 Add custom text 132 Adding widget options 133 Generating random content 138 Expiration dates: adding options to our widget 142 Expiration dates: enforcing the shelf life 143 Explaining the $instance 144 Adding a custom manager page 146 Adding options to the custom manager page 148 Randomizing content from the database 151 Review of PHP functions used 152 Summary 152 Chapter 6: Standardized Custom Content 154 What WordPress does for you: custom fields 155 What WordPress doesn't do for you 155 Standardizing a post's custom fields 156 Creating a new plugin 156 Removing the default WordPress form for custom fields 157 Creating our own custom meta box 160 Defining custom fields 162 Generating custom form elements 166 Saving custom content 172 Having trouble saving data? 174 Displaying custom data in your Templates 175 Copying a theme 175 Modifying the theme 176 Granular display of custom fields 178 Bonus for the MySQL curious 180 Known limitations 181 Summary 182 Chapter 7: Custom Post Types 184 Background: What's in a name? 185 Understanding register_post_type() 186 Customizing our post type 192 Using shortcodes 194 Testing our shortcode 197 Customizing our plugin 198 Creating a settings shortcut link 203 Cleaning up when uninstalling 205 Summary 207 Chapter 8: Versioning Your Code with Subversion (SVN) 208 Why Subversion? 209 Understanding the terminology and concepts 209 Checking out a local working copy 210 SVN folder structure 211 Checkout, revisited 213 Setting up an SVN repository 214 Checking out a local working copy of our repo 215 Adding files 216 Committing changes to the repository 217 Overcoming errors 218 Verifying the new state of your repository 219 Adding more files to your repository 220 Removing files from the repository 221 Updating your working copy 221 Tagging a version 222 Reverting an entire project 223 Reverting a single file 224 Moving files 225 Exporting your working copy 225 Quick reference 226 Summary 228 Chapter 9: Preparing Your Plugin for Distribution 230 Public enemy number one: PHP notices 230 PHP short tags 232 Conflicting names 232 Modifying loader.php 237 Testing WordPress version 239 Testing PHP version 239 Testing MySQL version 240 Testing PHP modules 240 Testing WordPress installed plugins 241 Custom tests 243 Unit tests 243 WordPress limitations 244 Health check page 244 Storing test results in the database 246 Death to clippy: Use sensible configurations 246 Double check your interface 247 Documentation 247 Identify the purpose 247 Learning to drive: Keeping it relevant 248 Phrasebooks vs. dictionaries: Give examples 249 Analogy: The three bears 249 Analogy: PC load letter 249 The decalogue of documentation 250 Summary 252 Chapter 10: Publishing Your Plugin 254 Internationalization and localization 254 Processing each message 255 Choosing a textdomain 257 Best practices 257 Working with formatting 258 More advanced messages 259 Plural vs. singular 259 More complex messages 260 Notes to translators 261 Language files 262 Creating a POT file 263 Creating translations: .po files 265 Loading a textdomain 267 Updating a translation 268 Format for the readme.txt file 269 Section—installation 270 Section—Frequently Asked Questions 270 Section—screenshots 270 New addition—videos 271 Section—summary 271 Requesting and using SVN access 272 Publicity and promotion 274 Summary 275 Appendix A: Recommended Resources 276 PHP reference 276 Function reference 276 The WordPress forums 276 WebDev Studios 277 Viper007Bond 277 Kovshenin 277 SLTaylor 277 XPlus3 277 WP Engineer 278 Other plugins 278 Appendix B: WordPress API Reference 280 PHP functions 280 dirname 280 file_get_contents 280 preg_match 281 preg_replace 281 print_r 281 sprintf 282 strtolower 282 substr 282 WordPress Functions 283 __ 283 _e 283 add_action 283 add_filter 284 add_meta_box 284 add_options_page 284 check_admin_referer 284 esc_html 285 get_option 285 get_post_meta 285 get_the_ID 285 register_post_type 286 remove_meta_box 286 screen_icon 286 the_content 286 the_meta 286 update_post_meta 287 wp_count_posts 287 wp_die 287 wp_nonce_field 287 Actions 287 admin_init 287 admin_menu 287 do_meta_boxes 288 init 288 save_post 288 widgets_init 288 wp_head 289 Filters 289 the_content 289 Index 290
In Detail
WordPress is one of the most popular platforms for building blogs and general websites. By learning how to develop and integrate your own plugins, you can add functionality and extend WordPress in any way imaginable. By tapping into the additional power and functionality that plugins provide, you can make your site easier to administer, add new features, or even alter the very nature of how WordPress works. Covering WordPress version 3, this book makes it super easy for you to build a variety of plugins.
WordPress 3 Plugin Development Essentials is a practical hands-on tutorial for learning how to create your own plugins for WordPress. Using best coding practices, this book will walk you through the design and creation of a variety of original plugins.
WordPress 3 Plugin Development Essentials focuses on teaching you all aspects of modern WordPress development. The book uses real and published WordPress plugins and follows their creation from the idea to the finishing touches in a series of easy-to-follow and informative steps. 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 to the world.
Each new chapter introduces different features of WordPress and how to put them to good use, allowing you to gradually advance your knowledge. WordPress 3 Plugin Development Essentials is packed with information, tips, and examples that will help you gain comfort and confidence in your ability to harness and extend the power of WordPress via plugins.
A step-by-step guide for creating feature-rich plug-ins for WordPress
Approach
This book will provide an easy to understand, step-by-step approach to building your own WordPress plugins from scratch. We will construct a variety of plugins from the group up, demonstrating how to tap into a number of WordPress' development aspects, as well as how to prepare and release your plugin(s) to the public.
Who this book is for
This book is for WordPress users who want to learn how to create their own plugins and for developers who are new to the WordPress platform. Basic knowledge of PHP and HTML is expected, as well as a functional knowledge of how WordPress works from a user standpoint.