wordpress as an application framework

26
WordPress as an Application Framework Dustin Filippini Developer @ WebDevStudios @dustyf dustyf.com

Upload: dustin-filippini

Post on 14-Jul-2015

127 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: WordPress as an application framework

WordPress as an

Application Framework

Dustin Filippini Developer @ WebDevStudios

@dustyf dustyf.com

Page 2: WordPress as an application framework
Page 3: WordPress as an application framework
Page 4: WordPress as an application framework
Page 5: WordPress as an application framework

WWhat Makes a Framework Good?

• Easy

• Well Supported

• Extensible

Page 6: WordPress as an application framework

EASYIt does workfor you

Page 7: WordPress as an application framework

WellSupportedWe are here to help!

Page 8: WordPress as an application framework

ExtensibleAdd on More Dogs

Page 9: WordPress as an application framework

A Real World ExampleCreate a billing/invoicing system using WordPress!

Think… FreshBooks

Page 10: WordPress as an application framework

“I need an easy, reusable way to store, access, and categorize structured data”

Custom Post Types & Custom Taxonomies.

function register_invoice() { $args = array( 'public' => true, 'label' => 'Invoices' ); register_post_type( 'invoice',$args ); } add_action(‘init','register_invoice');

function create_invoice_status() { register_taxonomy( 'status', 'invoice', array( 'label' => ‘Invoice Status', 'rewrite' => array( 'slug' => ‘invoice_status' ), 'hierarchical' => false, ) ); } add_action( 'init', ‘create_invoice_status' );

Page 11: WordPress as an application framework

“I need to store data related to my structured

data and users.”

Post Meta and User Meta.

update_post_meta( $post_id, ‘purchase_order_number’, $value );

update_user_meta( $user_id, ‘job_title’, $value );

Page 12: WordPress as an application framework

“I need users to be able to access

data and limit access to

different users”

Create users and assign roles and capabilities to access different parts of

your application.

Built-in User Management panel and ability to manipulate through your code.

ADD A NEW ROLE

add_role( ‘client', ‘Client', array( 'read' => true, 'edit_posts' => true, ) );

ADD A CAPABILITY TO THE ROLE

$role = get_role( ‘client' ); $role->add_cap( ‘view_invoices’ );

RESTRICT ACTION

if ( current_user_can( ‘view_invoices’ ) { // Do Something

}

Page 13: WordPress as an application framework

“I need to create custom views for different

types of data"

Templating through WordPress Theme API.

Allows you to define different templates for different types

of content and views.

BASIC WORDPRESS PAGE LOOP

get_header(); if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile; endif; get_sidebar(); get_footer();

TEMPLATE FILE NAMING

single-invoices.php - Single Invoice View archive-invoices.php - Archive of Invoices

LOADING CUSTOM TEMPLATES

get_template_part( 'loop', 'index' );

will do a PHP require() for the first file that exists among these, in this priority:

wp-content/themes/themename/loop-index.php wp-content/themes/themename/loop.php

Page 14: WordPress as an application framework

“I need to customize

default actions of my app”

WordPress has hooks for nearly everything that happens.

Use ‘add_action’ to do execute custom code at a certain time.

Use ‘add_filter’ to change default values.

function email_invoice( $post_id ) { $to = ‘[email protected]’; $subject = get_the_title( $post_id ); $message = get_the_permalink( $post_id );

wp_mail( $to, $subject, $message ); } add_action(‘save_post’,‘email_invoice’);

function mod_title( $title, $post_id ) { if ( ‘invoice’ == get_post_type($post_id) &&

has_term( ‘paid’, ‘invoice_status’, $post_id

) { $title = ‘PAID: ‘ . $title;

} return $title;

} add_filter( ‘the title’, ‘mod_title’, 10, 2 );

Page 15: WordPress as an application framework

“I need easy, secure, access

to the database”

The wpdb class provides easy, secure database

interaction for those times you need it.

global $wpdb;

$results = $wpdb->get_results( 'SELECT * FROM wp_options WHERE option_id = 1');

$wpdb->insert( ‘payments_made', array( 'client' => 'Apple', ‘invoice_num' => 123 ), array( '%s', '%d' ) );

// PROTECT FROM SQL INJECTION

$metakey = ‘purchase_order_num’; $metavalue = ‘12345’; $wpdb->query( $wpdb->prepare( "INSERT INTO $wpdb->postmeta ( post_id, meta_key, meta_value ) VALUES ( %d, %s, %s )", 10, $metakey, $metavalue ) );

Page 16: WordPress as an application framework

“I need to have a reliable way

to rewrite URLs”

WordPress rewrites posts, pages, taxonomies, and

custom post types by default. But, you can use the Rewrite API to make

custom rules.

function custom_rewrite() { add_rewrite_rule('^invoice/([0-9]+)/?', 'index.php?page_id=$matches[1]', 'top'); } add_action(‘init','custom_rewrite');

Now, you can access the same page as http://example.com/invoice/95

Page 17: WordPress as an application framework

“Different app instances for

different users/clients/

companies.”

WordPress multisite allows you to create multiple site/app instances. Each can share or have different

functionality, views, settings, userbases, and more.

Add the following to your wp-config.php and follow the instructions in wp-admin under Settings > Network Setup

define( 'WP_ALLOW_MULTISITE', true );

Use Case: Each company using this invoice system can have their own instance at their own url…

google.example.com or example.com/google

Page 18: WordPress as an application framework

“I need to be able to connect with third-party

services”

Built in HTTP API supports multiple transport methods

for your HTTP requests because different hosting

environments support different things.

$response = wp_remote_post( $url, array( 'method' => 'POST', 'timeout' => 45, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => array( 'username' => 'bob', 'password' => '1234xyz' ), 'cookies' => array() ) );

$response returns a nice array

AlSO: wp_remote_request() wp_remote_get() wp_remote_post() wp_remote_head() wp_remote_retrieve_body() wp_remote_retrieve_header() wp_remote_retrieve_headers() wp_remote_retrieve_response_code() wp_remote_retrieve_response_message()

Page 19: WordPress as an application framework

“I need to improve

performance by limiting requests”

Transients allow you quickly store data with an expiration

for quick access.

Get more in depth with caching plugins.

set_transient( ‘special_query’, $special_query, 60*60*12

);

get_transient( 'special_query' );

//EXAMPLE

if ( false === ( $special_query = get_transient( 'special_query' ) ) ) {

$special_query_results = new WP_Query( 'cat=5&order=random&tag=tech&post_meta_key=thumbnail' );

set_transient( 'special_query', $special_query, 4 * HOUR_IN_SECONDS

); }

- get_transient - set_transient - wp_cache_set - wp_cache_get

Page 20: WordPress as an application framework

“I need to be able to open

my application to third parties”

Create an API for your app using the XML-RPC API or the new JSON REST API

JSON REST API will be in WP Core soon, but is now available in a stable plugin.

Out of the box provides:

• Endpoints for retrieving, creating, editing, and deleting all built-in post types and registered custom post types

• Also, media, users, taxonomies and meta

• Can be extended to support your other types of data

Page 21: WordPress as an application framework

“I don’t want to create things that have been done

hundreds of times before”

Thousands of WordPress Plugins both free and

premium. Also, feel free to use other PHP Libraries.

35,000+ Free Plugins at wordpress.org/plugins/ and many great premium plugins. BuddyPress - Social Network BBPress - Forums BadgeOS - Award Achievements CMB2 - Custom Fields and Metaboxes Gravity Forms - Form Building Posts 2 Posts - Relational post linking WooCommerce - eCommerce Events Calendar Pro - Calendar

Or build your own to use over and over (or to share or sell!)

Page 22: WordPress as an application framework

MOAREXAMPLES

WordPress is being used as an application framework all

over the web.

Reactor by AppPresser (http://reactor.apppresser.com/) WP Powered application that builds mobile applications from your WP site.

DesktopServer 4.0 (http://serverpress.com/) Desktop application to create local development sites.

ManageWP (http://managewp.com/) Upgrade, perform backups, monitor, track statistics, and manage multiple WP sites.

YMCA Y-MVP App (http://www.ymcanyc.org/association/pages/y-mvp) iPad application used as a kiosk at YMCA to allow members to scan card and log exercise activities.

Frito-Lay Project Management (http://liftux.com/portfolio/frito-lay-creative-project-management-app/) Custom project management app.

Page 23: WordPress as an application framework

Easy

Page 24: WordPress as an application framework

WellSupported

Page 25: WordPress as an application framework

Extensible

Page 26: WordPress as an application framework

Go Forth& Create Apps

Dustin Filippini [email protected] @dustyf

dustyf.com webdevstudios.com