You are on page 1of 7

Pluging Dev

Plugin activation/deactivation

register_activation_hook( __FILE__,
'function_to_execute_after_registration' );
function_to_execute_after_registration(){
}
Called when a plugin is going to be registered or activated
Register_deactivation_hook(__FILE__, function_to_execute)

Local Paths
To determine the local server path to your plugin, youll use the
plugin_dir_path() function. This function extracts the physical
location relative to the plugins directory from its file name.
<?php echo plugin_dir_path( __FILE__ .'js/script.js' ); ?>
plugins_url( 'images/icon.png', __FILE__ )

Usefull Urls
WordPress also features various functions to determine URLs in
WordPress. The following is a list of the functions available:
admin_url() Admin URL (http://example.com/wp-admin/)
site_url() Site URL for the current site (http://example.com)
home_url() Home URL for the current site (http://example.com)
includes_url() Includes directory URL (http://example.com/wp-
includes/)
content_url() Content directory URL (http://example.com/wp-
content/)
wp_upload_dir() Returns an array with location information on the
configured uploads Directory

Hook
A hook is simply a PHP function call with various parameters that can
be sent. Following is an
example showing a properly formatted Action hook call:
<?php add_action( $tag, $function_to_add, $priority, $accepted_args );
?>

The add_filter() function is used to execute a Filter action. You are
using the fi lter called the_content, which is the filter for your
post content. This tells WordPress that every time the content is
displayed, it needs to pass through your custom function called
prowp_function().
The add_filter() function can accept four parameters:
1. filter_action (string): The fi lter to use
2. custom_filter_function (string): The custom function to pass the fi
lter through
3. priority (integer): The priority in which this fi lter should run.
When multiple callback
functions are attached to the same hook, the priority parameter
determines the execution
order
4. accepted args (integer): The number of arguments the function
accepts

<?php
add_filter( 'the_content', 'prowp_profanity_filter' );
function prowp_profanity_filter( $content ) {
$profanities = array( 'sissy', 'dummy' );
$content= str_ireplace( $profanities, '[censored]', $content );
return $content;
}
?>

<?php
add_action( 'comment_post', 'prowp_email_new_comment' );
function prowp_email_new_comment() {
wp_mail( 'me@example.com', 'New blog comment',
'There is a new comment on your website: http://example.com' );
}
?>

Popular Filter Hooks
More than 1,500 different hooks are available in WordPress, which is a
bit overwhelming at fi rst.
Fortunately, a handful of them are used much more often than the rest.
This section explores some
of the more commonly used hooks in WordPress.
Some of the more common Filter hooks are:
the_content Applied to the content of the post or page before
displaying
the_content_rss Applied to the content of the post or page for RSS
inclusion
the_title Applied to the post or page title before displaying
comment_text Applied to the comment text before displaying
wp_title Applied to the page <title> before displaying
the_permalink Applied to the permalink URL

<?php //adds subscription text to any content
add_filter ( 'the_content', 'prowp_subscriber_footer' );
function prowp_subscriber_footer( $content ) {
if( is_single() ) {
$content.= '<h3>Enjoyed this article?</h3>';
$content.= '<p>Subscribe to my <a href="http://example.com/feed">RSS
feed</a>!</p>';
}
return $content;
}
?>
<?php //edits title
add_filter( 'the_title', 'prowp_custom_title' );
function prowp_custom_title( $title ) {
$title .= ' - By Example.com';
return $title;
}
?>
The default_content Filter hook is useful for setting the default
content when creating a new post
or page. This is helpful if you have a set format for all of your
posts as it can save you valuable
writing time:
<?php
add_filter( 'default_content', 'prowp_default_content' );
function prowp_default_content( $content ) {
$content = 'For more great content please subscribe to my RSS feed';
return $content;
}
?>
Filter hooks are exceptionally powerful for inserting your own
processing into a variety of points in
the Loop processing of each post. Realizing the full power of the
WordPress plugin system means
also using action hooks to fi re your own code in response to events
within the WordPress core.

Popular Action Hooks
Some of the more common Action hooks are:
publish_post Triggered when a new post is published.
create_category Triggered when a new category is created.
switch_theme Triggered when you switch themes.
admin_head Triggered in the <head> section of the admin dashboard.
wp_head Triggered in the <head> section of your theme.
wp_footer Triggered in the footer section of your theme usually
directly before the </body> tag.
init Triggered after WordPress has finished loading, but before
any headers are sent.
Good place to intercept $_GET and $_POST HTML requests.
admin_init: Same as init but only runs on admin dashboard pages.
user_register: Triggered when a new user is created.
comment_post: Triggered when a new comment is created.

<?php
add_action( 'wp_head', 'prowp_custom_css' );
function prowp_custom_css() {
?>
<style type="text/css">
a {
font-size: 14px; color: #000000; text-decoration: none;
}
a:hover { font-size: 14px; color: #FF0000; text-decoration:
underline;}
</style>
<?php
}
?>

The wp_footer hook is also a very commonly used Action hook. Using
this hook you can insert any custom code in the footer of the
WordPress theme. This is a great method for adding analytic tracking
code to your website:
<?php
add_action( 'wp_footer', 'prowp_site_analytics' );
function prowp_site_analytics() {
?>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ?
"https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost +
'google-analytics.com/ga.js'
type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXXXX-XX");
pageTracker._trackPageview();
</script>
<?php
}
?>

You might also like