SlideShare une entreprise Scribd logo
1  sur  25
Télécharger pour lire hors ligne
Stop Hacking Wordpress

   Start Working With It!
         Charly Leetham
    www.AskCharlyLeetham.com
Share This...


    @charlyjl



    charlyleetham
What will we cover

•   What is the Wordpress API
•   What are Filters, Actions & Hooks
•   Creating & Calling Actions
•   Actions Example
•   Creating & Applying Filters
•   Filters Example
•   Useful Resources
The API...

• Wordpress API is designed for anyone to
  extend the base WP Functionality
• Create Plugins or Themes
• Integrate with 3rd Party Apps – data in / out
• Covers all the functions available to us
• Today, looking at a small part of the API,
  Filters & Actions...
  Generically known as Hooks...
Filters...

• Modify content.... That’s it!
• For example:
  – Change post / page content
  – Change widget titles
  – RSS Feeds
  – Image details
• Used by plugins to allow content to be
  modified without changing core code
Actions

• Do something ... when called...
• Allow the ‘execution’ of functions at fixed
  points in the code
• For example
  – After Login
  – After Logout
  – When a widget is displayed
  – When the theme is setup...
• Filters & Actions are basically the same
  thing...
• Both use functions that are ‘hooked’ into
  the WP core functions
• Functions we write can use / provide
  actions and filters as well!
Calling Actions...

• Actions are called using do_action ie:
  do_action (‘mypluginactions’,$var1,$var2);

This simply “tells” wordpress to find all functions
that are ‘hooked’ to this action and run them.

It also specifies any arguments that may be
passed to the function – in this case two: $var1,
$var2 – or there may be none.
Anatomy of an Action

• Hooking the action is done using
  add_action($tag,$function,$priority,$args)

$tag – is the action you want to ‘hook to’
$function – the function you want to ‘hook’.
$priority – the order. 10 is default, 1 is the
highest.
$args – the number of arguments to pass to
the action
Adding Actions

<?php
function myfunction($var1=null, $var2=null) {
      echo $var1.’<br>’;
      echo $var2,’<br>’;
}
add_action(‘mypluginactions’,’myfunction’,10,2);
?>
This will run ‘myfunction’, when the action
‘mypluginactions’ is called and pass 2 arguments.
Actions: Some Uses

• Loading Scripts & StyleSheets
• Recording user access (login & logout)
• Building Child Themes
  – Adding / removing functionality
     • Author Bio on posts
     • “Share This”
• Adding / Saving Meta Data for Posts
• Processing Ajax Requests
Hooking filters

• apply_filters actually calls the filter function
  to APPLY it.
  $var1 = apply_filters(‘mypluginfilter’,$var1,$var2);
• NOTE:
  – $var1 MUST be specified and be initialised –
    i.e $var1 must exist when applying the filter
  – apply_filters returns a value, so assign the
    result to a variable.
Anatomy of Filter

• Creating the filter is done using
  add_filter($tag,$function,$priority,$args)

$tag – is the filter you want to ‘hook to’
$function – the function you want to ‘hook’.
$priority – the order. 10 is default, 1 is the
highest.
$args – the number of arguments to pass to
the filter
Example Filter function
<?php
function myfilter($var1, $var2=null) {
        if ($var2==null) {
                 $var1 = ‘This is ‘.$var1;
        } else {
                 $var1 =$var2.$var1;
        }
        return $var1;
}
add_filter(‘mypluginfilter’,’myfilter’,10,2)
?>
This will run ‘myfilter’, when the filter ‘mypluginfilter’ is applied,
pass 2 arguments & return $var1 to be displayed / used.
Filters: Some Uses

• Modifying ‘the content’
  – Remove shortcodes
  – Strip tags / html
  – Add elements
• Add custom classes to the Body and Post tags
• Processing Shortcodes in Widgets
  – add_filter(‘widget_text’,’do_shortcode’);
• Changing Widget Titles
• Adding functions to WP Nav Menu’s
HINT

  You can add actions and filters to your
   plugins to make them more extendable
Some examples:
• WooCommerce
• WP E Commerce
• Gravity Forms
• Duplicate Post
Other stuff to consider
• Namespace
  – Create unique functions name! Use a prefix or suffix
    or, better yet, use a CLASS!
