Presentation: Plugging Into Wordpress
-
Upload
matt-harris -
Category
Technology
-
view
2.528 -
download
3
description
Transcript of Presentation: Plugging Into Wordpress
![Page 1: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/1.jpg)
Plugging intoMatt Harris
<head> conference26th October 2008
1
![Page 2: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/2.jpg)
Type
s of
Wor
dpre
ss Wordpress.comHosted blogging solution
Wordpress.orgDownloadable edition for use on your own server
Wordpress MUMulti-user edition of Wordpress.org
2
![Page 3: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/3.jpg)
Wor
dpre
ss.c
omHosted
Free
Maintained and backed up for you
Not hackable or pluggable by you
3Hosted: scalable, Automa1c deals with the spikes you may get
![Page 4: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/4.jpg)
Wor
dpre
ss.o
rgCustomisable
Self-hosted
Complete code control
Maintenance, backup and reliability are your responsibility
4Customisable: hackable, expandable. Add your own features
![Page 5: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/5.jpg)
Wor
dpre
ss M
UAll of Wordpress.org pros and cons
Hosting of multiple blogs under one install
Bespoke permission settings per blog
Your own Wordpress.com
5Basically this is the meat behind Wordpress.comGreat for Uni’s, EducaEonal places, newspapers, blog networks etc
![Page 6: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/6.jpg)
User Interface
6
![Page 7: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/7.jpg)
User Interface
7
![Page 8: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/8.jpg)
User Interface
8
![Page 9: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/9.jpg)
User Interface
9
![Page 10: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/10.jpg)
Admin Interface
10
![Page 11: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/11.jpg)
Admin Interface
11
![Page 12: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/12.jpg)
Admin Interface
12
![Page 13: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/13.jpg)
Hooks and Filters
add_action($tag, $function, $priority, $accepted_args)
add_filter($tag, $function, $priority, $accepted_args)
http://www.flickr.com/photos/gaetanlee/2906941718/
add_shortcode($tag, $function)
13Don’t hack at the core code – makes it difficult to update the your installaEonUse hooks and filters to ‘inject’ an event into the page processing
Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen
Hooks aOach custom funcEons onto specific acEonshOp://codex.wordpress.org/Plugin_API/AcEon_ReferencehOp://codex.wordpress.org/Plugin_API/Filter_ReferencehOp://codex.wordpress.org/Shortcode_API
![Page 14: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/14.jpg)
Where to put your code
PluginsInside it’s own file
Theme folder in the file
functions.php
14Plugins advantage – easily switch off and onableFuncEons – will always run – best if required as part of your theme (not covered today but example would be to change something a theme relies on – maybe the movement of a login buOon)
![Page 15: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/15.jpg)
Simple Filter
<?php
function changeWord($content) { $search = “wordpress”; $replace = “my blog”; return str_ireplace($search, $replace, $content);}
add_filter(‘the_content’, ‘changeWord’);
?>
http://codex.wordpress.org/Plugin_API/Filter_Reference
15‘the_content” is a built in Wordpress tagStr_ireplace is case insensiEve
To test this included it in funcEons.php
![Page 16: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/16.jpg)
Simple Filter
<?php
function changeWord($content) { $search = “wordpress”; $replace = “my blog”; return str_ireplace($search, $replace, $content);}
add_filter(‘the_content’, ‘changeWord’);
?>
http://codex.wordpress.org/Plugin_API/Filter_Reference
16‘the_content” is a built in Wordpress tagStr_ireplace is case insensiEve
To test this included it in funcEons.php
![Page 17: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/17.jpg)
Simple Filter
Before After
17
![Page 18: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/18.jpg)
Simple Hook
<?php
function addText() { echo “Can you see me?”;}
add_action(’wp_footer', ’addText’);
?>
http://codex.wordpress.org/Plugin_API/Action_Reference
18wp_footer Runs when the template calls the wp_footer funcEon, generally near the boOom of the blog page.
![Page 19: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/19.jpg)
Simple Hook
<?php
function addText() { echo “Can you see me?”;}
add_action(’wp_footer', ’addText’);
?>
http://codex.wordpress.org/Plugin_API/Action_Reference
19wp_footer Runs when the template calls the wp_footer funcEon, generally near the boOom of the blog page.
![Page 20: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/20.jpg)
Simple Hook<?php
function changeTextCol() { echo “ <style type=‘text/css’> body { color: #0ff00f; } </style> “;}
add_action(’wp_head', ’changeTextCol’);
?>
http://codex.wordpress.org/Plugin_API/Action_Reference
20wp_footer Runs when the template calls the wp_footer funcEon, generally near the boOom of the blog page.
![Page 21: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/21.jpg)
Simple Hook
Before After
21
![Page 22: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/22.jpg)
Simple Shortcode<?php
function myH1_shortcode($atts, $content = null) { extract($atts); return ‘ <div class=“heading”> <h1 style=“color:’ . $colour . ‘”>' . $content . '</h1> </div>’;}
add_shortcode(’heading', 'myH1_shortcode');
?>
http://codex.wordpress.org/Shortcode_API
22Shortcodes cannot be nested be default. Must add do_shortcode($content) into your handler to do this
Since WP2.5 (fixed 2.5.1. parsing order)
Note this isn’t really safe, $content should be saniEzed first using (stripslashes(wp_filter_post_kses($content) and colour should be protected with aOribute_escape($colour)
![Page 23: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/23.jpg)
Simple Shortcode<?php
[heading color=“#f00”]This is my heading[/heading]
?>
Output
<div class="heading"> <h1 style="color:#f00">This is my heading</h1></div>
23Shortcodes cannot be nested be default. Must add do_shortcode($content) into your handler to do this
![Page 24: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/24.jpg)
Namespacing
http://www.flickr.com/photos/thost/2244046981/24
![Page 25: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/25.jpg)
Namespacing
<?php
function addText() { echo “Can you see me?”;}
add_action(’wp_footer', ’addText’);
?>
25
![Page 26: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/26.jpg)
Namespacing
<?php
function tmh_addText() { echo “Can you see me?”;}
add_action(’wp_footer', ’tmh_addText’);
?>
26
![Page 27: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/27.jpg)
Widgets
27
![Page 28: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/28.jpg)
Plugin Code Structure<?php
/*Plugin Name: My WidgetPlugin URI: http://themattharris.comDescription: A widget that puts some text in the sidebarAuthor: Matt HarrisVersion: 1.0Author URI: http://themattharris.com*/
?>
28
![Page 29: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/29.jpg)
Widget Code Structure<?php
function tmh_renderWidget($args) { extract($args); ?> <?php echo $before_widget; ?> <?php echo $before_title . wptexturize(’My Widget') . $after_title; ?> <p>My Widget Code</p> <?php echo $after_widget; ?> <?php}
?>
29
![Page 30: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/30.jpg)
Widget Code Structure<?php
function tmh_renderWidget($args) { extract($args); ?> <?php echo $before_widget; ?> <?php echo $before_title . wptexturize(’My Widget') . $after_title; ?> <p>My Widget Code</p> <?php echo $after_widget; ?> <?php}
?>
30
![Page 31: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/31.jpg)
Widget Code Structure<?php
function tmh_renderWidget($args) { extract($args); ?> <?php echo $before_widget; ?> <?php echo $before_title . wptexturize(’My Widget') . $after_title; ?> <p>My Widget Code</p> <?php echo $after_widget; ?> <?php}
?>
31
![Page 32: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/32.jpg)
Widget Code Structure<?php
function tmh_renderWidget($args) { extract($args); ?> <?php echo $before_widget; ?> <?php echo $before_title . wptexturize(‘My Widget’) . $after_title; ?> <p>My Widget Code</p> <?php echo $after_widget; ?> <?php}
?>
32wp_specialchars ‐ Like htmlspecialchars except don't double‐encode HTML enEEes
![Page 33: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/33.jpg)
<?php
function tmh_renderWidget($args) { extract($args); ?> <?php echo $before_widget; ?> <?php echo $before_title . wptexturize(’My Widget') . $after_title; ?> <p>My Widget Code</p> <?php echo $after_widget; ?> <?php}
?>
Widget Code Structure
33
![Page 34: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/34.jpg)
Widget Registration Code<?php
function tmh_widgetInit() { if (function_exists('register_sidebar_widget’)) { register_sidebar_widget('My Widget', 'tmh_renderWidget'); }}
add_action( 'widgets_init', 'tmh_widgetInit');
?>
34
![Page 35: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/35.jpg)
Putting it all together<?php/*Plugin Name: My WidgetPlugin URI: http://themattharris.comDescription: A widget that puts some text in the sidebarAuthor: Matt HarrisVersion: 1.0Author URI: http://themattharris.com*/
function tmh_renderWidget($args) { extract($args); ?> <?php echo $before_widget; ?> <?php echo $before_title . wptexturize('My Widget') . $after_title; ?>
<p>My Widget Code</p>
<?php echo $after_widget; ?> <?php}
function tmh_widgetInit() { if ( function_exists('register_sidebar_widget') ) { register_sidebar_widget('My Widget', 'tmh_renderWidget'); }}
add_action( 'widgets_init', 'tmh_widgetInit');
?>
35
![Page 36: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/36.jpg)
Making it work
36
![Page 37: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/37.jpg)
Making it work
37
![Page 38: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/38.jpg)
Making it work
38
![Page 39: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/39.jpg)
Making it work
39
![Page 40: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/40.jpg)
40
![Page 41: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/41.jpg)
41
![Page 42: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/42.jpg)
Protecting your blog
check_admin_referer($action, [$query_arg]);
Use with
wp_create_nonce($action);
Use this on any forms you have.
42
![Page 43: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/43.jpg)
Protecting your blog
attribute_escape($text); // also tag escape
wp_filter_post_kses($data); // adds slashes
wp_filter_nohtml_kses($data); // adds slashes
Use these when outputting data
43aOribute_escape (used for escaping for HTML aOributes)
Kses checks for allow html (or removes it in nohtml)
![Page 44: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/44.jpg)
Protecting your blog
<?php current_user_can($capability) ?>
http://codex.wordpress.org/Roles_and_Capabilities
<?php if (current_user_can(‘unfiltered_html’)) { $data = Wp_filter_post_kses($data); } else { $data = wp_filter_nohtml_kses($data); }?>
Example
44aOribute_escape (used for escaping for HTML aOributes)
Kses checks for allow html (or removes it in nohtml)
![Page 45: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/45.jpg)
Translation friendly
http://codex.wordpress.org/Translating_WordPress
<?php __($message, $domain); ?>
<?php _e($message, $domain); ?>
<?php _e(“Title:”,’tmh_pluginname’); ?>
Example
45WP uses GNU geOext localizaEon framework
Message level translaEon
_e echos
__ doesn’t
Too much info to go into here on how to have wordpress do the translaEon, but its worth building this in from the start
$message is output/returned if no translaEon is found so always worth building this in
![Page 46: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/46.jpg)
Help and More InformationWordpress Mailing Lists
http://codex.wordpress.org/Mailing_Lists
Wordpress Codex (or Google term + “codex”)
http://codex.wordpress.org/
Wordpress Support Forum
http://wordpress.org/support/
Writing a Plugin
http://codex.wordpress.org/Writing_a_Plugin
46Wordpres support forum – if the FAQ can’t helpGoogle search – whole community out there that can help
![Page 47: Presentation: Plugging Into Wordpress](https://reader035.fdocuments.net/reader035/viewer/2022062219/554bda49b4c905706a8b5414/html5/thumbnails/47.jpg)
Matt Harrishttp://[email protected]
All the links: http://ma.gnolia.com/people/themattharris/tags/plugging%20into%20wordpress
Image credits:“Hello … my name is”: http://www.flickr.com/photos/thost/2244046981/“Hooks for hand”: http://www.flickr.com/photos/gaetanlee/2906941718/“Wordpress Logo”: http://wordpress.org/about/logos/
47