Date post: | 08-May-2015 |
Category: |
Technology |
Upload: | wordcamp-sydney |
View: | 1,224 times |
Download: | 0 times |
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://twitter.com/charlyjl
http://linkedin.com/in/charlyleetham
http://www.youtube.com/user/AskCharlyLeetham
charlyjl
http://facebook.com/charlyleetham
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