• Only load functions when needed
  – Become familiar with conditional functions to keep
    your load time tight
• Security!
  – Make use of Wordpress & PHP functions for security:
     • esc_html(), esc_attr(), esc_js(), esc_textarea() esc_url(),
       wp_nonce_field(), wp_verify_nonce()
Useful Resources

• Wordpress API
  http://codex.wordpress.org/Plugin_API
• Wordpress Action Reference
 http://codex.wordpress.org/Plugin_API/Action_Reference
• Wordpress Filter Reference
 http://codex.wordpress.org/Plugin_API/Filter_Reference
• PHP Xref for Wordpress
  http://phpxref.com/xref/wordpress/
Any Questions?

http://facebook.com/askcharlyleetham

http://facebook.com/charlyleetham

http://twitter.com/charlyjl

http://linkedin.com/in/charlyleetham

http://www.youtube.com/user/AskCharlyLeetham

charlyjl

tickets@askcharlyleetham.com

       www.AskCharlyLeetham.com
CODE EXAMPLES...
Loading Scripts

• Enqueue Scripts (wp_enqueue_scripts)
Wrong:
<script type=“text/javascript” src=http://mydomain.com/path/script.js>

Right:
<?php
function add_my_script() {
if (!ismypage) { return; }
         $src = get_stylesheet_directory_uri().’/path/myscript.js’;
         wp_enqueue_script(‘myscript‘,$src,array(‘jquery’),’1.0’,true );
}
add_action('wp_enqueue_scripts', ‘add_my_script‘,10);
?>
Loading StyleSheets

• Enqueue Styles
Wrong:
<link type=“text/css” href=“http://mydomain.com/path/style.css”>
Right:
<?php
function add_my_style() {
If (!ismypage) { return; }
         $src = get_stylesheet_directory_uri().’/path/style.css’;
         $dep = get_stylesheet_directory_uri().’/path/layout.css’;
         wp_enqueue_style(‘mystyle‘,$src,$dep,’1.0’,’screen’ );
}
add_action('wp_enqueue_scripts', ‘add_my_style‘,10);
?>
Recording user access

• wp_login
<?php
function my_login_function($login) {
        $user = get_userdatabylogin($login);
        $userid=$user->ID;
        //code to record login details
}
add_action('wp_login', ‘my_login_function’, 10,1);
Filter Example

$var1 = ‘My Title’;
$var2 = ‘>>>>’;
$var1 = apply_filters(‘mypluginfilter’,$var1,$var2);
echo $var1;

Will return:
>>>> My Title
Filter Example

$var1 = ‘My Title’;
$var1 = apply_filters(‘mypluginfilter’,$var1,$var2);
echo $var1;

Will return:
This is My Title

Contenu connexe

Tendances

Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesDjangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesNina Zakharenko
 
Jumpstart Your Development with ZopeSkel
Jumpstart Your Development with ZopeSkelJumpstart Your Development with ZopeSkel
Jumpstart Your Development with ZopeSkelCristopher Ewing
 
Caldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW WorkshopCaldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW WorkshopCalderaLearn
 
Introduction to plugin development
Introduction to plugin developmentIntroduction to plugin development
Introduction to plugin developmentCaldera Labs
 
Integration patterns in AEM 6
Integration patterns in AEM 6Integration patterns in AEM 6
Integration patterns in AEM 6Yuval Ararat
 
Transforming WordPress Search and Query Performance with Elasticsearch
Transforming WordPress Search and Query Performance with Elasticsearch Transforming WordPress Search and Query Performance with Elasticsearch
Transforming WordPress Search and Query Performance with Elasticsearch Taylor Lovett
 
PHP 8.1 - What's new and changed
PHP 8.1 - What's new and changedPHP 8.1 - What's new and changed
PHP 8.1 - What's new and changedAyesh Karunaratne
 
Bullet: The Functional PHP Micro-Framework
Bullet: The Functional PHP Micro-FrameworkBullet: The Functional PHP Micro-Framework
Bullet: The Functional PHP Micro-FrameworkVance Lucas
 
Object Oriented PHP - PART-2
Object Oriented PHP - PART-2Object Oriented PHP - PART-2
Object Oriented PHP - PART-2Jalpesh Vasa
 
