Actions and Filters and Mail Hooks and Shopping Trips

My notes from Robert Gillmer’s presentation at WordCamp LA 2018

Presentation by Robert Gillmer On twitter @RobertFGillmer

Hooks are one thing which intermediate programmers struggle with the most. Mastering actions and filters will elevate intermediate developers to advanced ones. However, much of the documentation about hooks are hard to understand, and the standard examples are insufficient. I’m a big fan of putting a different spin on lectures and using analogies which no one else has in order to make things more understandable. I explain the difference between the two by drawing a parallel to a shopping trip. Filters are when my wife calls me to ask me to pick something new up from the store I’m going to; actions are when she asks me to go to a completely different store. I then go into how to use hooks from a developer side – add_action and add_filter – as well as how to use them from a “builder” side – apply_filters and do_action.

Presentation Abstract

Hooks

Actions

Actions | Plugin Developer Handbook | WordPress Developer Resources

<h1><?php the_title(); ?></h1>
<?php do_action( ‘ put_stuff_after_the_title’  ); ?>
<? 
$title = get_the_title();
apply_filters( ‘ i_can_change_the_title’, $title );
apply_filters( $title, ‘i_can_change_the_title’);
?>

Using filters

Common filters Changing […] to something more useful

function rfg_read_more_link( $more ) {
	return ‘<a class=“read-more-link” href=“‘ . get_permalink() . ‘“>Read more</a>;
}

add_filter( ’the_content_more_link’, ‘rgf_read_more_link’ );

Changing the number of characters in an excerpt

function rfg_excerpt_length{
	return 20;
}

add_filter( ‘excerpt_length’ , ’rfg_excerpt_length’ );

Add text with a css class name

function rfg_add_label_with_filter( $post_title ) {
$post_title .= ‘<span class=“editorial-label”>Editorial</span>;

return $post_title;
}

add_filter( ‘genesis_post_title’, rfg_add_label_with_filter’ );

Functions involved

Programmer side

  • add_action()
  • remove_action()
  • add_filter()

Builder side

  • do_action()
  • apply_filters()

Theme agnostic plugin to show hooks

Tags:


← Previous Gutenberg Q&A Panel GDPR Compliance. What is it & how does it affect you? Next →