<Head> Presentation: Plugging Into Wordpress

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite

    <Head> Presentation: Plugging Into Wordpress - Presentation Transcript

    1. Plugging into Matt Harris <head> conference 26th October 2008 1
    2. Types of Wordpress Wordpress.com Hosted blogging solution Wordpress.org Downloadable edition for use on your own server Wordpress MU Multi-user edition of Wordpress.org 2
    3. Hosted Wordpress.com Free Maintained and backed up for you Not hackable or pluggable by you 3 Hosted: scalable, Automa1c deals with the spikes you may  get
    4. Customisable Wordpress.org Self-hosted Complete code control Maintenance, backup and reliability are your responsibility 4 Customisable: hackable, expandable.  Add your own  features
    5. All of Wordpress.org pros and cons Wordpress MU Hosting of multiple blogs under one install Bespoke permission settings per blog Your own Wordpress.com 5 Basically this is the meat behind Wordpress.com Great for Uni’s, EducaEonal places, newspapers, blog networks etc
    6. User Interface 6
    7. User Interface 7
    8. User Interface 8
    9. User Interface 9
    10. Admin Interface 10
    11. Admin Interface 11
    12. Admin Interface 12
    13. Hooks and Filters add_action($tag, $function, $priority, $accepted_args) add_filter($tag, $function, $priority, $accepted_args) add_shortcode($tag, $function) http://www.flickr.com/photos/gaetanlee/2906941718/ 13 Don’t hack at the core code – makes it difficult to update the your installaEon Use 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 acEons hOp://codex.wordpress.org/Plugin_API/AcEon_Reference hOp://codex.wordpress.org/Plugin_API/Filter_Reference hOp://codex.wordpress.org/Shortcode_API
    14. Where to put your code Plugins Inside it’s own file Theme folder in the file functions.php 14 Plugins advantage – easily switch off and onable FuncEons – 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)
    15. 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 tag Str_ireplace is case insensiEve To test this included it in funcEons.php
    16. 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 tag Str_ireplace is case insensiEve To test this included it in funcEons.php
    17. Simple Filter Before After 17
    18. Simple Hook <?php function addText() { echo “Can you see me?”; } add_action(’wp_footer', ’addText’); ?> http://codex.wordpress.org/Plugin_API/Action_Reference 18 wp_footer Runs when the template calls the wp_footer funcEon, generally near the boOom of the blog page.
    19. Simple Hook <?php function addText() { echo “Can you see me?”; } add_action(’wp_footer', ’addText’); ?> http://codex.wordpress.org/Plugin_API/Action_Reference 19 wp_footer Runs when the template calls the wp_footer funcEon, generally near the boOom of the blog page.
    20. 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 20 wp_footer Runs when the template calls the wp_footer funcEon, generally near the boOom of the blog page.
    21. Simple Hook Before After 21
    22. 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 22 Shortcodes 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)
    23. 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> 23 Shortcodes cannot be nested be default. Must add do_shortcode($content) into your handler to do this
    24. Namespacing http://www.flickr.com/photos/thost/2244046981/ 24
    25. Namespacing <?php function addText() { echo “Can you see me?”; } add_action(’wp_footer', ’addText’); ?> 25
    26. Namespacing <?php function tmh_addText() { echo “Can you see me?”; } add_action(’wp_footer', ’tmh_addText’); ?> 26
    27. Widgets 27
    28. Plugin Code Structure <?php /* Plugin Name: My Widget Plugin URI: http://themattharris.com Description: A widget that puts some text in the sidebar Author: Matt Harris Version: 1.0 Author URI: http://themattharris.com */ ?> 28
    29. 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
    30. 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
    31. 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
    32. 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 } ?> 32 wp_specialchars ‐ Like htmlspecialchars except don't double‐encode HTML  enEEes
    33. 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 } ?> 33
    34. 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
    35. Putting it all together <?php /* Plugin Name: My Widget Plugin URI: http://themattharris.com Description: A widget that puts some text in the sidebar Author: Matt Harris Version: 1.0 Author 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
    36. Making it work 36
    37. Making it work 37
    38. Making it work 38
    39. Making it work 39
    40. 40
    41. 41
    42. Protecting your blog check_admin_referer($action, [$query_arg]); Use with wp_create_nonce($action); Use this on any forms you have. 42
    43. 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 43 aOribute_escape (used for escaping for HTML aOributes) Kses checks for allow html (or removes it in nohtml)
    44. Protecting your blog <?php current_user_can($capability) ?> Example <?php if (current_user_can(‘unfiltered_html’)) { $data = Wp_filter_post_kses($data); } else { $data = wp_filter_nohtml_kses($data); } ?> http://codex.wordpress.org/Roles_and_Capabilities 44 aOribute_escape (used for escaping for HTML aOributes) Kses checks for allow html (or removes it in nohtml)
    45. Translation friendly <?php __($message, $domain); ?> <?php _e($message, $domain); ?> Example <?php _e(“Title:”,’tmh_pluginname’); ?> http://codex.wordpress.org/Translating_WordPress 45 WP 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
    46. Help and More Information Wordpress 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 46 Wordpres support forum – if the FAQ can’t help Google search – whole community out there that can help
    47. Matt Harris http://themattharris.com me@themattharris.com 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

    + Matt HarrisMatt Harris, 2 years ago

    custom

    1007 views, 1 favs, 0 embeds more stats

    The presentation I gave during the <Head> Conferenc more

    More info about this document

    CC Attribution License

    Go to text version

    • Total Views 1007
      • 1007 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 3
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories