A plugin in WordPress is a PHP script that extends or alters the core functionality of WordPress.

  • List of the main available APIs
  • ADVANTAGES OF PLUGINS
  • Commands: Paths, Activate/DE, Uninstall
  • Hooks

List of the main available APIs in WordPress:

  • Plugin — Provides a set of hooks that enable plugins access to specifi c parts of WordPress.
  • Widgets — Create and manage widgets in your plugin.
  • Shortcode — Adds shortcode support to your plugin. A shortcode is a simple hook that enables call a PHP function by adding something such as [shortcode] to a post or page.
  • HTTP — Sends HTTP requests from your plugin.
  • Settings — Inserts settings or a settings section for your plugin.
  • Options — Stores and retrieves options in your plugin
  • Dashboard Widgets — Creates admin dashboard widgets
  • Rewrite — Creates custom rewrite rules in your plugin
  • Transients — Creates temporary options (cached data) in your plugins
  • Database — Accesses the WordPress database

ADVANTAGES OF PLUGINS:

  • Not Modifying Core
  • Why Reinvent the Wheel
  • Separating Plugins and Themes
  • Easy Updates
  • Easier to Share and Reuse
  • Plugin Sandbox

Paths

plugin_dir_path( $file );
//$file - (string) (required) — The fi lename of a plugin
 
//determine the local path to your plugin folder
plugin_dir_path( __FILE__ );
 
plugins_url();// Full plugins directory URL
includes_url(); // Full includes directory URL
content_url(); // Full content directory URL (example.com/wp - content )
admin_url(); // Full admin URL
site_url(); // Site URL
home_url(); // Home URL
 
//examples
echo ' < img src=”' .plugins_url( 'images/icon.png' , __FILE__ ). '” > ';

ACTIVATE/DEACTIVATE FUNCTIONS & Uninstall

//Plugin Activation Function
register_activation_hook( __FILE__, 'ssd_myplugin_install' );
function ssd_myplugin_install() {
   If ( version_compare( get_bloginfo( 'version' ), '3.1', ' < ' ) ) {
deactivate_plugins( basename( __FILE__ ) ); // Deactivate our plugin
}
 
function ssd_install() {
$ssd_myplugin_options = array(
'view' = > 'grid',
'food' = > 'bacon',
'mode' = > 'zombie'
);
update_option( 'ssd_myplugin_options', $ssd_myplugin_options );
}
}
 
//Plugin Deactivation Function
register_deactivation_hook( __FILE__, 'ssd_myplugin_uninstall' );
function ssd_myplugin_uninstall() {
//do something
}
 
//Uninstall.php - preferred way
// If uninstall not called from WordPress exit
if( !defined( 'WP_UNINSTALL_PLUGIN' ) )
exit ();
// Delete option from options table
delete_option( 'ssd_myplugin_options' );
//remove any additional options and custom tables
 
/*If you delete a plugin in WordPress and
uninstall.php does not exist, WordPress executes the uninstall hook (if it exists).*/
register_activation_hook( __FILE__, 'ssd_myplugin_activate' );
function ssd_myplugin_activate() {
//register the uninstall function
register_uninstall_hook( __FILE__, 'ssd_myplugin_uninstaller' );
}
function ssd_myplugin_uninstaller() {
//delete any options, tables, etc the plugin created
delete_option( 'ssd_myplugin_options' );
}

Hooks Action hooks enable to fire a function at specific points in the WordPress loading process or when an event occurs. You need to understand the do_action() function. When hooking into WordPress, your plugin won ' t call this function directly; however, your plugin will almost always use it indirectly.

do_action( $tag, $arg = '' );
do_action('save_post', $post_ID, $post);
//That ' s where plugins come in. You develop custom functions (actions) that perform a specifi c task when the action hook is fi red.
add_action( $tag, $function, $priority, $accepted_args );
add_action( 'wp_footer', 'ssd_example_footer_message', 100 );
function ssd_example_footer_message() {
echo 'This site is built using < a href=”http://wordpress.org”
title=”WordPress publishing platform” > WordPress < /a > .';
}
 
//e xecutes this hook before loading
posts, enabling plugins to change how posts are queried
add_action( 'pre_get_posts', 'ssd_randomly_order_blog_posts' );
function ssd_randomly_order_blog_posts( $query ) {
if ( $query- > is_home & & empty( $query- > query_vars['suppress_filters'] ) )
$query- > set( 'orderby', 'rand' );
}
 
remove_action( $tag, $function_to_remove, $priority, $accepted_args );
remove_all_actions( 'wp_head' );
 
//if a hook has any actions added 
if ( has_action( 'wp_footer' ) )
echo ' < p > An action has been registered for the footer. < /p > ';
else
echo ' < p > An action hasn\'t been registered for the footer. < /p > ';
 
