Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

WordPress Plugin Development

A short presentation on "Developing Plugin for WordPress" for WordPressians 3rd Meetup @ BASIS Board Room, Dhaka, Bangladesh.
by

Nazmul Hasan Rupok

on 19 July 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of WordPress Plugin Development

WordPress Plugin Development
Who Am I
Nazmul Hasan Rupok
What's Inside?
Plugins Overview
File Structure
Creating a Plugin from Scratch
Activation & Deactivation Hooks
Action & Filter Hooks
Inserting CSS/JavaScript
Shortcode API
Adding Administration Menu
What is WordPress Plugin?
A Plugin in WordPress is a PHP Script that extend or alters the core functionality of WordPress.

Quite simply, Plugins are files installed in WordPress to add a feature or set of features to WordPress websites.
File Structure
Made from one or more php file(s)
Can optionally contain other file types (e.g. images, text files, translation files etc.)
Located directly in the wp-content\plugins directory or in a sub directory within the plugins folder.
Entry point is a .php file that contains a specific plugin header at its top
Let's Create a Plugin...
Freelancer
Web Developer
WordPressian
Android App Developer
www.rupok.me
Popular Plugins
Plugin Location
www/
wordpress/
wp-content/
plugins/
Plugin Files
Things to Remember
File Header
!important
An unique name
Readme.txt for WordPress repository
Plugin Homepage
Plugin Files
May be a single
php
file!
my-plugin
/
plugin.php
[inside the plugin directory]
Creating Plugin File
<?php
/*
Plugin Name: My Plugin
Plugin URI: http://www.w3xperts.net
Description: My Plugin Description
Version: 1.0
Author: Nazmul Hasan Rupok
Author URI: http://www.rupok.me
License : GPLv2 or later
*/
?>
Header
Parameters
Used to let user know about the details of plugins/available download options/addition doccumentation etc.
Plugin URI :
Description :
Version :
Author & Author URI :
License :
Used to provide a summary of functionality of the plugin.
Used to indicate version. Not mendatory but very important for updating.
Used to provide details about the developer
Used to define the conditions of using the plugin.
Preview on Dashboard
"Hooks" enable plugin developers to "hook" into WordPress and change how it works without modifying the core code.
Hooks
Two Kinds of Hooks
Action Hook
&
Filter Hook
Plugin Activation & Deactivation Hook
register_activation_hook( );
register_deactivation_hook( );
function
myplugin_activation
() {
}
register_activation_hook
( __FILE__, '
myplugin_activation
' );
function
myplugin_deactivation
() {
}
register_deactivation_hook
( __FILE__, '
myplugin_deactivation
' );
Why we should use Activation/Deactivation Hooks?
Create custom database table on activation to store data and remove tables on deactivation
Create custom options for plugin on activation and reset on deactivation.
It's neccessary if you need to let your user save plugin settings
Action
In WordPress; an Action is a PHP function that is executed at specific points throughout the WordPress Core.
add_action
( '
hook_name
', '
function_name
', [
priority
],

[
accepted_args
] );
Sample Action : "wp-login"
function
notify_on_login
() {
//your code here
//welcome message etc.
}
add_action('wp-login', '
notify_on_login
');
Common Action Hooks
plugins_loaded
- Earliest hook in the WP loading process, after all plugins have been loaded.

init
- Fire after most of WP is set up so all information is available.

admin_menu
- Runs when the basic admin menu structure is in place.

emplate_redirect
t - Executed just before the theme template is choosen

wp_head
- Executed on the front end of WordPress between the <head> tags.

wp_footer
- Runs on the footer of your theme.

admin_head
- Executed on the admin side of WordPress between the <head> tags.

admin-footer
- Runs in the footer of admin side of WordPress between the <head> tags.

user_register
- Executes when a new user account is created.

save_post
- Runs when a post or page is created or updated.
Filters
In WordPress, a Filter is a function that is associated with an existing Action by specifying any existing Hook.
-From the Codex
Functions that modify texts, list and various types of informations that are used and produced by WordPress
add_filter
( '
hook_name
', '
filter_function
', [
priority
],

[
accepted_args
] );
function
add_rss_invite
() {
//output to screen the link to RSS feed
//if first time visitor
}
add_filter('the_content', '
add_rss_invite
');
Sample Filter : "the_content"
Including CSS/JavaScripts
Filter Hooks...
When do I use which hooks?
Use actions when you want to add something to the existing page such as stylesheets, JavaScript dependencies, or send an email when an event has happened.

Use filters when you want to manipulate data coming out of the database prior to going to the browser, or coming from the browser prior to going into the database.
wp_register_script
( $handle, $src, $deps, $ver, $in_footer );

wp_enqueue_scripts
( $handle, $src, $deps, $ver, $in_footer );
Parameters

$handle
– what you’ll use to refer to this particular script wherever you might need to enqueue it, and you have to include this variable at the very least


$src
– the path to the source file within your plugin or theme


$deps
– an array containing the $handle for any other scripts your script needs to run (i.e. a dependency)


$ver
– the version number for your script, which can be used for cache-busting. By default, WordPress will use its own version number as the version number for your script


$in_footer
– do you want your script to load in the footer? Set this to true or false. It is false by default, so it loads in the header where wp_head() is, and if you specify true it will load where wp_footer() appears in the theme
OK, Let's try some example
function
my_scripts()
{

// Register the script like this for a plugin:

wp_register_script(
'custom-script'
, plugins_url(
'/js/custom-script.js', __FILE__
) );

// or
// Register the script like this for a theme:

wp_register_script(
'custom-script',
get_template_directory_uri()
. '/js/custom-script.js'
);


// For either a plugin or a theme, you can then enqueue the script:

wp_enqueue_script(
'custom-script' );

}
add_action
( 'wp_enqueue_scripts', 'my_scripts'
);

Loading a custom script:
The output we get from that basic example is :

<script

type
="text/javascript"
src=
"http://yourdomain.com/wp-content/plugins/yourplugin/js/custom-script.js?ver=3.3.1"
></script>

Loading scripts from Wordpress Libraries
function
my_scripts_with_jquery()
{

// Register the script like this for a plugin:

wp_register_script(
'custom-script'
, plugins_url(
'/js/custom-script.js', __FILE__
),
array
(
'jquery'
) );

// or
// Register the script like this for a theme:

wp_register_script(
'custom-script',
get_template_directory_uri()
. '/js/custom-script.js', array( 'jquery' )
);


// For either a plugin or a theme, you can then enqueue the script:

wp_enqueue_script(
'custom-script' );

}
add_action
( 'wp_enqueue_scripts', 'my_scripts_with_jquery'
);

The changed Output :

<script

type
='text/javascript'
src
='http://yourdomain.com/wp-includes/js/jquery/jquery.js?ver=1.7.1'
></script>



<script
type
='text/javascript'
src
='http://yourdomain.com/wp-content/plugins/yourplugin/js/custom-script.js?ver=3.3.1'
></script>

Shortcode API
What is Shortcode API?
Shortcode API, a simple set of functions for creating macro codes for use in post content.

Example :
[demo id="1"]

- is able to retrieve a demo record of ID 1and embed the results into the post.
Implementing Shortcodes
add_shortcode
("my_shortcode", "
my_shortcode_function
");

function
my_shortcode_function
() {
return "<h1> Hello Shortcodes </h1>";
}
Usage
Use
[my_shortcode]
on post content to display "
Hello Shortcodes
" as a first heading!
Adding Administration Menu
Admin Menu Functions :
add_menu_page
: create a top level menu
add_submenu_page
: define one or more sub menu pages


Sample Usage
<?php add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); ?>
See parameters & example here :

http://codex.wordpress.org/Creating_Options_Pages
Resources & References
http://codex.wordpress.org/Writing_a_Plugin
http://codex.wordpress.org/Plugin_API
http://codex.wordpress.org/Plugin_Resources
http://wp.tutsplus.com/articles/how-to-include-javascript-and-css-in-your-wordpress-themes-and-plugins/
http://www.slideshare.net/mahtonu/developing-wordpress-plugins-for-begineers
Thank You!
www.rupok.me
http://profiles.wordpress.org/re_enter_rupok
Full transcript