What is hook ??

WordPress hooks are arguably the basis of WordPress development, forming a large part of the core functionality and used by almost every plugin and theme available to date. The concept of hooks can also be somewhat daunting for users who are starting out with developing for WordPress. Today, we’ll jump in and find out a bit more about just what exactly WordPress hooks are and how they can help you on your way to WordPress rock stardom.

What exactly *are* WordPress hooks anyways?

WordPress hooks are, essentially, triggers of sorts that allow users to, with short snippets of code, modify areas a WordPress theme or plugin, or add their own code to various parts of WordPress without modifying the original files. An example of this could be along the lines of either “when WordPress chooses which template file to load, run our custom code” or “when you generate the content for each post, add social bookmarking links to the end of the content”. These examples will be expanded upon once we’re a bit more familiar with what exactly the different types of hooks are.

Hooks can be divided into “Action” and “Filter” hooks, the former allowing for insertion of custom code at various points (not unlike events in JavaScript) and the latter allowing for the manipulation of various bits of content (for example, the content of a page or blog post). Lets take a closer look at each of these, shall we?
Action Hooks

Action hooks are designated points in the WordPress core, theme and plugin code where it is possible for outside resources (outside of the scope of where the hook is… either in the core, theme or plugin) to insert additional code and, there by, customise the code to do additional functions they may desire. An example of this is the commonly used wp_head action hook, used by many themes and plugins to inject additional CSS stylesheets, processing code or anything else they require to sit between theandtags of their WordPress theme’s XHTML structure. This is the reason for including wp_head(); in all WordPress themes.

To hook on to an action, create a function in your theme’s functions.php file (or in your plugin’s code) and hook it on using the add_action() function, as follows:

	add_action( 'wp_head', 'wpcandy_actionhook_example' );

	function wpcandy_actionhook_example () {

		echo '<meta name="description" content="This is the meta description for this page." />' . "

	} // End wpcandy_actionhook_example()

The above code adds the text “Hello WPCandy Readers!” between your theme’stags. Placing “wp_head” in the call to add_action() with “get_header” would display this text above your theme.

The way I like to explain action hooks, in a single sentence, is: “When you get to this point, do that.”

Filter Hooks

Filter hooks are used to manipulate output. An example of this would be to add a line or text (or a hyperlink, or a signature sign-off—whatever you’d like) to the end of the content of each of your blog posts. Filter hooks can also be used for truncating text, changing formatting of content, or just about any other programming manipulation requirement (for example, adding to or overriding an array of values).

Custom code is added as a filter using the add_filter() function. The following code adds a sign-off to the end of each blog post, only when viewing the full blog post screen:

	add_filter( 'the_content', 'wpcandy_filterhook_signoff' );

	function wpcandy_filterhook_signoff ( $content ) {

		if ( is_single() ) {

			$content .= '
Th-th-th-th-th That\'s all, folks!
' . " "; } // End IF Statement return $content; } // End wpcandy_filterhook_signoff() ?>

The above code adds a new div tag to the end of the content of our blog post, only when on a single blog post screen.

How To Speed Up WordPress

As a side note, these are not ordered by importance or any criteria, I’ve just gathered everything I’ve learned about speeding up page loads on WordPress and compiled them here.

I guarantee that using even a few will help speed up your site.

1. Choose a good host
2. Start with a solid framework/theme
3. Use an effective caching plugin
4. Use a content delivery network (CDN)
5. Optimize images (automatically)
6. Optimize your homepage to load quickly
7. Optimize your WordPress database
8. Disable hotlinking and leeching of your content
9. Add an expires header to static resources
10. Adjust Gravatar images
11. Add LazyLoad to your images
12. Control the amount of post revisions stored
13. Turn off pingbacks and trackbacks
14. Replace PHP with static HTML, when necessary
15. Use CloudFlare

Using PHP Copy to move files from server to server.

You can just create a php file in the destination server and load the file once in your browser. For example you add this code in http://destination-url/copy-files.php and in copy-files.php you add this php code:


  Transfer Files Server to Server using PHP Copy



/* Source File URL */

$remote_file_url =http://origin-server-url/files.zip&#8221;


/* New file name and path for this file */

$local_file =files.zip”;

/* Copy the file from source url to server */

$copy = copy( $remote_file_url, $local_file );

/* Add notice for success/failure */

if( !$copy ) {

    echo “Doh! failed to copy $file…\n”;



    echo “WOOT! success to copy $file…\n”;


How to send email using PHPMailer

First, download PHPMailer using the direct link below:


After you have downloaded the file, unzip and extract it to your public_html. After unzipping the file we have public_html/PHPMailer_5.2.0. Next you will need to edit your web pages to use the PHPMailer code.

Add the PHPMailer code to your site:


$mail = new PHPMailer();

$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "mail.hostname.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "USERNAME"; // SMTP username
$mail->Password = "PASSWORD"; // SMTP password

$mail->From = "from@example.com";
$mail->FromName = "Mailer";
$mail->AddAddress("josh@example.net", "Josh Adams");
$mail->AddAddress("ellen@example.com"); // name is optional
$mail->AddReplyTo("info@example.com", "Information");

$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->AddAttachment("/var/tmp/file.tar.gz"); // add attachments
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name
$mail->IsHTML(true); // set email format to HTML

$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";

echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;

echo "Message has been sent";