write your first wordpress plugin
TRANSCRIPT
![Page 1: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/1.jpg)
Write your first WordPress pluginAnthony Montalbano @italianst4 [email protected]
![Page 2: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/2.jpg)
Who is Anthony Montalbano?
Passionate for codeBachelor's in Computer Science
Passionate for WordPressWordCamp Detroit Organizer
Passionate for open sourceWordPress plugin developer
Passionate for wordsSerial blogger
Passionate for possibilitiesCo-founder of flipfrog and AMBR Detroit
![Page 3: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/3.jpg)
What is a WordPress plugin?
"Plugins are tools to extend the functionality of WordPress."~ http://codex.wordpress.org/Plugins
MEGA IMPORTANT!
The Codex
The online manual for WordPress and a living repository for WordPress information and documentation.
![Page 4: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/4.jpg)
What can plugins do?
![Page 5: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/5.jpg)
WordPress Plugins by the Numbers
21,214# of plugins
345,389,937# of plugin downloads
Sources:http://wordpress.org/extend/plugins/http://digwp.com/2010/01/poll-results-how-many-plugins-do-you-use/http://www.daveligthart.com/top-1000-wordpress-plugin-authors/
63% of users that use 10 plugins or less
12,134,168# of downloads of the most popular
plugin - All in One SEO Pack
9,783+# of plugin developers
![Page 6: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/6.jpg)
Your WordPress plugin "Google"
![Page 7: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/7.jpg)
Let's 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.
![Page 8: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/8.jpg)
Filter Hooks and Action Hooks
WordPress plugins rely on the many hooks within the system to get things done.
Filter HooksFilters 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.
Action HooksActions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur.
Source: http://codex.wordpress.org/Plugin_API
![Page 9: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/9.jpg)
How Hooks Work
WordPress Plugin API provides you with a set of PHP functions that allow you to signal your own functions to be called when that hook is called.
Filter Example:
Action Example:
add_filter('the_title', function($title) { return '<b>'. $title. '</b>';})
add_action( 'save_post', 'my_save_post', 10, 2 );
Actions: http://codex.wordpress.org/Plugin_API/Action_Reference
Filters: http://codex.wordpress.org/Plugin_API/Filter_Reference
![Page 10: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/10.jpg)
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
![Page 11: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/11.jpg)
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 PluginPlugin URI: http://wordpress.org/extend/plugins/Description: This is a description of a pluginAuthor: Anthony MontalbanoVersion: alphaAuthor URI: http://www.ambrdetroit.com*/
http://codex.wordpress.org/Writing_a_Plugin#File_Headers
![Page 12: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/12.jpg)
Activation and Uninstall
What will your plugin do when it is first activated?
● Create database tables, data, and files● Update database tables, data, and files
What will your plugin do when it is uninstalled?● Delete databases tables, data, files
![Page 13: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/13.jpg)
On Activation
Add the following hook:
Create a new function called 'demo_activate'
Source: http://codex.wordpress.org/Function_Reference/register_activation_hook
register_activation_hook( __FILE__, 'demo_activate' );
function demo_activate() {//do something when the plugin first initializes
};
![Page 14: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/14.jpg)
On Uninstall
Create a file called uninstall.php in the root directory.
Add the following code:
Source: http://codex.wordpress.org/Function_Reference/register_deactivation_hook
<?phpif(!defined('WP_UNINSTALL_PLUGIN')) exit();
delete_option('demo_myValue');
![Page 15: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/15.jpg)
Adding a Filter
Add the following filter hook:
Create a new function called 'demo_title_change'
Source: http://codex.wordpress.org/Function_Reference/add_filter
add_filter( 'the_title, 'demo_title_change' );
function demo_title_change($title) {//do something with the titlestr_replace( 'world', 'something', $title);return $title;
};
![Page 16: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/16.jpg)
Adding an Action
Add the following filter hook:
Create a new function called 'demo_title_change'
Source: http://codex.wordpress.org/Function_Reference/add_filter
add_action( 'publish_post, 'demo_email_me' );
function demo_email_me($post_id) {wp_mail('[email protected]', 'New post!', 'New
post on my demo blog, go check it out:' . get_bloginfo('url'));
return $post_id;};
![Page 17: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/17.jpg)
Adding a Settings Page
First we need add a hook to where the settings page will show in the admin:
Next we need to add a function to define the menu:
add_action('admin_menu', 'my_plugin_menu');
Source: http://codex.wordpress.org/Function_Reference/add_options_page
function my_plugin_menu() {add_options_page('Demo Plugin Options', 'Demo
Plugin', 'manage_options', 'demo-plugin', 'demo_plugin_options');}
![Page 18: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/18.jpg)
Adding a Settings Page (continued)
Finally we need to generate the HTML and functionality 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 Plugin Settings Saved!</p></div>"; } //display the page include_once(dirname(__FILE__) . '/demo_admin.php');}
![Page 19: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/19.jpg)
Adding a Widget
First we need to add a hook to load the widget on widget initialization:
Next, we need to create a function to register the widget:
add_action( 'widgets_init', 'demo_load_widgets' );
Source: http://codex.wordpress.org/Widgets_API
function demo_load_widgets() { register_widget( "demo_widget" ); }
![Page 20: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/20.jpg)
Adding a Widget (continued)
Finally we create the widget by extending the WordPress 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; }}
![Page 21: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/21.jpg)
Tip 1: Use a plugin prefix
When creating a plugin, create a unique plugin prefix that can be used for all functions and variables.
Since there are many plugins, it's important that your functions and variables don't conflict with other plugins.
![Page 22: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/22.jpg)
Tip 2: Never use PHP MySQL calls
WordPress has a great database class called WPDB and makes it very each to plugin to the WordPress database.
http://codex.wordpress.org/Class_Reference/wpdb
For simple name/value pairs you can use WordPress options
http://codex.wordpress.org/Function_Reference/add_option
![Page 23: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/23.jpg)
Tip 3: Queuing Scripts and Styles
There are many cases where you may want to include a javascript or style sheet with your plugin. WordPress has this functionality built in. By default WordPress has many scripts included, such as jQuery.
http://codex.wordpress.org/Function_Reference/wp_enqueue_script
![Page 24: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/24.jpg)
Tip 4: Use WordPress Admin Styles
The WordPress admin has a style sheet that should be used when creating admin menus. The goal is to make your plugin fit seamless with WordPress.
http://codex.wordpress.org/User:TECannon/UI_Pattern_and_Style_Guide
![Page 25: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/25.jpg)
Tip 5: Prepare your SQL Statements
WordPress Database class has a function called prepare(). Use this function to properly prepare your SQL statements.
http://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks
![Page 26: Write your first WordPress plugin](https://reader033.vdocuments.mx/reader033/viewer/2022052618/554951f8b4c905fc4e8b4bd3/html5/thumbnails/26.jpg)
There's a Plugin for That
http://bit.ly/wcdetplugins
WordCamp Detroit 2010