Turning Tricks: Using Action and Filter Hooks in WordPress


I spoke at the fantastic WordPress NYC Meetup tonight on the topic of Hooks in WordPress.

The video should be up shortly, but here are my slides. (download links below)

<?php // admin-after-title.php
  add_action( 'edit_form_after_title', 'myprefix_edit_form_after_title' );
  function myprefix_edit_form_after_title() {
     echo 'Don't forget to save!';
 }
<?php // CUSTOM ADMIN MENU LINK FOR ALL SETTINGS
function all_settings_link() {
	add_options_page(__('All Settings'), __('All Settings'), 'administrator', 'options.php');
}
add_action('admin_menu', 'all_settings_link');
<?php  // async wp
function MY_CRON(){
	wp_schedule_single_event(time(), 'MY_ACTION');
}
add_action('save_post', 'MY_CRON');
 
function MY_FUNCTION(){
  // YOUR CODE HERE
}
add_action('MY_ACTION', 'MY_FUNCTION');
<?php // Replaces the login header logo URL 
add_filter( 'login_headerurl', 'namespace_login_headerurl' );
function namespace_login_headerurl( $url ) {
    $url = home_url( '/' );
    return $url;
}

// Replaces the login header logo title
add_filter( 'login_headertitle', 'namespace_login_headertitle' );
function namespace_login_headertitle( $title ) {
    $title = get_bloginfo( 'name' );
    return $title;
}

// Replaces the login header logo
add_action( 'login_head', 'namespace_login_style' );
function namespace_login_style() {
    echo '<style>.login h1 a { background-image: url( ' . get_template_directory_uri() . '/images/Inkwell-Logo.png ) !important; background-size: 271px 202px; height: 150px; }</style>';
}
<?php // browser-body-class.php
add_filter('body_class','browser_body_class');
function browser_body_class($classes) {
	global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;

	if($is_lynx) $classes[] = 'lynx';
	elseif($is_gecko) $classes[] = 'gecko';
	elseif($is_opera) $classes[] = 'opera';
	elseif($is_NS4) $classes[] = 'ns4';
	elseif($is_safari) $classes[] = 'safari';
	elseif($is_chrome) $classes[] = 'chrome';
	elseif($is_IE) $classes[] = 'ie';
	else $classes[] = 'unknown';

	if($is_iphone) $classes[] = 'iphone';
	return $classes;
}
<?php // adds GA code in footer
add_action('open_body', 'add_ga_code');	
function add_ga_code(){ ?>
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-41027480-1', 'jackreichert.com');
  ga('send', 'pageview');

</script>
<?php
}
<?php // adds GA code in footer
add_action('wp_footer', 'add_ga_code');	
function add_ga_code(){ ?>
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-41027480-1', 'jackreichert.com');
  ga('send', 'pageview');

</script>
<?php
}
<?php // notify-via-email.php
add_action( 'publish_post', 'notify_via_email' );
function notify_via_email( $post_id ) {  
    $post = get_post( $post_id );  
    $to = 'example@example.org';      
  
    $subject = 'Post Published on ' . get_bloginfo( 'name' );  
    $message = $post->post_title . ' was published on ' . get_bloginfo( 'name' ) . ' as of ' . $post->post_date . '. by '.get_the_author_meta('display_name', $post->post_author).' You may view it at ' . get_permalink( $post_id ) . '.';  
  
    wp_mail( $to, $subject, $message );  
}