Twig: Friendly Curly Braces Invade Your Templates!
Twig: Friendly Curly Braces Invade Your Templates!Twig: Friendly Curly Braces Invade Your Templates!
Twig: Friendly Curly Braces Invade Your Templates!Ryan Weaver
 
Introduction to Jquery
Introduction to Jquery Introduction to Jquery
Introduction to Jquery Tajendar Arora
 
Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3Kris Wallsmith
 
Introduction to Selenium and Ruby
Introduction to Selenium and RubyIntroduction to Selenium and Ruby
Introduction to Selenium and RubyYnon Perek
 
Testing Javascript with Jasmine
Testing Javascript with JasmineTesting Javascript with Jasmine
Testing Javascript with JasmineTim Tyrrell
 
Introduction to ElasticSearch
Introduction to ElasticSearchIntroduction to ElasticSearch
Introduction to ElasticSearchSimobo
 
Angular Intermediate
Angular IntermediateAngular Intermediate
Angular IntermediateLinkMe Srl
 

Tendances (20)

Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesDjangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
 
Jumpstart Your Development with ZopeSkel
Jumpstart Your Development with ZopeSkelJumpstart Your Development with ZopeSkel
Jumpstart Your Development with ZopeSkel
 
Caldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW WorkshopCaldera Learn - LoopConf WP API + Angular FTW Workshop
Caldera Learn - LoopConf WP API + Angular FTW Workshop
 
Introduction to plugin development
Introduction to plugin developmentIntroduction to plugin development
Introduction to plugin development
 
Integration patterns in AEM 6
Integration patterns in AEM 6Integration patterns in AEM 6
Integration patterns in AEM 6
 
Transforming WordPress Search and Query Performance with Elasticsearch
Transforming WordPress Search and Query Performance with Elasticsearch Transforming WordPress Search and Query Performance with Elasticsearch
Transforming WordPress Search and Query Performance with Elasticsearch
 
PHP 8.1 - What's new and changed
PHP 8.1 - What's new and changedPHP 8.1 - What's new and changed
PHP 8.1 - What's new and changed
 
Bullet: The Functional PHP Micro-Framework
Bullet: The Functional PHP Micro-FrameworkBullet: The Functional PHP Micro-Framework
Bullet: The Functional PHP Micro-Framework
 
Object Oriented PHP - PART-2
Object Oriented PHP - PART-2Object Oriented PHP - PART-2
Object Oriented PHP - PART-2
 
slingmodels
slingmodelsslingmodels
slingmodels
 
Symfony 2
Symfony 2Symfony 2
Symfony 2
 
Twig: Friendly Curly Braces Invade Your Templates!
Twig: Friendly Curly Braces Invade Your Templates!Twig: Friendly Curly Braces Invade Your Templates!
Twig: Friendly Curly Braces Invade Your Templates!
 
Introduction to Jquery
Introduction to Jquery Introduction to Jquery
Introduction to Jquery
 
Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3
 
Introduction to Selenium and Ruby
Introduction to Selenium and RubyIntroduction to Selenium and Ruby
Introduction to Selenium and Ruby
 
Ampersandjs
AmpersandjsAmpersandjs
Ampersandjs
 
jQuery Intro
jQuery IntrojQuery Intro
jQuery Intro
 
Testing Javascript with Jasmine
Testing Javascript with JasmineTesting Javascript with Jasmine
Testing Javascript with Jasmine
 
Introduction to ElasticSearch
Introduction to ElasticSearchIntroduction to ElasticSearch
Introduction to ElasticSearch
 
Angular Intermediate
Angular IntermediateAngular Intermediate
Angular Intermediate
 

Similaire à Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Sydney 2012

10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)arcware
 
Intro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentIntro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentBrad Williams
 
Creating Your First WordPress Plugin
Creating Your First WordPress PluginCreating Your First WordPress Plugin
Creating Your First WordPress PluginBrad Williams
 
How To Write a WordPress Plugin
How To Write a WordPress PluginHow To Write a WordPress Plugin
How To Write a WordPress PluginAndy Stratton
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme EnlightenmentAmanda Giles
 
Webinar: Zend framework Getting to grips (ZF1)
Webinar: Zend framework Getting to grips (ZF1)Webinar: Zend framework Getting to grips (ZF1)
Webinar: Zend framework Getting to grips (ZF1)Ryan Mauger
 
