0
WordPress Plugin Basics Prepared by Amanda Giles March 2011 [email_address]
What is a Plugin? <ul><li>WordPress Plugins allow easy modification, customization, and enhancement to a WordPress blog. I...
Why Write a Plugin? <ul><li>Solve a problem </li></ul><ul><li>Extend existing functionality </li></ul><ul><li>Save time </...
Plugin Basics <ul><li>1 or more files placed in the  wp-content/plugins  folder </li></ul><ul><li>1 file should have the h...
Plugin Header <ul><li>One of your plugin files must have this header for WP to recognize it: </li></ul><?php /* Plugin Nam...
Plugin License <ul><li>Most WordPress plugins use the GPL2 license also used by WordPress. To use it, include the followin...
Plugin Code May Include: <ul><li>Hooks to  Actions  or  Filters </li></ul><ul><ul><li>Hooks define how your plugin interac...
<ul><li>Actions hooks are triggered by events in WP such as  wp_head ,  wp_footer ,  admin_init ,  register_user ,  commen...
Filter Hooks <ul><li>Filter hooks are used to modify text before adding it to the database or displaying it on screen – ex...
Filter Functions <ul><li>A filter is altering text (or potentially altering text) </li></ul><ul><li>Therefore, a filter fu...
Removing WP Functionality <ul><li>Sometimes a plugin is designed to remove something WP is already doing </li></ul><ul><li...
Changes to WP Admin <ul><li>Add an Options page within Admin </li></ul><ul><li>Save a new plugin option in WP Database: </...
Saving Data <ul><li>For small bits of data to be saved, using the WP options table works well </li></ul><ul><li>Data write...
Creating new database tables <ul><li>Define table name: </li></ul>global $wpdb;  $table_name = $wpdb->prefix . “contributo...
Other Considerations <ul><li>Ensure your plugin has a  unique  folder name (or  unique  file names if not in a folder) </l...
Internationalization <ul><li>Goal is to mark strings which can be translated (even if no current translation exists) </li>...
More Information <ul><li>Writing a Plugin: </li></ul><ul><li>http://codex.wordpress.org/Writing_a_Plugin </li></ul><ul><li...
Upcoming SlideShare
Loading in...5
×

WordPress Plugin Basics

2,955

Published on

Reviews the basic of creating a WordPress plugin and some of the things you can do with a plugin. Presentation prepared for the Seacoast WordPress Developers Meetup in NH.

Published in: Self Improvement
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,955
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
102
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "WordPress Plugin Basics"

  1. 1. WordPress Plugin Basics Prepared by Amanda Giles March 2011 [email_address]
  2. 2. What is a Plugin? <ul><li>WordPress Plugins allow easy modification, customization, and enhancement to a WordPress blog. Instead of changing the core programming of WordPress, you can add functionality with WordPress Plugins. Here is a basic definition: </li></ul><ul><li>WordPress Plugin: A WordPress Plugin is a program, or a set of one or more functions, written in the PHP scripting language, that adds a specific set of features or services to the WordPress weblog, which can be seamlessly integrated with the weblog using access points and methods provided by the WordPress Plugin Application Program Interface (API). </li></ul><ul><li>Swiped from http://codex.wordpress.org/Writing_a_Plugin </li></ul>
  3. 3. Why Write a Plugin? <ul><li>Solve a problem </li></ul><ul><li>Extend existing functionality </li></ul><ul><li>Save time </li></ul><ul><li>Portability (changing themes, using on multiple sites) </li></ul><ul><li>Make money (???) </li></ul>
  4. 4. Plugin Basics <ul><li>1 or more files placed in the wp-content/plugins folder </li></ul><ul><li>1 file should have the header so WP recognizes it </li></ul><ul><li>Can integrate into the admin as well your site pages and posts </li></ul><ul><li>Can extend or even remove existing WP functionality (careful!) </li></ul>
  5. 5. Plugin Header <ul><li>One of your plugin files must have this header for WP to recognize it: </li></ul><?php /* Plugin Name: Header and Footer Plugin URI: http://www.satollo.net/plugins/header-footer Description: Lets you to add code to the head and footer of pages. Author: Satollo Author URI: http://www.satollo.net */ ?>
  6. 6. Plugin License <ul><li>Most WordPress plugins use the GPL2 license also used by WordPress. To use it, include the following in your plugin file: </li></ul><?php /* Copyright YEAR PLUGIN_AUTHOR_NAME (email : PLUGIN AUTHOR EMAIL) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ ?>
  7. 7. Plugin Code May Include: <ul><li>Hooks to Actions or Filters </li></ul><ul><ul><li>Hooks define how your plugin interacts with WordPress </li></ul></ul><ul><li>Functions </li></ul><ul><li>Defined Constants </li></ul><ul><li>New Admin pages & saved options </li></ul>
  8. 8. <ul><li>Actions hooks are triggered by events in WP such as wp_head , wp_footer , admin_init , register_user , comment_post </li></ul><ul><li>Syntax is: </li></ul><ul><li>P=Priority (numeric, defaults to 10, lower the number the earlier it executes) </li></ul><ul><li>N=Number of arguments the function can accept </li></ul><ul><li>Example: </li></ul>Action Hooks add_action(‘hook-name’, ‘function-name’) add_action(‘hook-name’, ‘function-name’, P, N) add_action( 'admin_notices', 'hello_dolly' ); function hello_dolly() { $chosen = hello_dolly_get_lyric(); echo &quot;<p id='dolly'>$chosen</p>&quot;; }
  9. 9. Filter Hooks <ul><li>Filter hooks are used to modify text before adding it to the database or displaying it on screen – examples include the_content , the_title , posts_where </li></ul><ul><li>Syntax is: </li></ul><ul><li>P=Priority (numeric, defaults to 10, lower the number the earlier it executes) </li></ul><ul><li>N=Number of arguments the function can accept </li></ul><ul><li>Example: </li></ul>add_filter(‘hook-name’, ‘function-name’); add_filter(‘hook-name’, ‘function-name’, P, N); add_filter( ‘the_title', ‘star_titles' );
  10. 10. Filter Functions <ul><li>A filter is altering text (or potentially altering text) </li></ul><ul><li>Therefore, a filter function is receiving text as a parameter and must return text as a return value </li></ul><ul><li>Example: </li></ul>add_filter( ‘the_title', ‘star_titles' ); function star_titles($title) { return ‘*** ‘ . $title . ‘ ***’; }
  11. 11. Removing WP Functionality <ul><li>Sometimes a plugin is designed to remove something WP is already doing </li></ul><ul><li>Syntax is: </li></ul><ul><li>Obviously, be very careful when doing this! </li></ul><ul><li>remove_action(‘hook-name’, ‘function-name’); </li></ul><ul><li>remove_filter(‘hook-name’, ‘function-name’); </li></ul>
  12. 12. Changes to WP Admin <ul><li>Add an Options page within Admin </li></ul><ul><li>Save a new plugin option in WP Database: </li></ul><ul><li>Update an option in WP Database: (even someone else’s): </li></ul><ul><li>Retrieve an option from WP Database: </li></ul>add_options_page(‘page_title’,‘menu_title’,‘capability’,’menu-slug’,’function-name’); add_options_page(‘Footer Edit’,‘Footer Edit’,‘manage_options’,’footer-edit’, ’ footer-edit/footer.php’); add_option(‘option_name’,‘option_value’); add_option(‘footer_text’,‘Copyright 2011’); update_option(‘option_name’,‘option_value’); get_option(‘option_name’);
  13. 13. Saving Data <ul><li>For small bits of data to be saved, using the WP options table works well </li></ul><ul><li>Data writes are time consuming. To save time writing and pulling data, can combine bits of data into an array. </li></ul>add_option(‘first_name’,‘Jane’); add_option(‘middle_name’,‘Ann’); add_option(‘last_name’,‘Smith’); $name_arr = array( “ first_name” => ‘Jane’, “ middle_name” => ‘Ann’, “ last_name” => ‘Smith’); add_option(‘full_name’, $name_arr ); Can be written as What if your plugin requires much more data to be stored in the database???
  14. 14. Creating new database tables <ul><li>Define table name: </li></ul>global $wpdb; $table_name = $wpdb->prefix . “contributor_names&quot;; <ul><li>Check if table already exists: </li></ul>if($wpdb->get_var(&quot;SHOW TABLES LIKE '$table_name'&quot;) != $table_name) <ul><li>Add table using dbDelta function: </li></ul>$sql = &quot;CREATE TABLE &quot; . $table_name . &quot; ( id mediumint(9) NOT NULL AUTO_INCREMENT, first_name tinytext NOT NULL, middle_name tinytext NOT NULL, last_name tinytext NOT NULL, url VARCHAR(200) NOT NULL, UNIQUE KEY id (id) );&quot;; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql);
  15. 15. Other Considerations <ul><li>Ensure your plugin has a unique folder name (or unique file names if not in a folder) </li></ul><ul><li>Use unique function names. Having a uniform prefix for all your functions can help with this. Very important! </li></ul><ul><li>WP Version Checking within code, or check if specific WP function exists before calling </li></ul><ul><li>Internationalization </li></ul><ul><li>Licensing </li></ul><ul><li>Documentation, Comments, or ReadMe.txt </li></ul>if ( function_exists ( ' has_term') )
  16. 16. Internationalization <ul><li>Goal is to mark strings which can be translated (even if no current translation exists) </li></ul><ul><li>Good idea if you plan on distributing your plugin </li></ul><ul><li>Translate String syntax: </li></ul><ul><li>Function name begins with 2 underscores back to back </li></ul><ul><li>The ‘text_domain’ is a unique identifier, which makes sure WP can distinguish between all loaded translations. Using the basename of your plugin is always a good choice. </li></ul>_ _(‘string_to_translate’); _ _e(‘string_to_translate’); //Echoed to browser _ _(‘string_to_translate’,‘text_domain’); _ _e(‘string_to_translate’,‘text_domain’); //Echoed to browser
  17. 17. More Information <ul><li>Writing a Plugin: </li></ul><ul><li>http://codex.wordpress.org/Writing_a_Plugin </li></ul><ul><li>Plugin API </li></ul><ul><li>http://codex.wordpress.org/Plugin_API </li></ul><ul><li>Plugin API / Action Reference </li></ul><ul><li>http://codex.wordpress.org/Plugin_API/Action_Reference </li></ul><ul><li>Plugin API / Filter Reference </li></ul><ul><li>http://codex.wordpress.org/Plugin_API/Filter_Reference </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×