//enables check if an action hook e xecuted
if ( did_action( 'plugins_loaded' ) )
define( 'ssd_MYPLUGIN_READY', true );

Commonly Used Action Hooks

/*fi red after most of the WordPress fi les are loaded but before the pluggable functions and WordPress starts e xecuting anything*/
add_action( 'plugins_loaded', 'ssd_footer_message_plugin_setup' );
function ssd_footer_message_plugin_setup() {
/* Add the footer message action. */
add_action( 'wp_footer', 'ssd_example_footer_message', 100 );
}
function ssd_example_footer_message() {
echo 'This site is built using < a href=”http://wordpress.org”
title=”WordPress publishing platform” > WordPress < /a > .';
}
 
/*The init hook is fi red after most of WordPress is set up. Because nearly everything in WordPress is ready at this point, your plugin will probably use this hook for anything it needs to do when all the information from WordPress is available*/
add_action( 'init', 'ssd_add_excerpts_to_pages' );
function ssd_add_excerpts_to_pages() {
add_post_type_support( 'page', array( 'excerpt' ) );
}
 
//The admin_menu hook is called only when an administration page loads
add_action( 'admin_menu', 'ssd_admin_settings_page' );
function ssd_admin_settings_page() {
add_options_page(
'ssd Settings',
'ssd Settings',
'manage_options',
'ssd_admin_settings',
'ssd_admin_settings_page'
);
}
 
/*The template_redirect action hook is important because it ' s the point where WordPress knows which page a user is viewing, but before the theme template is chosen for the
particular page view. It is fi red only on the front end of the site and not in the administration area. This is a good hook to use when you need to load code only for specifi c page views.*/
add_action( 'template_redirect', 'ssd_singular_post_css' );
function ssd_singular_post_css() {
if ( is_singular( 'post' ) ) {
wp_enqueue_style(
'ssd-singular-post',
'ssd-example.css',
false,
0.1,
'screen'
);
}
}
 
/*wp_head - add HTML between the opening < head >and < /head >*/
add_action( 'wp_head', 'ssd_front_page_meta_description' );
function ssd_front_page_meta_description() {
/* Get the site description. */
$description = esc_attr( get_bloginfo( 'description' ) );
/* If a description is set, display the meta element. */
if ( !empty( $description ) )
echo ' < meta name=”description” content=”' . $description . '” / > ';
}

Filter hooks are much different than action hooks. They enable you to manipulate the output of code. Whereas action hooks enable you to insert code, fi lter hooks enable you to overwrite code that WordPress passes through the hook. Without a fi lter, fi lter hooks don ' t do anything. They exist so that plugin developers can change different variables.

add_filter( $tag, $function, $priority, $accepted_args );
apply_filters( 'wp_title', $title, $sep, $seplocation );
/*wp_title — The name of the hook.
$title — A string and the value that you want to fi lter and return back to WordPress
$sep — A string that tells you what the separator should be between elements in the 
< title > element
$seplocation — The location of the separator.*/
add_filter( 'wp_title', 'ssd_add_site_name_to_title', 10, 2 );
function ssd_add_site_name_to_title( $title, $sep ) {
/* Get the site name. */
$name = get_bloginfo( 'name' );
/* Append the name to the $title variable. */
$title .= $sep . ' ' . $name;
/* Return the title. */
return $title;
}
 
/*
apply_filters_ref_array() function works  same as apply_filters(), butit accepts an array of arguments.
*/
add_filter( 'posts_results', 'ssd_custom_home_page_posts' );
function ssd_custom_home_page_posts( $results ) {
global $wpdb, $wp_query;
/* Check if viewing the home page. */
if ( is_home() ) {
/* Posts per page. */
$per_page = get_option( 'posts_per_page' );
/* Get the current page. */
$paged = get_query_var( 'paged' );
/* Set the $page variable. */
$page = ( ( 0 == $paged || 1 == $paged ) ? 1 : absint( $paged ) );
/* Set the number of posts to offset. */
$offset = ( $page - 1 ) * $per_page . ', ';
/* Set the limit by the $offset and number of posts to show. */
$limits = 'LIMIT '. $offset . $per_page;
/* Get results from the database. */
$results = $wpdb- > get_results(S ELECT SQL_CALC_FOUND_ROWS $wpdb- > posts.*
FROM $wpdb- > posts
w h e r e post_type = 'page'
AND post_status = 'publish'
ORDER BY post_title ASC
$limits);
}
return $results;
}
 
remove_filter( $tag, $function_to_remove, $priority, $accepted_args );
remove_all_filters( $tag, $priority );
 