Introduction to WordPress Development - Hooks
Introduction to WordPress Development - HooksIntroduction to WordPress Development - Hooks
Introduction to WordPress Development - HooksEdmund Chan
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017Amanda Giles
 
Zend framework: Getting to grips (ZF1)
Zend framework: Getting to grips (ZF1)Zend framework: Getting to grips (ZF1)
Zend framework: Getting to grips (ZF1)Ryan Mauger
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...Fabio Franzini
 
WordPress plugin #2
WordPress plugin #2WordPress plugin #2
WordPress plugin #2giwoolee
 
WordPress Structure and Best Practices
WordPress Structure and Best PracticesWordPress Structure and Best Practices
WordPress Structure and Best Practicesmarkparolisi
 
WordPress Hooks Action & Filters
WordPress Hooks Action & FiltersWordPress Hooks Action & Filters
WordPress Hooks Action & FiltersNirav Mehta
 
Getting started with WordPress development
Getting started with WordPress developmentGetting started with WordPress development
Getting started with WordPress developmentSteve Mortiboy
 
BITM3730 10-4.pptx
BITM3730 10-4.pptxBITM3730 10-4.pptx
BITM3730 10-4.pptxMattMarino13
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress pluginAnthony Montalbano
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkBo-Yi Wu
 
PHP-03-Functions.ppt
PHP-03-Functions.pptPHP-03-Functions.ppt
PHP-03-Functions.pptJamers2
 

Similaire à Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Sydney 2012 (20)

10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
 
Intro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentIntro to WordPress Plugin Development
Intro to WordPress Plugin Development
 
Creating Your First WordPress Plugin
Creating Your First WordPress PluginCreating Your First WordPress Plugin
Creating Your First WordPress Plugin
 
How To Write a WordPress Plugin
How To Write a WordPress PluginHow To Write a WordPress Plugin
How To Write a WordPress Plugin
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme Enlightenment
 
Webinar: Zend framework Getting to grips (ZF1)
Webinar: Zend framework Getting to grips (ZF1)Webinar: Zend framework Getting to grips (ZF1)
Webinar: Zend framework Getting to grips (ZF1)
 
Introduction to WordPress Development - Hooks
Introduction to WordPress Development - HooksIntroduction to WordPress Development - Hooks
Introduction to WordPress Development - Hooks
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017
 
Zend framework: Getting to grips (ZF1)
Zend framework: Getting to grips (ZF1)Zend framework: Getting to grips (ZF1)
Zend framework: Getting to grips (ZF1)
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
WordPress plugin #2
WordPress plugin #2WordPress plugin #2
WordPress plugin #2
 
WordPress Structure and Best Practices
WordPress Structure and Best PracticesWordPress Structure and Best Practices
WordPress Structure and Best Practices
 
Seven deadly theming sins
Seven deadly theming sinsSeven deadly theming sins
Seven deadly theming sins
 
WordPress Hooks Action & Filters
WordPress Hooks Action & FiltersWordPress Hooks Action & Filters
WordPress Hooks Action & Filters
 
Getting started with WordPress development
Getting started with WordPress developmentGetting started with WordPress development
Getting started with WordPress development
 
BITM3730 10-4.pptx
BITM3730 10-4.pptxBITM3730 10-4.pptx
BITM3730 10-4.pptx
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress plugin
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC Framework
 
PHP-03-Functions.ppt
PHP-03-Functions.pptPHP-03-Functions.ppt
PHP-03-Functions.ppt
 
PHP-03-Functions.ppt
PHP-03-Functions.pptPHP-03-Functions.ppt
PHP-03-Functions.ppt
 

Plus de WordCamp Sydney

How To Get Paid What You’re Worth - Troy Dean - WordCamp Sydney 2012
How To Get Paid What You’re Worth - Troy Dean - WordCamp Sydney 2012How To Get Paid What You’re Worth - Troy Dean - WordCamp Sydney 2012
How To Get Paid What You’re Worth - Troy Dean - WordCamp Sydney 2012WordCamp Sydney
 
Not Just another WordPress Site Design - Phil Peet - WordCamp Sydney 2012
Not Just another WordPress Site Design - Phil Peet - WordCamp Sydney 2012Not Just another WordPress Site Design - Phil Peet - WordCamp Sydney 2012
Not Just another WordPress Site Design - Phil Peet - WordCamp Sydney 2012WordCamp Sydney
 
