Introduction
WordPress and WooCommerce hooks provide developers with powerful tools for extending and customizing themes. Hooks enable you to add, remove, or modify functionality without altering the core code, ensuring compatibility and ease updates.
In this comprehensive guide, we'll explore the various hooks available in WordPress and WooCommerce, demonstrating how to leverage them for advanced the customization.
What are Hooks?
Hooks are mechanisms in WordPress and WooCommerce that allow developers to inject code at specific points during execution.
There are two types of hooks:
- Actions: Allow you to add or change WordPress behavior by triggering functions at specific points.
- Filters: Enable you to modify data before it is processed or outputted.
WordPress Hooks - list of all hooks
Action Hooks
wp_head
- Fires in the<head>
section of the document.wp_footer
- Fires before the closing</body>
tag.init
- Runs after WordPress has finished loading but before any headers are sent.wp_enqueue_scripts
- Used to enqueue styles and scripts on the frontend.admin_enqueue_scripts
- Enqueue scripts/styles in the admin dashboard.login_init
- Fires when the login page is accessed.user_register
- Fires when a user registers.save_post
- Fires when a post is saved or updated.publish_post
- Fires when a post is published.comment_post
- Fires when a comment is posted.template_redirect
- Fires before WordPress determines which template to load.admin_menu
- Fires before the admin menu is displayed.wp_logout
- Fires when a user logs out.wp_login
- Fires when a user logs in.
Filter Hooks
the_content
- Filters the content of a post or page.the_title
- Filters the title of a post or page.excerpt_length
- Filters the excerpt length.excerpt_more
- Filters the "more" text for excerpts.get_avatar
- Filters the avatar HTML output.widget_text
- Filters the content of text widgets.wp_mail_from
- Filters the 'from' email address for outgoing emails.wp_mail_from_name
- Filters the 'from' name for outgoing emails.
WooCommerce Hooks
Action Hooks
woocommerce_before_main_content
- Fires before the main content area on product pages.woocommerce_after_main_content
- Fires after the main content area on product pages.woocommerce_before_shop_loop
- Fires before the start of the shop loop.woocommerce_after_shop_loop
- Fires after the end of the shop loop.woocommerce_before_single_product
- Fires before the single product page.woocommerce_after_single_product
- Fires after the single product page.woocommerce_before_cart
- Fires before the cart page.woocommerce_after_cart
- Fires after the cart page.woocommerce_before_checkout_form
- Fires before the checkout form is displayed.woocommerce_after_checkout_form
- Fires after the checkout form is displayed.woocommerce_before_checkout_payment
- Fires before the checkout payment section.woocommerce_after_checkout_payment
- Fires after the checkout payment section.woocommerce_add_to_cart
- Fires when a product is added to the cart.woocommerce_order_status_changed
- Fires when an order status changes.woocommerce_payment_complete
- Fires when an order is marked as paid.woocommerce_order_received
- Fires after the customer has received the order.
Filter Hooks
woocommerce_product_title
- Filters the product title.woocommerce_product_price
- Filters the product price.woocommerce_order_item_name
- Filters the name of a product in an order.woocommerce_cart_item_name
- Filters the name of a product in the cart.woocommerce_cart_item_price
- Filters the price of an item in the cart.woocommerce_checkout_fields
- Filters the fields on the checkout page.woocommerce_email_subject_new_order
- Filters the email subject when a new order is placed.woocommerce_email_body_new_order
- Filters the email body when a new order is placed.woocommerce_email_from_name
- Filters the "from" name in WooCommerce emails.
These are some of the most commonly used hooks, but there are many more available within both WordPress and WooCommerce. For full documentation, you can always refer to WordPress Codex and WooCommerce Codex.
WordPress Hooks - examples
Action Hooks
Action hooks allow you to add custom functions to WordPress at specific execution points.
Common WordPress Action Hooks
-
init
- Description: Runs after WordPress has finished loading but before any headers are sent.
- Usage:
1function custom_init_function() {2 // Custom code here3}4add_action('init', 'custom_init_function');
-
wp_enqueue_scripts
- Description: Used to enqueue scripts and styles in the frontend. - Usage:1function enqueue_custom_scripts() {2wp_enqueue_style('custom-style', get_template_directory_uri() . '/css/custom-style.css');34wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), null, true);5 }67add_action('wp_enqueue_scripts', 'enqueue_custom_scripts'); -
widgets_init
- Description: Fires when loading the widgets for the sidebar.
- Usage:
1function register_custom_widget() {2 register_sidebar(array(3 'name' => 'Custom Widget Area',4 'id' => 'custom_widget_area',5 'before_widget' => '<div>',6 'after_widget' => '</div>',7 'before_title' => '<h2>',8 'after_title' => '</h2>',9 ));10}11add_action('widgets_init', 'register_custom_widget');
-
after_setup_theme
- Description: Runs after the theme is initialized.
- Usage:
1function custom_theme_setup() {2 add_theme_support('post-thumbnails');3 add_theme_support('custom-logo');4}5add_action('after_setup_theme', 'custom_theme_setup');
Filter Hooks
Filter hooks allow you to modify data before it is used or displayed.
Common WordPress Filter Hooks
the_content
- Description: Filters the post content.
- Usage:
1function add_custom_content($content) {2 if (is_single()) {3 $content .= '<p>Thank you for reading!</p>';4 }5 return $content;6}7add_filter('the_content', 'add_custom_content');
excerpt_length
- Description: Filters the length of the excerpt.
- Usage:
1function custom_excerpt_length($length) {2 return 25;3}4add_filter('excerpt_length', 'custom_excerpt_length');
excerpt_more
- Description: Filters the "read more" string in excerpts.
- Usage:
1function custom_excerpt_more($more) {2 return '... <a href="' . get_permalink() . '">Read More</a>';3}4add_filter('excerpt_more', 'custom_excerpt_more');
wp_nav_menu_args
- Description: Filters the arguments used to display a navigation menu.
- Usage:
1function custom_nav_menu_args($args) {2 $args['container'] = 'nav';3 return $args;4}5add_filter('wp_nav_menu_args', 'custom_nav_menu_args');
WooCommerce Hooks - examples
WooCommerce extends WordPress with eCommerce functionality and comes with its own set of hooks for customization.
Action Hooks
woocommerce_before_main_content
- Description: Fired before the main content on WooCommerce pages. - Usage:
1function custom_before_main_content() {2 echo '<div class="custom-wrapper">';3}4
5add_action('woocommerce_before_main_content', 'custom_before_main_content');
woocommerce_after_main_content
- Description: Fired after the main content on WooCommerce pages.
- Usage:
1function custom_after_main_content() {2 echo '</div>';3}4add_action('woocommerce_after_main_content', 'custom_after_main_content')
woocommerce_single_product_summary
- Description: Fired within the single product summary. - Usage:
1function custom_single_product_summary() {2 echo '<p>Custom message on single product page.</p>';3}4add_action('woocommerce_single_product_summary', 'custom_single_product_summary', 25);
woocommerce_before_shop_loop
- Description: Fired before the product loop in the shop page.
- Usage:
1function custom_before_shop_loop() {2 echo '<h2>Welcome to Our Shop!</h2>';3}4add_action('woocommerce_before_shop_loop', 'custom_before_shop_loop');
Filter Hooks
-
woocommerce_product_tabs
- Description: Filters the product tabs on the single product page.
- Usage:
1function custom_product_tabs($tabs) {2 $tabs['custom_tab'] = array(3 'title' => __('Custom Tab', 'textdomain'),4 'priority' => 50,5 'callback' => 'custom_product_tab_content'6 );7 return $tabs;8}9add_filter('woocommerce_product_tabs', 'custom_product_tabs');10
11function custom_product_tab_content() {12 echo '<h2>Custom Tab Content</h2>';13 echo '<p>Here is some custom content for this tab.</p>';14}
woocommerce_add_to_cart_fragments
- Description: Filters the fragments to refresh after AJAX calls. - Usage:
1function custom_add_to_cart_fragment($fragments) {2 ob_start();3 ?>4 <span class="custom-cart-count"><?php echo5WC()->cart->get_cart_contents_count(); ?></span>6 <?php7 $fragments['span.custom-cart-count'] = ob_get_clean();8 return $fragments;9}10add_filter('woocommerce_add_to_cart_fragments',11'custom_add_to_cart_fragment');
woocommerce_checkout_fields
- Description: Filters the checkout fields. - Usage:
1function custom_checkout_fields($fields) {2 $fields['billing']['billing_phone']['placeholder'] = 'Your Contact3Number';4 return $fields;5}6add_filter('woocommerce_checkout_fields', 'custom_checkout_fields');
woocommerce_product_get_price
- Description: Filters the product price.
- Usage:
1function custom_product_price($price, $product) {2 // Apply a 10% discount3 $discounted_price = $price * 0.9;4 return $discounted_price;5}6add_filter('woocommerce_product_get_price', 'custom_product_price', 10, 2);
Advanced Theme Customization with Hooks
Removing Default WooCommerce Actions
Sometimes, you might want to remove default WooCommerce actions to customize t layout.
1function custom_remove_wc_actions() {2 remove_action('woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);3 remove_action('woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);4}5add_action('init', 'custom_remove_wc_actions');
Adding Custom Content to WooCommerce Pages
You can add custom content using action hooks at various points.
1function custom_add_banner() {2 echo '<div class="custom-banner">Welcome to Our Store!</div>';3 }4 add_action('woocommerce_before_shop_loop', 'custom_add_banner');
Modifying Product Display
Customize how products are displayed using hooks.
1function custom_product_thumbnail() {2 echo '<span class="custom-thumbnail">New!</span>';3 }4 add_action('woocommerce_before_shop_loop_item_title', 'custom_product_thumbnail', 15);
Customizing the Checkout Process
Use hooks to streamline or enhance the checkout process.
1function custom_checkout_message() {2 echo '<p>Thank you for shopping with us!</p>';3 }4 add_action('woocommerce_checkout_before_customer_details', 'custom_checkout_message');
Best Practices for Using Hooks
• Use Child Themes: Always make changes in a child theme to prevent losing
customizations during theme updates.
• Prioritize Performance: Limit the number of hooks and ensure that callback
functions are optimized.
• Maintain Clean Code: Keep your hook-related code organized, possibly in
separate files or documented sections.
• Test Thoroughly: Always test changes in a staging environment before
deploying to production.
Conclusion
Hooks are indispensable tools for WordPress and WooCommerce developers seeking
to customize themes beyond the basics. By leveraging the extensive array of
action and filter hooks available, you can create highly tailored and efficient
themes that meet your specific requirements.
For a complete list of available hooks, refer to the WordPress Codex and WooCommerce Codex hooks documentation.
Comments
You must be logged in to comment.
Loading comments...