wordpress plugin basics

17
WordPress Plugin Basics Prepared by Amanda Giles March 2011 [email protected]

Upload: amanda-giles

Post on 08-May-2015

2.911 views

Category:

Self Improvement


0 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: WordPress Plugin Basics

WordPress Plugin Basics

Prepared by Amanda GilesMarch 2011

[email protected]

Page 2: WordPress Plugin Basics

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

Page 3: WordPress Plugin Basics

Why Write a Plugin?

• Solve a problem• Extend existing functionality• Save time• Portability (changing themes, using on

multiple sites)• Make money (???)

Page 4: WordPress Plugin Basics

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!)

Page 5: WordPress Plugin Basics

Plugin Header

• One of your plugin files must have this header for WP to recognize it:

<?php/*Plugin Name: Header and FooterPlugin URI: http://www.satollo.net/plugins/header-footerDescription: Lets you to add code to the head and footer of pages.Author: SatolloAuthor URI: http://www.satollo.net*/?>

Page 6: WordPress Plugin Basics

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*/?>

Page 7: WordPress Plugin Basics

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

Page 8: WordPress Plugin Basics

• 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 "<p id='dolly'>$chosen</p>";

}

Page 9: WordPress Plugin Basics

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' );

Page 10: WordPress Plugin Basics

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 . ‘ ***’;

}

Page 11: WordPress Plugin Basics

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’);

Page 12: WordPress Plugin Basics

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’);

Page 13: WordPress Plugin Basics

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???

Page 14: WordPress Plugin Basics

Creating new database tables• Define table name:

global $wpdb; $table_name = $wpdb->prefix . “contributor_names";

• Check if table already exists:if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name)

• Add table using dbDelta function:$sql = "CREATE TABLE " . $table_name . " (

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));";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');dbDelta($sql);

Page 15: WordPress Plugin Basics

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') )

Page 16: WordPress Plugin Basics

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

Page 17: WordPress Plugin Basics

More InformationWriting a Plugin:http://codex.wordpress.org/Writing_a_Plugin

Plugin APIhttp://codex.wordpress.org/Plugin_API

Plugin API / Action Referencehttp://codex.wordpress.org/Plugin_API/Action_Reference

Plugin API / Filter Referencehttp://codex.wordpress.org/Plugin_API/Filter_Reference