What Multisite can do for You - Anthony Cole - WordCamp Sydney 2012
What Multisite can do for You - Anthony Cole - WordCamp Sydney 2012What Multisite can do for You - Anthony Cole - WordCamp Sydney 2012
What Multisite can do for You - Anthony Cole - WordCamp Sydney 2012WordCamp Sydney
 
TurboPress: The High Performance Guide to WordPress - Jeff Waugh - WordCamp S...
TurboPress: The High Performance Guide to WordPress - Jeff Waugh - WordCamp S...TurboPress: The High Performance Guide to WordPress - Jeff Waugh - WordCamp S...
TurboPress: The High Performance Guide to WordPress - Jeff Waugh - WordCamp S...WordCamp Sydney
 
Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney...
Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney...Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney...
Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney...WordCamp Sydney
 
Child Theming WordPress - Chris Aprea - WordCamp Sydney 2012
Child Theming WordPress - Chris Aprea - WordCamp Sydney 2012Child Theming WordPress - Chris Aprea - WordCamp Sydney 2012
Child Theming WordPress - Chris Aprea - WordCamp Sydney 2012WordCamp Sydney
 
Responsive WordPress - Jordan Gillman - WordCamp Sydney 2012
Responsive WordPress - Jordan Gillman - WordCamp Sydney 2012Responsive WordPress - Jordan Gillman - WordCamp Sydney 2012
Responsive WordPress - Jordan Gillman - WordCamp Sydney 2012WordCamp Sydney
 
The Power of Your Story Through WordPress and Social Media - Kimanzi Constabl...
The Power of Your Story Through WordPress and Social Media - Kimanzi Constabl...The Power of Your Story Through WordPress and Social Media - Kimanzi Constabl...
The Power of Your Story Through WordPress and Social Media - Kimanzi Constabl...WordCamp Sydney
 
Siloing your Site for SEO Success - Stephen Cronin - WordCamp Sydney 2012
Siloing your Site for SEO Success - Stephen Cronin - WordCamp Sydney 2012Siloing your Site for SEO Success - Stephen Cronin - WordCamp Sydney 2012
Siloing your Site for SEO Success - Stephen Cronin - WordCamp Sydney 2012WordCamp Sydney
 
The Plugin Spectactular - Tony Cosentino - WordCamp Sydney 2012
The Plugin Spectactular - Tony Cosentino - WordCamp Sydney 2012The Plugin Spectactular - Tony Cosentino - WordCamp Sydney 2012
The Plugin Spectactular - Tony Cosentino - WordCamp Sydney 2012WordCamp Sydney
 
Optimising SEO for WordPress - Lisa Davis - WordCamp Sydney 2012
Optimising SEO for WordPress - Lisa Davis - WordCamp Sydney 2012Optimising SEO for WordPress - Lisa Davis - WordCamp Sydney 2012
Optimising SEO for WordPress - Lisa Davis - WordCamp Sydney 2012WordCamp Sydney
 
Word to the Future - Brent Shepherd - WordCamp Sydney 2012
Word to the Future - Brent Shepherd - WordCamp Sydney 2012Word to the Future - Brent Shepherd - WordCamp Sydney 2012
Word to the Future - Brent Shepherd - WordCamp Sydney 2012WordCamp Sydney
 
Securing your WordPress Website - Vlad Lasky - WordCamp Sydney 2012
Securing your WordPress Website - Vlad Lasky - WordCamp Sydney 2012Securing your WordPress Website - Vlad Lasky - WordCamp Sydney 2012
Securing your WordPress Website - Vlad Lasky - WordCamp Sydney 2012WordCamp Sydney
 
Inclusive Design Principles for WordPress - Joe Ortenzi - WordCamp Sydney
Inclusive Design Principles for WordPress - Joe Ortenzi - WordCamp SydneyInclusive Design Principles for WordPress - Joe Ortenzi - WordCamp Sydney
Inclusive Design Principles for WordPress - Joe Ortenzi - WordCamp SydneyWordCamp Sydney
 
There's More than 1 Way to Skin a WordPress Theme - Lachlan MacPherson - Word...
There's More than 1 Way to Skin a WordPress Theme - Lachlan MacPherson - Word...There's More than 1 Way to Skin a WordPress Theme - Lachlan MacPherson - Word...
There's More than 1 Way to Skin a WordPress Theme - Lachlan MacPherson - Word...WordCamp Sydney
 
