Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Building Web Apps with WordPress
Preface
Who This Book Is For
Who This Book Is Not For
What You’ll Learn
About the Code
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
Foreword
1. Building Web Apps with WordPress
What Is a Website?
What Is an App?
What Is a Web App?
Features of a Web App
Why Use WordPress?
You Are Already Using WordPress
Content Management Is Easy with WordPress
User Management Is Easy and Secure with WordPress
Plugins
Flexibility Is Important
Frequent Security Updates
Cost
.NET App
WordPress App
Responses to Some Common Criticisms of WordPress
When Not to Use WordPress
You Plan to License or Sell Your Site’s Technology
There Is Another Platform That Will Get You “There” Faster
Flexibility Is NOT Important to You
Your App Needs to Be Highly Real Time
WordPress as an Application Framework
WordPress Versus MVC Frameworks
MVC plugins for WordPress
Models = plugins
Views = themes
Controllers = template loader
Anatomy of a WordPress App
What Is SchoolPress?
SchoolPress Runs on a WordPress Multisite Network
The SchoolPress Business Model
Membership Levels and User Roles
Classes Are BuddyPress Groups
Assignments Are a Custom Post Type
Submissions Are a (Sub)CPT for Assignments
Semesters Are a Taxonomy on the Class CPT
Departments Are a Taxonomy on the Class CPT
SchoolPress Has One Main Custom Plugin
SchoolPress Uses a Few Other Custom Plugins
SchoolPress Uses the StartBox Theme Framework
2. WordPress Basics
WordPress Directory Structure
Root Directory
/wp-admin
/wp-includes
/wp-content
/wp-content/plugins
/wp-content/themes
/wp-content/uploads
/wp-content/mu-plugins
WordPress Database Structure
wp_options
Functions Found in /wp-includes/option.php
add_option( $option, $value = ', $deprecated = ', $autoload = yes )
update_option( $option, $newvalue )
get_option( $option, $default = false )
delete_option( $option )
wp_users
Functions Found in /wp-includes/…
wp_insert_user( $userdata )
wp_create_user( $username, $password, $email )
wp_update_user( $userdata )
get_user_by( $field, $value )
get_userdata( $userid )
wp_delete_user( $id, $reassign = novalue )
wp_usermeta
get_user_meta( $user_id, $key = '', $single = false )
update_user_meta( $user_id, $meta_key, $meta_value, $prev_value = '' )
add_user_meta($user_id, $meta_key, $meta_value, $unique = false)
delete_user_meta($user_id, $meta_key, $meta_value = '')
wp_posts
Functions found in /wp-includes/post.php
wp_insert_post($postarr, $wp_error = false)
wp_update_post( $postarr = array(), $wp_error = false )
get_post( $post = null, $output = OBJECT, $filter = raw )
get_posts($args = null)
wp_delete_post( $postid = 0, $force_delete = false )
wp_postmeta
Functions Found in /wp-includes/post.php
get_post_meta($post_id, $key = '', $single = false)
update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '')
add_post_meta($post_id, $meta_key, $meta_value, $unique = false)
delete_post_meta($post_id, $meta_key, $meta_value = '')
wp_comments
Functions Found in /wp-includes/comment.php
get_comment( $comment, $output = OBJECT )
get_comments( $args = '' )
wp_insert_comment( $commentdata )
wp_update_comment( $commentarr )
wp_delete_comment( $comment_id, $force_delete = false )
wp_commentsmeta
Functions Found in /wp-includes/comment.php
get_comment_meta($comment_id, $key = '', $single = false)
add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false)
update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '')
delete_comment_meta($comment_id, $meta_key, $meta_value = '')
wp_links
wp_terms
Functions Found in /wp-includes/taxonomy.php
get_terms( $taxonomies, $args = '' )
get_term( $term, $taxonomy, $output = OBJECT, $filter = raw )
wp_insert_term( $term, $taxonomy, $args = array() )
wp_update_term( $term_id, $taxonomy, $args = array() )
wp_delete_term( $term, $taxonomy, $args = array() )
wp_term_taxonomy
/wp-includes/taxonomy.php
get_taxonomies( $args = array(), $output = names, $operator = and )
get_taxonomy( $taxonomy )
register_taxonomy( $taxonomy, $object_type, $args = array() )
wp_term_relationships
get_object_taxonomies( $object, $output = names )
wp_get_object_terms( $object_ids, $taxonomies, $args = array() )
wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false )
Extending WordPress
3. Leveraging WordPress Plugins
The GPLv2 License
Installing WordPress Plugins
Building Your Own Plugin
File Structure for an App Plugin
/adminpages/
/classes/
/css/
/js/
/images/
/includes/
/includes/lib/
/pages/
/services/
/scheduled/
/schoolpress.php
Add-Ons to Existing Plugins
Use Cases and Examples
The WordPress Loop
WordPress Global Variables
$wpdb
Using custom DB tables
Running queries
Escaping in DB queries
SELECT queries with $wpdb
Insert, replace, and update
Action Hooks
Filters
Free Plugins
All in One SEO Pack
BadgeOS
Custom Post Type UI
Posts 2 Posts
Members
W3 Total Cache
Premium Plugins
Gravity Forms
Backup Buddy
WP All Import
Community Plugins
BuddyPress
Database tables
Components
Pages
Settings
Profile fields
BuddyPress plugins
4. Themes
Themes Versus Plugins
When Developing Apps
When Developing Plugins
When Developing Themes
The Template Hierarchy
Page Templates
Sample Page Template
Using Hooks to Copy Templates
When to Use a Theme Template
Theme-Related WP Functions
Using locate_template in Your Plugins
Style.css
Versioning Your Theme’s CSS Files
Functions.php
Themes and Custom Post Types
Popular Theme Frameworks
WP Theme Frameworks
_s (Underscores)
StartBox
Genesis
Non-WP Theme Frameworks
Creating a Child Theme for StartBox
Including Bootstrap in Your App’s Theme
Menus
Nav Menus
Dynamic Menus
Responsive Design
Device and Display Detection in CSS
Device and Feature Detection in JavaScript
Detecting the screen and window size with JavaScript and jQuery
Feature detection in JavaScript
Device Detection in PHP
Browser detection in WordPress core
Browser detection with PHP’s get_browser()
Final Note on Browser Detection
Versioning CSS and JS Files
5. Custom Post Types, Post Metadata, and Taxonomies
Default Post Types and Custom Post Types
Page
Post
Attachment
Revisions
Nav Menu Item
Defining and Registering Custom Post Types
register_post_type( $post_type, $args );
label
labels
menu_name
description
publicly_queryable
exclude_from_search
capability_type
capabilities
map_meta_cap
hierarchical
public
rewrite
has_archive
query_var
supports
register_meta_box_cb
permalink_epmask
taxonomies
show_ui
menu_position
menu_icon
can_export
show_in_nav_menus
show_in_menu
show_in_admin_bar
delete_with_user
_builtin
_edit_link
What Is a Taxonomy and How Should I Use It?
Taxonomies Versus Post Meta
Creating Custom Taxonomies
register_taxonomy( $taxonomy, $object_type, $args )
label
labels
hierarchical
update_count_callback
rewrite
query_var
public
show_ui
show_in_nav_menus
show_tagcloud
show_admin_column
capabilities
register_taxonomy_for_object_type( $taxonomy, $object_type )
Using Custom Post Types and Taxonomies in Your Themes and Plugins
The Theme Archive and Single Template Files
Good Old WP_Query and get_posts()
Metadata with CPTs
add_meta_box( $id, $title, $callback, $screen, $context, $priority, $callback_args )
Custom Wrapper Classes for CPTs
Extending WP_Post Versus Wrapping It
Why Use Wrapper Classes?
Keep Your CPTs and Taxonomies Together
Keep It in the Wrapper Class
Wrapper Classes Read Better
6. Users, Roles, and Capabilities
Getting User Data
Add, Update, and Delete Users
Hooks and Filters
What Are Roles and Capabilities?
Checking a User’s Role and Capabilities
Creating Custom Roles and Capabilities
Extending the WP_User Class
Adding Registration and Profile Fields
Customizing the Users Table in the Dashboard
Plugins
Theme My Login
Hide Admin Bar from Non-Admins
Paid Memberships Pro
PMPro Register Helper
Members
7. Other WordPress APIs, Objects, and Helper Functions
Shortcode API
Shortcode Attributes
Nested Shortcodes
Removing Shortcodes
Other Useful Shortcode-Related Functions
Widgets API
Before You Add Your Own Widget
Adding Widgets
Defining a Widget Area
Embedding a Widget Outside of a Dynamic Sidebar
Dashboard Widgets API
Removing Dashboard Widgets
Adding Your Own Dashboard Widget
Settings API
Do You Really Need a Settings Page?
Could You Use a Hook or Filter Instead?
Use Standards When Adding Settings
Ignore Standards When Adding Settings
Rewrite API
Adding Rewrite Rules
Flushing Rewrite Rules
Other Rewrite Functions
WP-Cron
Adding Custom Intervals
Scheduling Single Events
Kicking Off Cron Jobs from the Server
Using Server Crons Only
WP Mail
Sending Nicer Emails with WordPress
File Header API
Adding File Headers to Your Own Files
Adding New Headers to Plugins and Themes
8. Secure WordPress
Why It’s Important
Security Basics
Update Frequently
Don’t Use the Username “admin”
Use a Strong Password
Examples of Bad Passwords
Examples of Good Passwords
Hardening Your WordPress Install
Don’t Allow Admins to Edit Plugins or Themes
Change Default Database Tables Prefix
Move wp-config.php
Hide Login Error Messages
Hide Your WordPress Version
Don’t Allow Logins via wp-login.php
Add Custom .htaccess Rules for Locking Down wp-admin
Backup Everything!
Scan Scan Scan!
Useful Security Plugins
Spam-Blocking Plugins
Akismet
Bad Behavior
Backup Plugins
Backup Buddy
VaultPress
Scanner Plugins
WP Security Scan
Exploit Scanner
BBQ
Antivirus-Once
Login and Password-Protection Plugins
Limit Login Attempts
Ask Apache Password Protect
Writing Secure Code
Check User Capabilities
user_can( $user, $capability )
current_user_can( $capability )
current_user_can_for_blog( $blog_id, $capability )
Custom SQL Statements
Data Validation, Sanitization, and Escaping
esc_url( $url, $protocols = null, $_context = display )
esc_url_raw( $url, $protocols = null )
esc_html( $text )
esc_js( $text )
esc_attr( $text )
esc_textarea( $text )
sanitize_option( $option, $value )
sanitize_text_field($str)
sanitize_user( $username, $strict = false )
sanitize_title( $title, $fallback_title = '' )
sanitize_email( $email )
sanitize_file_name( $filename )
wp_kses( $string, $allowed_html, $allowed_protocols = array () )
Nonces
wp_create_nonce( $action = -1 )
wp_verify_nonce($nonce, $action = -1)
check_admin_referer($action = -1, $query_arg = _wpnonce)
wp_nonce_url( $actionurl, $action = -1 )
wp_nonce_field( $action = -1, $name = “_wpnonce”, $referer = true , $echo = true )
check_ajax_referer( $action = -1, $query_arg = false, $die = true )
9. JavaScript, jQuery, and AJAX
What Is AJAX?
What Is JSON?
jQuery and WordPress
Enqueuing Other JavaScript Libraries
Where to Put Your Custom JavaScript
AJAX Calls with WordPress and jQuery
Managing Multiple AJAX Requests
Heartbeat API
Initialization
Client-side JavaScript
Server-side PHP
Initialization
Client-side JavaScript
Server-side PHP
WordPress Limitations with Asynchronous Processing
Backbone.js
10. XML-RPC
wp.getUsersBlogs
wp.getPosts
wp.getPost
wp.newPost
wp.editPost
wp.deletePost
wp.getTerms
wp.getTerm
wp.newTerm
wp.editTerm
wp.deleteTerm
wp.getTaxonomies
wp.getTaxonomy
wp.getUsers
wp.getUser
wp.getProfile
wp.editProfile
wp.getCommentCount
wp.getPageTemplates
wp.getOptions
wp.setOptions
wp.getComment
wp.getComments
wp.deleteComment
wp.editComment
wp.newComment
wp.getMediaLibrary
wp.getMediaItem
wp.uploadFile
wp.getPostFormats
wp.getPostType
wp.getPostTypes
11. Mobile Apps with WordPress
App Wrapper
iOS Applications
Enrolling as an Apple Developer
Building Your App with Xcode
Storyboard
View controller
iOS simulator
App Distribution
iOS Resources
Android Applications
AndroidManifest.xml
activity_main.xml
MainActivity.java
Creating an APK file
Getting Your App on Google Play
Android Resources
Extend Your App
AppPresser
Mobile App Use Cases
12. PHP Libraries, External APIs, and Web Services
Imagick
MaxMind GeoIP
Google Maps JavaScript API v3
Directions
Distance Matrix
Elevation
Geocoding
Street View Service
Practical App
Google Translate
Google+
People
Activities
Comments
Moments
Amazon Product Advertising API
Request Parameters
Operations
Response Groups
Twitter REST API v1.1
Set Up Your App on Twitter.com
Leverage a PHP Library
Facebook
Pictures
Search
Permissions
Building an Application
Leverage What’s Out There
Twilio
Microsoft Sharepoint
We Missed a Few
13. Building WordPress Multisite Networks
Why Multisite?
Setting Up a Multisite Network
Managing a Multisite Network
Dashboard
Sites
Users
Themes
Plugins
Settings
Operational Settings
Registration Settings
New Site Settings
Upload Settings
Menu Settings
Updates
Multisite Database Structure
Network-Wide Tables
wp_blogs
wp_blog_versions
wp_registration_log
wp_signups
wp_site
wp_sitemeta
Individual Site Tables
Shared Site Tables
Multisite Plugins
WordPress MU Domain Mapping
Blog Copier
More Privacy Options
Multisite Global Search
Multisite Robots.txt Manager
Basic Multisite Functionality
$blog_id
is_multisite()
get_current_blog_id()
switch_to_blog( $new_blog )
restore_current_blog()
get_blog_details( $fields = null, $get_all = true )
update_blog_details( $blog_id, $details = array() )
get_blog_status( $id, $pref )
update_blog_status( $blog_id, $pref, $value )
get_blog_option( $id, $option, $default = false )
update_blog_option( $id, $option, $value )
delete_blog_option( $id, $option )
get_blog_post( $blog_id, $post_id )
add_user_to_blog( $blog_id, $user_id, $role )
create_empty_blog( $domain, $path, $weblog_title, $site_id = 1 )
Functions We Didn’t Mention
14. Localizing WordPress Apps
Do You Even Need to Localize Your App?
How Localization Is Done in WordPress
Defining Your Locale in WordPress
Prepping Your Strings with Translation Functions
__($text, $domain = “default”)
_e($text, $domain = “default”)
_x($text, $context, $domain = “default”)
_ex($title, $context, $domain = “default”)
Escaping and Translating at the Same Time
Creating and Loading Translation Files
Our File Structure for Localization
Generating a .pot File
Creating a .po File
Creating a .mo File
Loading the Textdomain
Localizing Nonstring Assets
15. Ecommerce
Choosing a Plugin
Shopping Cart Plugins
Our favorite: Jigoshop
Notable runner-up: WooCommerce
Membership Plugins
Our favorite: Paid Memberships Pro
Digital Downloads
Our favorite: Easy Digital Downloads
Payment Gateways
Merchant Accounts
SSL Certificates and HTTPS
Installing an SSL Certificate on Your Server
SSL with Paid Memberships Pro
SSL with Jigoshop
WordPress Login and WordPress Admin over SSL
WordPress Frontend over SSL
SSL on Select Pages
Avoiding SSL Errors with the “Nuclear Option”
Setting Up Software as a Service (SaaS) with Paid Memberships Pro
The Software as a Service Model
Step 0: Figure Out How You Want to Charge for Your App
Step 1: Installing and Activating Paid Memberships Pro
Step 2: Setting Up the Level
Step 3: Setting Up Pages
Step 4: Payment Settings
Step 5: Email Settings
Step 6: Advanced Settings
Step 7: Locking Down Pages
Lock down a specific page
Lock down a page by URL
Lock down a portion of a page by shortcode
Lock down a portion of a page by PHP code using the pmpro_hasMembershipLevel() function
Step 8: Customizing Paid Memberships Pro
Restricting nonmembers to the homepage
Locking down files
Change user roles based on membership levels
International and long-form addresses
Upgrade/downgrade pricing
16. WordPress Optimization and Scaling
Terms
Origin Versus Edge
Testing
What to Test
Chrome Debug Bar
Apache Bench
Installing Apache Bench
Running Apache Bench
Testing with Apache Bench
Graphing Apache Bench results with gnuplot
Siege
Blitz.io
W3 Total Cache
Page Cache Settings
Minify
Database Caching
Object Cache
CDNs
GZIP Compression
Hosting
WordPress-Specific Hosts
Rolling Your Own Server
Apache server setup
Nginx server setup
Nginx in front of Apache
MySQL optimization
advanced-cache.php and object-cache.php
Alternative PHP Cache (APC)
Memcached
Redis
Varnish
Batcache
Selective Caching
The Transient API
Multisite Transients
Using JavaScript to Increase Performance
Custom Tables
Bypassing WordPress
Index
Colophon
Copyright
← Prev
Back
Next →
← Prev
Back
Next →