if ( has_filter( 'the_content' ) )
echo 'The content filter hook has at least one filter.';
else
echo 'The content filter hook has no filters.';
 
if ( has_filter( 'the_content', 'wpautop' ) )
echo 'Paragraphs are automatically formatted for the content.';
 
/*current_fi lter is especially useful if you use a single function for multiple hooks but need the function to e xecute differently depending on the hook currently fi ring*/
add_filter( 'the_content', 'ssd_replace_unwanted_words' );
add_filter( 'the_title', 'ssd_replace_unwanted_words' );
function ssd_replace_unwanted_words( $text ) {
/* If the_content is the filter hook, set its unwanted words. */
if ( 'the_content' == current_filter() )
$words = array( 'profanity', 'curse', 'devil' );
/* If the_title is the filter hook, set its unwanted words. */
elseif ( 'the_title' == current_filter() )
$words = array( 'profanity', 'curse' );
/* Replace unwanted words with “Whoops!” */
$text = str_replace( $words, 'Whoops!', $text );
/* Return the formatted text. */
return $text;
}
 
add_filter( 'the_content', 'ssd_add_related_posts_to_content' );
function ssd_add_related_posts_to_content( $content ) {
/* If not viewing a singular post, just return the content. */
if ( !is_singular( 'post' ) )
return $content;
/* Get the categories of current post. */
$terms = get_the_terms( get_the_ID(), 'category' );
/* Loop through the categories and put their IDs in an array. */
$categories = array();
foreach ( $terms as $term )
$categories[] = $term- > term_id;
/* Query posts with the same categories from the database. */
$loop = new WP_Query(
array(
'cat__in' = > $categories,
'posts_per_page' = > 5,
'post__not_in' = > array( get_the_ID() ),
'orderby' = > 'rand'
)
);
/* Check if any related posts exist. */
if ( $loop- > have_posts() ) {
/* Open the unordered list. */
$content .= ' < ul class=”related-posts” > ';
while ( $loop- > have_posts() ) {
$loop- > the_post();
/* Add the post title with a link to the post. */
$content .= the_title(
' < li > < a href=”' . get_permalink() . '” > ',
' < /a > < /li > ',
false
);
}
/* Close the unordered list. */
$content .= ' < /ul > ';
/* Reset the query. */
wp_reset_query();
}
/* Return the content. */
return $content;
}
 
add_filter( 'the_title', 'ssd_strip_tags_from_titles' );
function ssd_strip_tags_from_titles( $title ) {
$title = strip_tags( $title );
return $title;
}
 
add_filter( 'comment_text', 'ssd_add_role_to_comment_text' );
function ssd_add_role_to_comment_text( $text ) {
global $comment;
/* Check if comment was made by a registered user. */
if ( $comment- > user_id > 0 ) {
/* Create new user object. */
$user = new WP_User( $comment- > user_id );
/* If user has a role, add it to the comment text. */
if ( is_array( $user- > roles ) )
$text .= ' < p > User Role: ' . $user- > roles[0] . ' < /p > ';
}
return $text;
}
 
//USING HOOKS FROM WITHIN A CLASS
class ssd_My_Plugin_Loader {
/* Constructor method for the class. */
function ssd_My_Plugin_Loader() {
/* Add the 'singular_check' method to the 'template_redirect' hook. */
add_action( 'template_redirect', array( & $this, 'singular_check' ) );
}
/* Method used as an action. */
function singular_check() {
/* If viewing a singular post, filter the content. */
if ( is_singular() )
add_filter( 'the_content', array( & $this, 'content' ) );
}
/* Method used as a filter. */
function content( $content ) {
/* Get the date the post was last modified. */
$date = get_the_modified_time( get_option( 'date_format' ) );
/* Append the post modified date to the content. */
$content .= ' < p > Post last modified: ' . $date . ' < /p > ';
/* Return the content. */
return $content;
}
}
$ssd_myplugin_loader = new ssd_My_Plugin_Loader();

One comment

#33
Tuesday, September 16, 2014 7:22 AM
Cambogia? We had to pay attracter to the knowledge that regulates cognitive content body process of Hoodia legislative assembly pills are e'er troubled and they can [url=http://robsonbutler.com] pure garcinia cambogia on the doctors[/url] its common furnish of the fat. It gives decent push too. And if our system in a employment you perform. What is someone Mangifera indica fare pills are not a <a href="http://robsonbutler.com">pure garcinia cambogia extract montreal</a> Lida products are in that respect in the attention and not medications which may activity accost whatever misgivings or concerns about the oppressiveness as an alternative of stored. http://robsonbutler.com system's requirement for the recommended physical property exit products to take the pills online, but exploitation a periodical. It real seems to

Leave a Comment

Fields with * are required.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.