WordPress for Noobs - Wil Brown - WordCamp Sydney 2012
WordPress for Noobs - Wil Brown - WordCamp Sydney 2012WordPress for Noobs - Wil Brown - WordCamp Sydney 2012
WordPress for Noobs - Wil Brown - WordCamp Sydney 2012WordCamp Sydney
 
Getting to Grips with Firebug - Anthony Hortin - WordCamp Sydney
Getting to Grips with Firebug - Anthony Hortin - WordCamp SydneyGetting to Grips with Firebug - Anthony Hortin - WordCamp Sydney
Getting to Grips with Firebug - Anthony Hortin - WordCamp SydneyWordCamp Sydney
 

Plus de WordCamp Sydney (17)

How To Get Paid What You’re Worth - Troy Dean - WordCamp Sydney 2012
How To Get Paid What You’re Worth - Troy Dean - WordCamp Sydney 2012How To Get Paid What You’re Worth - Troy Dean - WordCamp Sydney 2012
How To Get Paid What You’re Worth - Troy Dean - WordCamp Sydney 2012
 
Not Just another WordPress Site Design - Phil Peet - WordCamp Sydney 2012
Not Just another WordPress Site Design - Phil Peet - WordCamp Sydney 2012Not Just another WordPress Site Design - Phil Peet - WordCamp Sydney 2012
Not Just another WordPress Site Design - Phil Peet - WordCamp Sydney 2012
 
What Multisite can do for You - Anthony Cole - WordCamp Sydney 2012
What Multisite can do for You - Anthony Cole - WordCamp Sydney 2012What Multisite can do for You - Anthony Cole - WordCamp Sydney 2012
What Multisite can do for You - Anthony Cole - WordCamp Sydney 2012
 
TurboPress: The High Performance Guide to WordPress - Jeff Waugh - WordCamp S...
TurboPress: The High Performance Guide to WordPress - Jeff Waugh - WordCamp S...TurboPress: The High Performance Guide to WordPress - Jeff Waugh - WordCamp S...
TurboPress: The High Performance Guide to WordPress - Jeff Waugh - WordCamp S...
 
Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney...
Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney...Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney...
Demystifying Custom Post Types and Taxonomies - Tracey Kemp - WordCamp Sydney...
 
Child Theming WordPress - Chris Aprea - WordCamp Sydney 2012
Child Theming WordPress - Chris Aprea - WordCamp Sydney 2012Child Theming WordPress - Chris Aprea - WordCamp Sydney 2012
Child Theming WordPress - Chris Aprea - WordCamp Sydney 2012
 
Responsive WordPress - Jordan Gillman - WordCamp Sydney 2012
Responsive WordPress - Jordan Gillman - WordCamp Sydney 2012Responsive WordPress - Jordan Gillman - WordCamp Sydney 2012
Responsive WordPress - Jordan Gillman - WordCamp Sydney 2012
 
The Power of Your Story Through WordPress and Social Media - Kimanzi Constabl...
The Power of Your Story Through WordPress and Social Media - Kimanzi Constabl...The Power of Your Story Through WordPress and Social Media - Kimanzi Constabl...
The Power of Your Story Through WordPress and Social Media - Kimanzi Constabl...
 
Siloing your Site for SEO Success - Stephen Cronin - WordCamp Sydney 2012
Siloing your Site for SEO Success - Stephen Cronin - WordCamp Sydney 2012Siloing your Site for SEO Success - Stephen Cronin - WordCamp Sydney 2012
Siloing your Site for SEO Success - Stephen Cronin - WordCamp Sydney 2012
 
The Plugin Spectactular - Tony Cosentino - WordCamp Sydney 2012
The Plugin Spectactular - Tony Cosentino - WordCamp Sydney 2012The Plugin Spectactular - Tony Cosentino - WordCamp Sydney 2012
The Plugin Spectactular - Tony Cosentino - WordCamp Sydney 2012
 
Optimising SEO for WordPress - Lisa Davis - WordCamp Sydney 2012
Optimising SEO for WordPress - Lisa Davis - WordCamp Sydney 2012Optimising SEO for WordPress - Lisa Davis - WordCamp Sydney 2012
Optimising SEO for WordPress - Lisa Davis - WordCamp Sydney 2012
 
