• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
WordPress Plugin Basics
 

WordPress Plugin Basics

on

  • 2,951 views

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.

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.

Statistics

Views

Total Views
2,951
Views on SlideShare
2,901
Embed Views
50

Actions

Likes
1
Downloads
86
Comments
0

2 Embeds 50

http://www.wordpressdevseacoast.com 48
http://faisalawanisee.wordpress.com 2

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    WordPress Plugin Basics WordPress Plugin Basics Presentation Transcript

    • WordPress Plugin Basics Prepared by Amanda Giles March 2011 [email_address]
    • What is a Plugin?
      • 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:
      • 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).
      • Swiped from http://codex.wordpress.org/Writing_a_Plugin
    • Why Write a Plugin?
      • Solve a problem
      • Extend existing functionality
      • Save time
      • Portability (changing themes, using on multiple sites)
      • Make money (???)
    • Plugin Basics
      • 1 or more files placed in the wp-content/plugins folder
      • 1 file should have the header so WP recognizes it
      • Can integrate into the admin as well your site pages and posts
      • Can extend or even remove existing WP functionality (careful!)
    • Plugin Header
      • One of your plugin files must have this header for WP to recognize it:
      <?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 */ ?>
    • Plugin License
      • Most WordPress plugins use the GPL2 license also used by WordPress. To use it, include the following in your plugin file:
      <?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 */ ?>
    • Plugin Code May Include:
      • Hooks to Actions or Filters
        • Hooks define how your plugin interacts with WordPress
      • Functions
      • Defined Constants
      • New Admin pages & saved options
      • Actions hooks are triggered by events in WP such as wp_head , wp_footer , admin_init , register_user , comment_post
      • Syntax is:
      • P=Priority (numeric, defaults to 10, lower the number the earlier it executes)
      • N=Number of arguments the function can accept
      • Example:
      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;; }
    • Filter Hooks
      • 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
      • Syntax is:
      • P=Priority (numeric, defaults to 10, lower the number the earlier it executes)
      • N=Number of arguments the function can accept
      • Example:
      add_filter(‘hook-name’, ‘function-name’); add_filter(‘hook-name’, ‘function-name’, P, N); add_filter( ‘the_title', ‘star_titles' );
    • Filter Functions
      • A filter is altering text (or potentially altering text)
      • Therefore, a filter function is receiving text as a parameter and must return text as a return value
      • Example:
      add_filter( ‘the_title', ‘star_titles' ); function star_titles($title) { return ‘*** ‘ . $title . ‘ ***’; }
    • Removing WP Functionality
      • Sometimes a plugin is designed to remove something WP is already doing
      • Syntax is:
      • Obviously, be very careful when doing this!
      • remove_action(‘hook-name’, ‘function-name’);
      • remove_filter(‘hook-name’, ‘function-name’);
    • Changes to WP Admin
      • Add an Options page within Admin
      • Save a new plugin option in WP Database:
      • Update an option in WP Database: (even someone else’s):
      • Retrieve an option from WP Database:
      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’);
    • Saving Data
      • For small bits of data to be saved, using the WP options table works well
      • Data writes are time consuming. To save time writing and pulling data, can combine bits of data into an array.
      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???
    • Creating new database tables
      • Define table name:
      global $wpdb; $table_name = $wpdb->prefix . “contributor_names&quot;;
      • Check if table already exists:
      if($wpdb->get_var(&quot;SHOW TABLES LIKE '$table_name'&quot;) != $table_name)
      • Add table using dbDelta function:
      $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);
    • Other Considerations
      • Ensure your plugin has a unique folder name (or unique file names if not in a folder)
      • Use unique function names. Having a uniform prefix for all your functions can help with this. Very important!
      • WP Version Checking within code, or check if specific WP function exists before calling
      • Internationalization
      • Licensing
      • Documentation, Comments, or ReadMe.txt
      if ( function_exists ( ' has_term') )
    • Internationalization
      • Goal is to mark strings which can be translated (even if no current translation exists)
      • Good idea if you plan on distributing your plugin
      • Translate String syntax:
      • Function name begins with 2 underscores back to back
      • 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.
      _ _(‘string_to_translate’); _ _e(‘string_to_translate’); //Echoed to browser _ _(‘string_to_translate’,‘text_domain’); _ _e(‘string_to_translate’,‘text_domain’); //Echoed to browser
    • More Information
      • Writing a Plugin:
      • http://codex.wordpress.org/Writing_a_Plugin
      • Plugin API
      • http://codex.wordpress.org/Plugin_API
      • Plugin API / Action Reference
      • http://codex.wordpress.org/Plugin_API/Action_Reference
      • Plugin API / Filter Reference
      • http://codex.wordpress.org/Plugin_API/Filter_Reference