5. “…huh?”
• WordPress ships with a simple plugin named
“Hello Dolly”
• Its name is a whimsical take on the
programmer's obligatory “Hello, World!”
• It’s trotted out only for pedantic programming
explanations like the one that you’re about to sit
through…
13. WordPress Hooks
• A “hook” is a WordPress Event
• Hooks are provided by WordPress to allow
your plugin to “hook into” the rest of
WordPress; that is, to call functions in your
plugin at specific times, and thereby set your
plugin in motion.
14.
15.
16. WordPress Hooks
• As a WordPress post is loaded, each hook is
run in a specific order
• There are two types of hooks:
• Actions – adds functionality
• Filters – changes functionality
17. WordPress Actions
We just saw two “Actions” in action:
• add_action( 'admin_notices', 'hello_dolly' );
• add_action( 'admin_head', 'dolly_css' );
19. WordPress Actions
What if we ran them together?
• add_action( 'admin_notices', 'hello_dolly' );
• add_action( 'admin_footer', 'hello_dolly' );
Would the output be the same?
22. WordPress Filters
Let’s edit the hello_dolly() function so it accepts
an input, and returns the input plus the lyric:
function hello_dolly($input) {
$chosen = hello_dolly_get_lyric();
return $input . "<p id='dolly'>$chosen</p>”;
}
24. Real World Examples
Register a Custom Menu in the Admin
function register_my_custom_menu_page() {
add_menu_page( 'custom menu title', 'custom menu',
'manage_options', 'myplugin/myplugin-admin.php', '', 'dashicons-admin-
site', 6 );
}
add_action( 'admin_menu', 'register_my_custom_menu_page' );
25. Real World Examples
Hook into Post Publishing
function publish_post_tweet($post_ID) {
global $post;
// Code to send a tweet with post info
}
add_action('publish_post', 'publish_post_tweet');
26. Real World Examples
Hook Into Widget Initialization
function create_my_widget() {
register_sidebar(array(
'name' => __( 'My Sidebar', 'mytheme' ),
'id' => 'my_sidebar',
'description' => __( 'The one and only', 'mytheme' ),
));
}
add_action( 'widgets_init', 'create_my_widget' );
27. Real World Examples
Hook Into Front-end Scripts and Styles
function theme_styles() {
wp_enqueue_style( 'bootstrap_css', get_template_directory_uri() . '/css/bootstrap.min.css' );
wp_enqueue_style( 'main_css', get_template_directory_uri() . '/style.css' );
wp_enqueue_script( 'bootstrap_js', get_template_directory_uri() . '/js/bootstrap.min.js',
array('jquery'), '', true );
wp_enqueue_script( 'theme_js', get_template_directory_uri() . '/js/theme.js', array('jquery',
'bootstrap_js'), '', true );
}
add_action( 'wp_enqueue_scripts', 'theme_styles' );
28. WordPress Wishlist?
Have you ever thought to yourself “This WordPress
thing is pretty good, but it would be amazing if it
did ‘x’…”