Word to the Future - Brent Shepherd - WordCamp Sydney 2012
Word to the Future - Brent Shepherd - WordCamp Sydney 2012Word to the Future - Brent Shepherd - WordCamp Sydney 2012
Word to the Future - Brent Shepherd - WordCamp Sydney 2012
 
Securing your WordPress Website - Vlad Lasky - WordCamp Sydney 2012
Securing your WordPress Website - Vlad Lasky - WordCamp Sydney 2012Securing your WordPress Website - Vlad Lasky - WordCamp Sydney 2012
Securing your WordPress Website - Vlad Lasky - WordCamp Sydney 2012
 
Inclusive Design Principles for WordPress - Joe Ortenzi - WordCamp Sydney
Inclusive Design Principles for WordPress - Joe Ortenzi - WordCamp SydneyInclusive Design Principles for WordPress - Joe Ortenzi - WordCamp Sydney
Inclusive Design Principles for WordPress - Joe Ortenzi - WordCamp Sydney
 
There's More than 1 Way to Skin a WordPress Theme - Lachlan MacPherson - Word...
There's More than 1 Way to Skin a WordPress Theme - Lachlan MacPherson - Word...There's More than 1 Way to Skin a WordPress Theme - Lachlan MacPherson - Word...
There's More than 1 Way to Skin a WordPress Theme - Lachlan MacPherson - Word...
 
WordPress for Noobs - Wil Brown - WordCamp Sydney 2012
WordPress for Noobs - Wil Brown - WordCamp Sydney 2012WordPress for Noobs - Wil Brown - WordCamp Sydney 2012
WordPress for Noobs - Wil Brown - WordCamp Sydney 2012
 
Getting to Grips with Firebug - Anthony Hortin - WordCamp Sydney
Getting to Grips with Firebug - Anthony Hortin - WordCamp SydneyGetting to Grips with Firebug - Anthony Hortin - WordCamp Sydney
Getting to Grips with Firebug - Anthony Hortin - WordCamp Sydney
 

Dernier

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
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 WorkerThousandEyes
 
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 RobisonAnna Loughnan Colquhoun
 
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...apidays
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
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)wesley chun
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
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?Igalia
 

