stop hacking wordpress, start working with it - charly leetham - wordcamp sydney 2012

25
Stop Hacking Wordpress Start Working With It! Charly Leetham www.AskCharlyLeetham.com

Upload: wordcamp-sydney

Post on 08-May-2015

1.224 views

Category:

Technology


0 download

DESCRIPTION

Many developers know how to write code in php but that doesn't mean they can write code for Wordpress. When developing sites for clients in Wordpress, it sometimes preferably to write a custom function that will simplify data input, management and display. The Wordpress coding framework is continually evolving and provides a number of hooks (through actions & filters) to allow developers to quickly and easily grab functions to create world class websites. Charly will speak about the power of hooks and filters and explain some of the most common ones to use if you want to write your own custom functions, to make the management of your wordpress websites a breeze.

TRANSCRIPT

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

Stop Hacking Wordpress

Start Working With It!

Charly Leetham

www.AskCharlyLeetham.com

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

Share This...

@charlyjl

charlyleetham

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

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

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

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...

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

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

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

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...

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

• 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!

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

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.

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

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

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

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.

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

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

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

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.

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

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

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

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.

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

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

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

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

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

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()

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

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/

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

Any Questions?

http://facebook.com/askcharlyleetham

http://twitter.com/charlyjl

http://linkedin.com/in/charlyleetham

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

charlyjl

[email protected]

http://facebook.com/charlyleetham

www.AskCharlyLeetham.com

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

CODE EXAMPLES...

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

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);

?>

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

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);

?>

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

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);

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

Filter Example

$var1 = ‘My Title’;

$var2 = ‘>>>>’;

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

echo $var1;

Will return:

>>>> My Title

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

Filter Example

$var1 = ‘My Title’;

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

echo $var1;

Will return:

This is My Title