Your SlideShare is downloading. ×
Write your first WordPress plugin
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Write your first WordPress plugin


Published on

Published in: Technology, Art & Photos

  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Write your firstWordPress pluginAnthony Montalbano @italianst4
  • 2. Who is Anthony Montalbano?Passionate for code Bachelors in Computer SciencePassionate for WordPress WordCamp Detroit OrganizerPassionate for open source WordPress plugin developerPassionate for words Serial bloggerPassionate for possibilities Co-founder of flipfrog and AMBR Detroit
  • 3. What is a WordPress plugin?"Plugins are tools to extend the functionality of WordPress." ~ IMPORTANT!The Codex The online manual for WordPress and a living repository for WordPress information and documentation.
  • 4. What can plugins do?
  • 5. WordPress Plugins by the Numbers21,214 345,389,937# of plugins # of plugin downloads 63 % of users that use 10 plugins or less 9,783+ # of plugin 12,134,168 developers # of downloads of the most popular plugin - All in One SEO PackSources:
  • 6. Your WordPress plugin "Google"
  • 7. Lets make a plugin!● Find and replace a word in the title.● Show what is being replaced as a sidebar widget.● Admin menu to change the find word and replace word.● Email me when a new post is published.
  • 8. Filter Hooks and Action HooksWordPress plugins rely on the many hookswithin the system to get things done.Filter HooksFilters are the hooks that WordPress launches to modifytext of various types before adding it to the database orsending it to the browser screen.Action HooksActions are the hooks that the WordPress core launches atspecific points during execution, or when specific eventsoccur. Source:
  • 9. How Hooks WorkWordPress Plugin API provides you with a setof PHP functions that allow you to signal yourown functions to be called when that hook iscalled.Filter Example:add_filter(the_title, function($title) { return <b>.$title. </b>;}) Filters: Example:add_action( save_post, my_save_post, 10, 2 ); Actions:
  • 10. How Hooks Work (continued)Hooks have 4 parameters● Tag (required) ○ This is the WordPress named location where the hook takes place.● Function (required) ○ This is the function to be called when the hook is executed.● Priority (optional) ○ This determines the order your function is run, the lower, the earlier.● Parameters (optional) ○ This is the number of parameters your function takes
  • 11. Set the Foundation● Create a new folder in wp-content/plugins● Create a php file with a plugin header comment box /* Plugin Name: My First Plugin Plugin URI: Description: This is a description of a plugin Author: Anthony Montalbano Version: alpha Author URI: */
  • 12. Activation and UninstallWhat will your plugin do when it is firstactivated? ● Create database tables, data, and files ● Update database tables, data, and filesWhat will your plugin do when it is uninstalled? ● Delete databases tables, data, files
  • 13. On ActivationAdd the following hook:register_activation_hook( __FILE__, demo_activate );Create a new function called demo_activate function demo_activate() { //do something when the plugin first initializes }; Source:
  • 14. On UninstallCreate a file called uninstall.php in the root directory.Add the following code: <?php if(!defined(WP_UNINSTALL_PLUGIN)) exit(); delete_option(demo_myValue); Source:
  • 15. Adding a FilterAdd the following filter hook:add_filter( the_title, demo_title_change );Create a new function called demo_title_change function demo_title_change($title) { //do something with the title str_replace( world, something, $title); return $title; }; Source:
  • 16. Adding an ActionAdd the following filter hook:add_action( publish_post, demo_email_me );Create a new function called demo_title_change function demo_email_me($post_id) { wp_mail(, New post!, New post on my demo blog, go check it out: . get_bloginfo (url)); return $post_id; }; Source:
  • 17. Adding a Settings PageFirst we need add a hook to where the settingspage will show in the admin: add_action(admin_menu, my_plugin_menu);Next we need to add a function to define themenu: function my_plugin_menu() { add_options_page(Demo Plugin Options, Demo Plugin, manage_options, demo-plugin, demo_plugin_options); } Source:
  • 18. Adding a Settings Page (continued)Finally we need to generate the HTML andfunctionality of the admin menu:function demo_plugin_options() { //get the option $replaceWord = get_option(demo_myValue); //save functionality if(isset($_REQUEST[demo_update_admin]) && $_REQUEST[demo_update_admin]) { update_option(demo_myValue, $_POST[myValue]); $replaceWord = $_POST[myValue]; echo "<div id=message class=updated fade><p>Demo PluginSettings Saved!</p></div>"; } //display the page include_once(dirname(__FILE__) . /demo_admin.php);}
  • 19. Adding a WidgetFirst we need to add a hook to load the widgeton widget initialization: add_action( widgets_init, demo_load_widgets );Next, we need to create a function to registerthe widget: function demo_load_widgets() { register_widget( "demo_widget" ); } Source:
  • 20. Adding a Widget (continued)Finally we create the widget by extending theWordPress Widget class: class Demo_Widget extends WP_Widget { public function __construct() { // widget actual processes parent::__construct( demo_widget, // Base ID Demo Widget, // Name array( description => __( My Little Demo Widget, text_domain ), ) // Arrgy ); } public function widget( $args, $instance ) { // outputs the content of the widget extract( $args ); $replaceWord = get_option(demo_myValue); echo $before_widget; if ( ! empty( $replaceWord ) ) echo $before_title . My value . $after_title . $replaceWord; echo $after_widget; } }
  • 21. Tip 1: Use a plugin prefixWhen creating a plugin, create a unique pluginprefix that can be used for all functions andvariables.Since there are many plugins, its important thatyour functions and variables dont conflict withother plugins.
  • 22. Tip 2: Never use PHP MySQL callsWordPress has a great database class calledWPDB and makes it very each to plugin to theWordPress database. simple name/value pairs you can useWordPress options
  • 23. Tip 3: Queuing Scripts and StylesThere are many cases where you may want toinclude a javascript or style sheet with yourplugin. WordPress has this functionality builtin. By default WordPress has many scriptsincluded, such as jQuery.
  • 24. Tip 4: Use WordPress Admin StylesThe WordPress admin has a style sheet thatshould be used when creating admin menus.The goal is to make your plugin fit seamlesswith WordPress.
  • 25. Tip 5: Prepare your SQL StatementsWordPress Database class has a functioncalled prepare(). Use this function to properlyprepare your SQL statements. http://codex.wordpress. org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks
  • 26. Theres a Plugin for That WordCamp Detroit 2010
  • 27. Thank you! Anthony Montalbano