SlideShare a Scribd company logo
1 of 46
Using WordPress
as a framework
Marco Martins
WordPress is a
blogging tool
WordPress is a
Content Management System
WordPress is a web
application framework
but is it?
A web application framework is a software
framework that is designed to support the
development of dynamic websites, web applications,
web services and web resources. The framework
aims to alleviate the overhead associated with
common activities performed in web development.
For example, many frameworks provide libraries for
database access, templating frameworks and session
management, and they often promote code reuse.
from Wikipedia (2015, May 9)
Data Model
• Custom Post Types
• Post Meta
• Taxonomies
• Posts 2 Posts
Custom Post Types
• Represent the model definition
• You can define:
• some content fields
• visibility (queryable, searchable, show in menus…)
• capabilities
• hierarchical
• rewrites & query vars
register_post_type( $post_type, $args );
Post Meta
• Adds extra fields to the model
• Extends the basic model data (title, editor,
image…) with extra fields (wysiwyg, embed,
field groups, repeatable fields…
• Plugins: CMB2, ACF, Custom-Meta-Boxes
Taxonomies
• Custom taxonomies
• Custom taxonomies can be attached to specific
content types
register_taxonomy( $taxonomy, $object_type, $args );
Posts 2 Posts
• Plugin
• Creates relationships between content types (including users)
• Supports cardinality
• Allows relationship meta data.
• Development of the plugin has not been active
• @link https://wordpress.org/plugins/posts-to-posts/
Template System
Template System
• PHP based templating
• Templates can be used in plugins
• Templates can be loaded programatically
Accessing Data
WP_Query
• Very central class in WordPress
• Makes queries by: post type, taxonomy, post meta, date,
taxonomy
$args = array(
'post_type' => ‘address',
'tax_query' => array(
array(
'taxonomy' => ‘type',
'field' => ‘slug',
'terms' => ‘shipping’,
),
),
);
$addresses = new WP_Query( $args );
$wpdb
• Global object to access the database
• Escapes data
$wpdb->insert( $table, $data, $format );
$wpdb->replace( $table, $data, $format );
$wpdb->update( $table, $data, $where, [$format],
[$where_format] );
$wpdb->delete( $table, $where, $where_format = null );
$wpdb->prepare( 'query', value_parameter[, value_parameter] );
Users
Users
• Supports user roles
• Supports user capabilities
• User information can be extended
• New roles and capabilities can be created
• Meta capabilities can be created
• Authentication method can be changed using hooks
Internationalization
&
Localization
Internationalization
• Gettext functions (partial list):
__( $text, $text_domain );
_e( $text, $text_domain );
_x( $text, $context, $text_domain );
• Loading text domain
function demo_load_textdomain() {
load_plugin_textdomain( ‘demo', FALSE,
basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'demo_load_textdomain' );
Localization
• POT files
• i18n tools (makepot.php / grunt-pot)
Ajax
Ajax
• Set the Ajax URL
function set_ajax_url() { ?>
<script type=“text/javascript">
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
</script>
<?php } ?>
Ajax
• Register the request handler
function callback() {
echo json_encode( array( ‘success’ => true ) ); die();
}
add_action( ‘wp_ajax_demo', 'callback' );
add_action( 'wp_ajax_nopriv_demo', 'callback' );
Ajax
• Making the requests
$.getJSON(ajaxurl, {action: ‘demo' }, function( data ){
console.log(‘data’);
});
Consuming
External APIs
HTTP API
• Standardizes and abstracts HTTP requests
• Supports and abstracts multiple PHP HTTP methods (cURL,
file_get_contents)
wp_remote_get( $url, $args ); / wp_remote_post( $url, $args );
wp_remote_request( $url, $args );
wp_remote_retrieve_body( $response );
wp_remote_retrieve_headers( $response, $header );
wp_remote_retrieve_response_code( $response );
API Provider
WP REST API
• Provides a HTTP REST API to interact with WordPress
• Posts, Post Meta, Users, Media… can be created, read, changed, and deleted
• Basic Authentication and Oauth are available
• Currently the API is available as a plugin but soon it will be part of the WordPress core
• @link http://wp-api.org/
GET /posts
GET /posts/<id>
*POST /posts
*PUT /posts/<id>
*POST /posts/<id>/meta
Caching
Transients API
• Core API
• Similar to the Options API
• Data can be stored in the database, Redis,
Memcached…
• Can be used on the network level and site level
Transients API
set_transient( $transient, $value, $expiration_in_seconds )
get_transient( $transient );
delete_transient( $transient );
set_site_transient( $transient, $value, $expiration_in_seconds );
get_site_transient( $transient );
delete_site_transient( $transient );
TLC Transients
• Soft expiration
• Can refresh the cache in the background
• https://github.com/markjaquith/WP-TLC-
Transients
TLC Transients
<?php
function my_callback_with_param( $param ) {
return str_replace(
'foo',
$param,
wp_remote_retrieve_body( wp_remote_get( 'http://example.com/feed.xml', array( 'timeout' => 30 ) ) ),
);
}
echo tlc_transient( 'example-feed' )
->updates_with( 'my_callback_with_param', array( 'bar' ) )
->expires_in( 300 )
->background_only()
->get();
URLs
Rewrite API
• Allows you to create new rewrite rules and
permalink structures programatically
• You can create “pages” without having to create
a page in the dashboard.
• Uses WP_Rewrite API
Rewrite API
add_rewrite_tag( '%orders%', '([^&]+)' );
add_rewrite_tag( ‘%order_id%', '([^&]+)' );
add_rewrite_rule( $base .
'/orders/([^/]*)/?$',
'index.php?' . $base .
'=true&orders=true&order_id=$matches[1]',
'top'
);
More Tools
More Tools
• WP_Error
• Options API
• Filesystem API
• Settings API
• Shortcode API
• Input Validation & Output Sanitization
• Media Management Functions
• Custom Post Statuses
• …
Advantages
Advantages
• Dashboard
• Multisite
• Documentation & Community
• Plugin repository
• Hooks (actions & filters) in both core and plugins
• Automatic updates
But it’s not all rainbows
and butterflies
Disadvantages
• Lack of a proper template engine
• Doesn’t enforce a strict MVC
• Performance (you need to load WordPress)
Extra
• WP_Mock (mocks WordPress for unit tests)
• Caching plugins (W3 Total Cache, WP Super
Cache)
• Plugin frameworks
• WPlinth
• @fimdalinha
• https://fi.linkedin.com/in/marconmartins
• h1.fi

More Related Content

What's hot

Decoupled Libraries for PHP
Decoupled Libraries for PHPDecoupled Libraries for PHP
Decoupled Libraries for PHP
Paul Jones
 
Wordcamp Fayetteville Pods Presentation (PDF)
Wordcamp Fayetteville Pods Presentation (PDF)Wordcamp Fayetteville Pods Presentation (PDF)
Wordcamp Fayetteville Pods Presentation (PDF)
mpvanwinkle
 
Cwinters Intro To Rest And JerREST and Jersey Introductionsey
Cwinters Intro To Rest And JerREST and Jersey IntroductionseyCwinters Intro To Rest And JerREST and Jersey Introductionsey
Cwinters Intro To Rest And JerREST and Jersey Introductionsey
elliando dias
 

What's hot (20)

Deploy and Manage the Infrastructure Using Azure Resource Manager
Deploy and Manage the Infrastructure Using Azure Resource ManagerDeploy and Manage the Infrastructure Using Azure Resource Manager
Deploy and Manage the Infrastructure Using Azure Resource Manager
 
8b. Column Oriented Databases Lab
8b. Column Oriented Databases Lab8b. Column Oriented Databases Lab
8b. Column Oriented Databases Lab
 
Couch db
Couch dbCouch db
Couch db
 
Resource Routing in ExpressionEngine
Resource Routing in ExpressionEngineResource Routing in ExpressionEngine
Resource Routing in ExpressionEngine
 
Drupal Camp Porto - Developing with Drupal: First Steps
Drupal Camp Porto - Developing with Drupal: First StepsDrupal Camp Porto - Developing with Drupal: First Steps
Drupal Camp Porto - Developing with Drupal: First Steps
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.js
 
Decoupled Libraries for PHP
Decoupled Libraries for PHPDecoupled Libraries for PHP
Decoupled Libraries for PHP
 
Drupal 7 database api
Drupal 7 database api Drupal 7 database api
Drupal 7 database api
 
Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018Supercharging WordPress Development in 2018
Supercharging WordPress Development in 2018
 
Relevance trilogy may dream be with you! (dec17)
Relevance trilogy  may dream be with you! (dec17)Relevance trilogy  may dream be with you! (dec17)
Relevance trilogy may dream be with you! (dec17)
 
OSCON 2011 CouchApps
OSCON 2011 CouchAppsOSCON 2011 CouchApps
OSCON 2011 CouchApps
 
Wordcamp Fayetteville Pods Presentation (PDF)
Wordcamp Fayetteville Pods Presentation (PDF)Wordcamp Fayetteville Pods Presentation (PDF)
Wordcamp Fayetteville Pods Presentation (PDF)
 
Cwinters Intro To Rest And JerREST and Jersey Introductionsey
Cwinters Intro To Rest And JerREST and Jersey IntroductionseyCwinters Intro To Rest And JerREST and Jersey Introductionsey
Cwinters Intro To Rest And JerREST and Jersey Introductionsey
 
Drupal 7 Theming - Behind the scenes
Drupal 7 Theming - Behind the scenes Drupal 7 Theming - Behind the scenes
Drupal 7 Theming - Behind the scenes
 
JavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to knowJavaScript in Drupal 7: What developers need to know
JavaScript in Drupal 7: What developers need to know
 
Scaling in Mind (Case study of Drupal Core)
Scaling in Mind (Case study of Drupal Core)Scaling in Mind (Case study of Drupal Core)
Scaling in Mind (Case study of Drupal Core)
 
Cutting Edge Data Processing with PHP & XQuery
Cutting Edge Data Processing with PHP & XQueryCutting Edge Data Processing with PHP & XQuery
Cutting Edge Data Processing with PHP & XQuery
 
Stop Making The Web Harder Than It Is; Real-world REST, HATEOAS, and Hypermed...
Stop Making The Web Harder Than It Is; Real-world REST, HATEOAS, and Hypermed...Stop Making The Web Harder Than It Is; Real-world REST, HATEOAS, and Hypermed...
Stop Making The Web Harder Than It Is; Real-world REST, HATEOAS, and Hypermed...
 
Asset Pipeline
Asset PipelineAsset Pipeline
Asset Pipeline
 
Web Application Development using PHP Chapter 7
Web Application Development using PHP Chapter 7Web Application Development using PHP Chapter 7
Web Application Development using PHP Chapter 7
 

Similar to WordPress Café: Using WordPress as a Framework

How Not to Build a WordPress Plugin
How Not to Build a WordPress PluginHow Not to Build a WordPress Plugin
How Not to Build a WordPress Plugin
Will Norris
 
An Introduction to Tornado
An Introduction to TornadoAn Introduction to Tornado
An Introduction to Tornado
Gavin Roy
 

Similar to WordPress Café: Using WordPress as a Framework (20)

WordPress for developers - phpday 2011
WordPress for developers -  phpday 2011WordPress for developers -  phpday 2011
WordPress for developers - phpday 2011
 
Staying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPStaying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHP
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017
 
Getting Started With WordPress Development
Getting Started With WordPress DevelopmentGetting Started With WordPress Development
Getting Started With WordPress Development
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme Enlightenment
 
CMS content
CMS contentCMS content
CMS content
 
Supercharging WordPress Development - Wordcamp Brighton 2019
Supercharging WordPress Development - Wordcamp Brighton 2019Supercharging WordPress Development - Wordcamp Brighton 2019
Supercharging WordPress Development - Wordcamp Brighton 2019
 
Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010
 
How Not to Build a WordPress Plugin
How Not to Build a WordPress PluginHow Not to Build a WordPress Plugin
How Not to Build a WordPress Plugin
 
Introduction to Plugin Programming, WordCamp Miami 2011
Introduction to Plugin Programming, WordCamp Miami 2011Introduction to Plugin Programming, WordCamp Miami 2011
Introduction to Plugin Programming, WordCamp Miami 2011
 
WordPress Plugins: ur doin it wrong
WordPress Plugins: ur doin it wrongWordPress Plugins: ur doin it wrong
WordPress Plugins: ur doin it wrong
 
An Introduction to Tornado
An Introduction to TornadoAn Introduction to Tornado
An Introduction to Tornado
 
[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018
 
Building Potent WordPress Websites
Building Potent WordPress WebsitesBuilding Potent WordPress Websites
Building Potent WordPress Websites
 
Service discovery and configuration provisioning
Service discovery and configuration provisioningService discovery and configuration provisioning
Service discovery and configuration provisioning
 
Using WordPress as your application stack
Using WordPress as your application stackUsing WordPress as your application stack
Using WordPress as your application stack
 
Php on the desktop and php gtk2
Php on the desktop and php gtk2Php on the desktop and php gtk2
Php on the desktop and php gtk2
 
CodeIgniter & MVC
CodeIgniter & MVCCodeIgniter & MVC
CodeIgniter & MVC
 
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
 
Assetic (Zendcon)
Assetic (Zendcon)Assetic (Zendcon)
Assetic (Zendcon)
 

More from Exove

More from Exove (20)

Data security in the age of GDPR – most common data security problems
Data security in the age of GDPR – most common data security problemsData security in the age of GDPR – most common data security problems
Data security in the age of GDPR – most common data security problems
 
Provisioning infrastructure to AWS using Terraform – Exove
Provisioning infrastructure to AWS using Terraform – ExoveProvisioning infrastructure to AWS using Terraform – Exove
Provisioning infrastructure to AWS using Terraform – Exove
 
Advanced custom fields in Wordpress
Advanced custom fields in WordpressAdvanced custom fields in Wordpress
Advanced custom fields in Wordpress
 
Introduction to Robot Framework – Exove
Introduction to Robot Framework – ExoveIntroduction to Robot Framework – Exove
Introduction to Robot Framework – Exove
 
Jenkins and visual regression – Exove
Jenkins and visual regression – ExoveJenkins and visual regression – Exove
Jenkins and visual regression – Exove
 
Server-side React with Headless CMS – Exove
Server-side React with Headless CMS – ExoveServer-side React with Headless CMS – Exove
Server-side React with Headless CMS – Exove
 
WebSockets in Bravo Dashboard – Exove
WebSockets in Bravo Dashboard – ExoveWebSockets in Bravo Dashboard – Exove
WebSockets in Bravo Dashboard – Exove
 
Diversity in recruitment
Diversity in recruitmentDiversity in recruitment
Diversity in recruitment
 
Saavutettavuus liiketoimintana
Saavutettavuus liiketoimintanaSaavutettavuus liiketoimintana
Saavutettavuus liiketoimintana
 
Saavutettavuus osana Eläkeliiton verkkosivu-uudistusta
Saavutettavuus osana Eläkeliiton verkkosivu-uudistustaSaavutettavuus osana Eläkeliiton verkkosivu-uudistusta
Saavutettavuus osana Eläkeliiton verkkosivu-uudistusta
 
Mitä saavutettavuusdirektiivi pitää sisällään
Mitä saavutettavuusdirektiivi pitää sisälläänMitä saavutettavuusdirektiivi pitää sisällään
Mitä saavutettavuusdirektiivi pitää sisällään
 
Creating Landing Pages for Drupal 8
Creating Landing Pages for Drupal 8Creating Landing Pages for Drupal 8
Creating Landing Pages for Drupal 8
 
GDPR for developers
GDPR for developersGDPR for developers
GDPR for developers
 
Managing Complexity and Privacy Debt with Drupal
Managing Complexity and Privacy Debt with DrupalManaging Complexity and Privacy Debt with Drupal
Managing Complexity and Privacy Debt with Drupal
 
Life with digital services after GDPR
Life with digital services after GDPRLife with digital services after GDPR
Life with digital services after GDPR
 
GDPR - no beginning no end
GDPR - no beginning no endGDPR - no beginning no end
GDPR - no beginning no end
 
Developing truly personalised experiences
Developing truly personalised experiencesDeveloping truly personalised experiences
Developing truly personalised experiences
 
Customer Experience and Personalisation
Customer Experience and PersonalisationCustomer Experience and Personalisation
Customer Experience and Personalisation
 
Adventures In Programmatic Branding – How To Design With Algorithms And How T...
Adventures In Programmatic Branding – How To Design With Algorithms And How T...Adventures In Programmatic Branding – How To Design With Algorithms And How T...
Adventures In Programmatic Branding – How To Design With Algorithms And How T...
 
Dataohjattu asiakaskokemus
Dataohjattu asiakaskokemusDataohjattu asiakaskokemus
Dataohjattu asiakaskokemus
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 

WordPress Café: Using WordPress as a Framework

  • 1. Using WordPress as a framework Marco Martins
  • 3. WordPress is a Content Management System
  • 4. WordPress is a web application framework
  • 6. A web application framework is a software framework that is designed to support the development of dynamic websites, web applications, web services and web resources. The framework aims to alleviate the overhead associated with common activities performed in web development. For example, many frameworks provide libraries for database access, templating frameworks and session management, and they often promote code reuse. from Wikipedia (2015, May 9)
  • 8. • Custom Post Types • Post Meta • Taxonomies • Posts 2 Posts
  • 9. Custom Post Types • Represent the model definition • You can define: • some content fields • visibility (queryable, searchable, show in menus…) • capabilities • hierarchical • rewrites & query vars register_post_type( $post_type, $args );
  • 10. Post Meta • Adds extra fields to the model • Extends the basic model data (title, editor, image…) with extra fields (wysiwyg, embed, field groups, repeatable fields… • Plugins: CMB2, ACF, Custom-Meta-Boxes
  • 11. Taxonomies • Custom taxonomies • Custom taxonomies can be attached to specific content types register_taxonomy( $taxonomy, $object_type, $args );
  • 12. Posts 2 Posts • Plugin • Creates relationships between content types (including users) • Supports cardinality • Allows relationship meta data. • Development of the plugin has not been active • @link https://wordpress.org/plugins/posts-to-posts/
  • 14. Template System • PHP based templating • Templates can be used in plugins • Templates can be loaded programatically
  • 16. WP_Query • Very central class in WordPress • Makes queries by: post type, taxonomy, post meta, date, taxonomy $args = array( 'post_type' => ‘address', 'tax_query' => array( array( 'taxonomy' => ‘type', 'field' => ‘slug', 'terms' => ‘shipping’, ), ), ); $addresses = new WP_Query( $args );
  • 17. $wpdb • Global object to access the database • Escapes data $wpdb->insert( $table, $data, $format ); $wpdb->replace( $table, $data, $format ); $wpdb->update( $table, $data, $where, [$format], [$where_format] ); $wpdb->delete( $table, $where, $where_format = null ); $wpdb->prepare( 'query', value_parameter[, value_parameter] );
  • 18. Users
  • 19. Users • Supports user roles • Supports user capabilities • User information can be extended • New roles and capabilities can be created • Meta capabilities can be created • Authentication method can be changed using hooks
  • 21. Internationalization • Gettext functions (partial list): __( $text, $text_domain ); _e( $text, $text_domain ); _x( $text, $context, $text_domain ); • Loading text domain function demo_load_textdomain() { load_plugin_textdomain( ‘demo', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' ); } add_action( 'plugins_loaded', 'demo_load_textdomain' );
  • 22. Localization • POT files • i18n tools (makepot.php / grunt-pot)
  • 23. Ajax
  • 24. Ajax • Set the Ajax URL function set_ajax_url() { ?> <script type=“text/javascript"> var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>"; </script> <?php } ?>
  • 25. Ajax • Register the request handler function callback() { echo json_encode( array( ‘success’ => true ) ); die(); } add_action( ‘wp_ajax_demo', 'callback' ); add_action( 'wp_ajax_nopriv_demo', 'callback' );
  • 26. Ajax • Making the requests $.getJSON(ajaxurl, {action: ‘demo' }, function( data ){ console.log(‘data’); });
  • 28. HTTP API • Standardizes and abstracts HTTP requests • Supports and abstracts multiple PHP HTTP methods (cURL, file_get_contents) wp_remote_get( $url, $args ); / wp_remote_post( $url, $args ); wp_remote_request( $url, $args ); wp_remote_retrieve_body( $response ); wp_remote_retrieve_headers( $response, $header ); wp_remote_retrieve_response_code( $response );
  • 30. WP REST API • Provides a HTTP REST API to interact with WordPress • Posts, Post Meta, Users, Media… can be created, read, changed, and deleted • Basic Authentication and Oauth are available • Currently the API is available as a plugin but soon it will be part of the WordPress core • @link http://wp-api.org/ GET /posts GET /posts/<id> *POST /posts *PUT /posts/<id> *POST /posts/<id>/meta
  • 32. Transients API • Core API • Similar to the Options API • Data can be stored in the database, Redis, Memcached… • Can be used on the network level and site level
  • 33. Transients API set_transient( $transient, $value, $expiration_in_seconds ) get_transient( $transient ); delete_transient( $transient ); set_site_transient( $transient, $value, $expiration_in_seconds ); get_site_transient( $transient ); delete_site_transient( $transient );
  • 34. TLC Transients • Soft expiration • Can refresh the cache in the background • https://github.com/markjaquith/WP-TLC- Transients
  • 35. TLC Transients <?php function my_callback_with_param( $param ) { return str_replace( 'foo', $param, wp_remote_retrieve_body( wp_remote_get( 'http://example.com/feed.xml', array( 'timeout' => 30 ) ) ), ); } echo tlc_transient( 'example-feed' ) ->updates_with( 'my_callback_with_param', array( 'bar' ) ) ->expires_in( 300 ) ->background_only() ->get();
  • 36. URLs
  • 37. Rewrite API • Allows you to create new rewrite rules and permalink structures programatically • You can create “pages” without having to create a page in the dashboard. • Uses WP_Rewrite API
  • 38. Rewrite API add_rewrite_tag( '%orders%', '([^&]+)' ); add_rewrite_tag( ‘%order_id%', '([^&]+)' ); add_rewrite_rule( $base . '/orders/([^/]*)/?$', 'index.php?' . $base . '=true&orders=true&order_id=$matches[1]', 'top' );
  • 40. More Tools • WP_Error • Options API • Filesystem API • Settings API • Shortcode API • Input Validation & Output Sanitization • Media Management Functions • Custom Post Statuses • …
  • 42. Advantages • Dashboard • Multisite • Documentation & Community • Plugin repository • Hooks (actions & filters) in both core and plugins • Automatic updates
  • 43. But it’s not all rainbows and butterflies
  • 44. Disadvantages • Lack of a proper template engine • Doesn’t enforce a strict MVC • Performance (you need to load WordPress)
  • 45. Extra • WP_Mock (mocks WordPress for unit tests) • Caching plugins (W3 Total Cache, WP Super Cache) • Plugin frameworks • WPlinth