Hooks: Actions and Filters
Posted on June 24, 2018 in WordPress by Matt Jennings
Hooks
Hooks are functions in WordPress that allow me to call others functions I create at specific times. There are two types of hooks in WordPress:
Why are Hooks Needed
- So I don’t mess with the WordPress core.
- So I modify/add functionality to the WordPress core.
Action
A WordPress function that is executed at specific points throughout the WordPress core.
Generic Action add_actions();
add_action($tag, $function_to_add, $priority, $accepted_args);
More Info on add_action();
See more info on add_action();.
Example Actions
// Enable dashicons if the front end
function load_dashicons_front_end() {
wp_enqueue_style( 'dashicons' );
}
// Register dashicons in the front end
add_action( 'wp_enqueue_scripts', 'load_dashicons_front_end' );
// Registers custom shortcodes
function cttq_blockquote_register_shortcodes() {
add_shortcode('tweetquote', 'cttq_blockquote_shortcode');
}
function cttq_blockquote_shortcode( $attributes, $content = null ) {
// Save ech attribute's values to its own variable.
// Below are attribute values for hashtags and CSS styles
extract( shortcode_atts( array(
'hashtags' => '',
'nostyle' => esc_attr( get_option('cttq_style_option') )
), $attributes ) );
// Get full URL
global $wp;
$current_url = home_url(add_query_arg(array(),$wp->request));
// setup output variable
$output = '<a href="#" ' . (($nostyle === "No Style") ? '' : 'style="text-decoration: none;"') . ' onclick="window.open(\'https://twitter.com/intent/tweet?text=' . str_replace(' ', '%20',$content) . '%20' . str_replace(' ', '%20', str_replace('#', '%23', $hashtags)) . '%20' . str_replace('/', '%2F', str_replace(':', '%3A', $current_url)) . '\', \'_blank\', \'width=500,height=500\'); return false;">';
$output .= '<blockquote' . (($nostyle === "No Style") ? '' : ' style="display: table; width: 100%;"') .'>';
$output .= '<div' . (($nostyle === "No Style") ? '' : ' style="display: table-cell"') . '>';
$output .= '<span class="dashicons dashicons-format-quote"' . (($nostyle === "No Style" ) ? '' : 'style="color: #1da1f2; font-size: 45px; padding-right: 65px; display: block; float: left; height: 50px;"') . '></span>';
$output .= '</div>';
$output .= '<div>';
// If $content exists assign $output variable
if(strlen($content)):
$output .= '<p ' . (($nostyle === "No Style") ? '' : 'style="display: inline; padding-top: 10px; font-size: 25px; font-style: normal; color: #000;" onmouseover="this.style.color=\'#1da1f2\'" onmouseout="this.style.color=\'#000\'"') . '>' . $content . '</p>';
endif;
$output .= '<p ' . (($nostyle === "No Style") ? '' : 'style="color: #1da1f2; font-style: normal;"') . '><i class="dashicons dashicons-twitter"' . (($nostyle === "No Style") ? '' : 'style="color: #1da1f2; font-size: 25px; display: block; float: left; padding-right: 30px;"' ) .'></i>Click to Tweet</p>';
$output .= '</div>';
$output .= '</blockquote>';
$output .= '</a>';
// return our results/html
return $output;
}
// Registers all custom shortcodes on init
add_action('init', 'cttq_blockquote_register_shortcodes');
// Registers admin menu
add_action('admin_menu', 'cttq_admin_menu');
// register custom plugin admin menu
function cttq_admin_menu() {
/* main menu */
$top_menu_item = 'cttq_dashboard_admin_page';
add_menu_page('', 'Click to Tweet Quote', 'manage_options', $top_menu_item, $top_menu_item, 'dashicons-twitter');
}
// Registers admin menu
add_action('admin_menu', 'cttq_admin_menu');
Filters
Filters are WordPress functions that allow us to get and modify WordPress data before sending it to the database/browser using custom functions.
Generic add_filter();
add_filter($tag, $function_to_add, $priority, $accepted_args);
More Info on add_filter();
See more info on add_filter();.
Example Filter
function my_tiny_mce_before_init( $init_array ) {
$init_array['body_class'] = 'entry_content';
return $init_array;
}
add_filter('tiny_mce_before_init', 'my_tiny_mce_before_init');