10 Useful WordPress Hook Hacks

7. Prevent Duplicate Content On Comment Pages

The problem.
Duplicate content is an SEO problem for many websites. It can also be a problem for your WordPress blog if you don’t apply a few tricks.

Introduced in version 2.7, paged comments is a great addition to WordPress because it lets bloggers split comments into multiple pages. The only problem, again, is the risk of duplicate content.

Let’s use the new rel="canonical" attribute to prevent duplicate content in paged comments.

The solution.
Simply paste the following code in your function.php file:

function canonical_for_comments() {
	global $cpage, $post;
	if ( $cpage > 1 ) :
		echo "\n";
	  	echo "<link rel='canonical' href='";
	  	echo get_permalink( $post->ID );
	  	echo "' />\n";
	endif;
}

add_action( 'wp_head', 'canonical_for_comments' );

Code explanation.
First, we create a function to add the rel="canonical" attribute to comment pages, except page 1. Then we hook this function to WordPress’ wp_head() function. As simple as that!

Sources:

8. Get Entire Post Or Page In A PHP Variable

The problem.
Being able to get the entire current post or page as a PHP variable is definitely a good thing. You could, for example, replace parts of the content using the str_replace() PHP function, and much more.

The solution.
Once again, nothing hard. Just paste the following in your function.php file.

function callback($buffer) {
	// modify buffer here, and then return the updated code
	return $buffer;
}

function buffer_start() {
	ob_start("callback");
}

function buffer_end() {
	ob_end_flush();
}

add_action('wp_head', 'buffer_start');
add_action('wp_footer', 'buffer_end');

Code explanation.
To achieve this hack, we need three functions:

  • callback(): this function returns the whole page as a variable called $buffer. You can modify it before returning it by using regular expressions, for example.
  • buffer_start(): this function simply starts the buffer. It is hooked to WordPress’ wp_head() function.
  • buffer_end(): this function clears the buffer. It is hooked to WordPress’ wp_footer() function.

Once we have our function, we just hook it to existing WordPress functions.

Sources:

9. Use Hooks And Cron To Schedule Events

The problem.
You probably already know that WordPress can schedule events. For example, it can publish posts at preset dates and times. Using hooks and wp-cron, we can easily schedule our own events. In this example, we will get our WordPress blog to send us an email once hourly.

The solution.
Paste the following code block in the functions.php file of your theme.

if (!wp_next_scheduled('my_task_hook')) {
	wp_schedule_event( time(), 'hourly', 'my_task_hook' );
}

add_action( 'my_task_hook', 'my_task_function' ); 

function my_task_function() {
	wp_mail('[email protected]', 'Automatic email', 'Hello, this is an automatically scheduled email from WordPress.');
}

Code explanation.
The first thing we did, of course, was create a function that performs the desired action. In this example, the function is called my_task_function() and it just sends an email to the specified email address.

To schedule an event, we have to use the wp_schedule_event() function. The last argument has to be a hook, which is why we hook our my_task_function() function to my_task_hook.

Sources:

10. List All Hooked Functions

The problem.
When things go wrong, listing all hooked functions can be very useful for debugging.

The solution.
As with the others, this code has to be pasted in your functions.php file. When you have finished debugging, don’t forget to remove the code from functions.php, or else the debugging message will continue to appear.

function list_hooked_functions($tag=false){
 global $wp_filter;
 if ($tag) {
  $hook[$tag]=$wp_filter[$tag];
  if (!is_array($hook[$tag])) {
  trigger_error("Nothing found for '$tag' hook", E_USER_WARNING);
  return;
  }
 }
 else {
  $hook=$wp_filter;
  ksort($hook);
 }
 echo '<pre>';
 foreach($hook as $tag => $priority){
  echo "<br />&gt;&gt;&gt;&gt;&gt;\t<strong>$tag</strong><br />";
  ksort($priority);
  foreach($priority as $priority => $function){
  echo $priority;
  foreach($function as $name => $properties) echo "\t$name<br />";
  }
 }
 echo '</pre>';
 return;
}

Once that’s done, simply call the list_hooked_functions() function, as shown below, to print all hooked WordPress functions on your screen.

list_hooked_functions();

Code explanation.
If no hook name is provided to the function as an argument, then hooks are printed to the screen using the global variable $wp_filter. Alternatively, you can list one particular hook by passing its name as an attribute:

list_hooked_functions('wp_head');

Sources:

Related posts

You may be interested in the following related posts:

-->