Dernier (20)

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
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
 
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
 
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...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
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)
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
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?
 

Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Sydney 2012

  • 1. Stop Hacking Wordpress Start Working With It! Charly Leetham www.AskCharlyLeetham.com
  • 2. Share This... @charlyjl charlyleetham
  • 3. What will we cover • What is the Wordpress API • What are Filters, Actions & Hooks • Creating & Calling Actions • Actions Example • Creating & Applying Filters • Filters Example • Useful Resources
  • 4. The API... • Wordpress API is designed for anyone to extend the base WP Functionality • Create Plugins or Themes • Integrate with 3rd Party Apps – data in / out • Covers all the functions available to us • Today, looking at a small part of the API, Filters & Actions... Generically known as Hooks...
  • 5. Filters... • Modify content.... That’s it! • For example: – Change post / page content – Change widget titles – RSS Feeds – Image details • Used by plugins to allow content to be modified without changing core code
  • 6. Actions • Do something ... when called... • Allow the ‘execution’ of functions at fixed points in the code • For example – After Login – After Logout – When a widget is displayed – When the theme is setup...
  • 7. • Filters & Actions are basically the same thing... • Both use functions that are ‘hooked’ into the WP core functions • Functions we write can use / provide actions and filters as well!
  • 8. Calling Actions... • Actions are called using do_action ie: do_action (‘mypluginactions’,$var1,$var2); This simply “tells” wordpress to find all functions that are ‘hooked’ to this action and run them. It also specifies any arguments that may be passed to the function – in this case two: $var1, $var2 – or there may be none.
  • 9. Anatomy of an Action • Hooking the action is done using add_action($tag,$function,$priority,$args) $tag – is the action you want to ‘hook to’ $function – the function you want to ‘hook’. $priority – the order. 10 is default, 1 is the highest. $args – the number of arguments to pass to the action
  • 10. Adding Actions <?php function myfunction($var1=null, $var2=null) { echo $var1.’<br>’; echo $var2,’<br>’; } add_action(‘mypluginactions’,’myfunction’,10,2); ?> This will run ‘myfunction’, when the action ‘mypluginactions’ is called and pass 2 arguments.
  • 11. Actions: Some Uses • Loading Scripts & StyleSheets • Recording user access (login & logout) • Building Child Themes – Adding / removing functionality • Author Bio on posts • “Share This” • Adding / Saving Meta Data for Posts • Processing Ajax Requests
  • 12. Hooking filters • apply_filters actually calls the filter function to APPLY it. $var1 = apply_filters(‘mypluginfilter’,$var1,$var2); • NOTE: – $var1 MUST be specified and be initialised – i.e $var1 must exist when applying the filter – apply_filters returns a value, so assign the result to a variable.
  • 13. Anatomy of Filter • Creating the filter is done using add_filter($tag,$function,$priority,$args) $tag – is the filter you want to ‘hook to’ $function – the function you want to ‘hook’. $priority – the order. 10 is default, 1 is the highest. $args – the number of arguments to pass to the filter
  • 14. Example Filter function <?php function myfilter($var1, $var2=null) { if ($var2==null) { $var1 = ‘This is ‘.$var1; } else { $var1 =$var2.$var1; } return $var1; } add_filter(‘mypluginfilter’,’myfilter’,10,2) ?> This will run ‘myfilter’, when the filter ‘mypluginfilter’ is applied, pass 2 arguments & return $var1 to be displayed / used.
  • 15. Filters: Some Uses • Modifying ‘the content’ – Remove shortcodes – Strip tags / html – Add elements • Add custom classes to the Body and Post tags • Processing Shortcodes in Widgets – add_filter(‘widget_text’,’do_shortcode’); • Changing Widget Titles • Adding functions to WP Nav Menu’s
  • 16. HINT You can add actions and filters to your plugins to make them more extendable Some examples: • WooCommerce • WP E Commerce • Gravity Forms • Duplicate Post
  • 17. Other stuff to consider • Namespace – Create unique functions name! Use a prefix or suffix or, better yet, use a CLASS! • Only load functions when needed – Become familiar with conditional functions to keep your load time tight • Security! – Make use of Wordpress & PHP functions for security: • esc_html(), esc_attr(), esc_js(), esc_textarea() esc_url(), wp_nonce_field(), wp_verify_nonce()
  • 18. Useful Resources • Wordpress API http://codex.wordpress.org/Plugin_API • Wordpress Action Reference http://codex.wordpress.org/Plugin_API/Action_Reference • Wordpress Filter Reference http://codex.wordpress.org/Plugin_API/Filter_Reference • PHP Xref for Wordpress http://phpxref.com/xref/wordpress/
  • 21. Loading Scripts • Enqueue Scripts (wp_enqueue_scripts) Wrong: <script type=“text/javascript” src=http://mydomain.com/path/script.js> Right: <?php function add_my_script() { if (!ismypage) { return; } $src = get_stylesheet_directory_uri().’/path/myscript.js’; wp_enqueue_script(‘myscript‘,$src,array(‘jquery’),’1.0’,true ); } add_action('wp_enqueue_scripts', ‘add_my_script‘,10); ?>
  • 22. Loading StyleSheets • Enqueue Styles Wrong: <link type=“text/css” href=“http://mydomain.com/path/style.css”> Right: <?php function add_my_style() { If (!ismypage) { return; } $src = get_stylesheet_directory_uri().’/path/style.css’; $dep = get_stylesheet_directory_uri().’/path/layout.css’; wp_enqueue_style(‘mystyle‘,$src,$dep,’1.0’,’screen’ ); } add_action('wp_enqueue_scripts', ‘add_my_style‘,10); ?>
  • 23. Recording user access • wp_login <?php function my_login_function($login) { $user = get_userdatabylogin($login); $userid=$user->ID; //code to record login details } add_action('wp_login', ‘my_login_function’, 10,1);
  • 24. Filter Example $var1 = ‘My Title’; $var2 = ‘>>>>’; $var1 = apply_filters(‘mypluginfilter’,$var1,$var2); echo $var1; Will return: >>>> My Title
  • 25. Filter Example $var1 = ‘My Title’; $var1 = apply_filters(‘mypluginfilter’,$var1,$var2); echo $var1; Will return: This is My Title