Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use get_template_part_slug action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
July 5, 2023
5 minutes read

So, you’re diving into the world of WordPress hooks, eh? Well, you’ve come to the right place! Let’s chat about the get_template_part_slug action. This nifty hook allows you to modify the way template parts are loaded in your WordPress theme. The dynamic portion of the hook name, $slug, refers to the slug name for the generic template part. It’s like having a secret key to customize your theme’s template behavior. Now, let’s explore how to use this hook with some live examples.

Example 1: Basic Hook Usage

To use the get_template_part_slug action, you first need to register it using add_action. This can be done in the functions.php of your theme or in a custom WordPress Plugin, which is always a safer bet. Here’s a basic example:

function weplugins_execute_on_get_template_part_slug_event($slug, $name, $args){
   // Your custom code here
}
// Add the action
add_action( "get_template_part_slug", "weplugins_execute_on_get_template_part_slug_event", 10, 3 );

Example 2: Removing a Hook

Sometimes, you need to remove a registered hook. This can be done using remove_action. Here’s how you can remove the get_template_part_slug action:

remove_action( "get_template_part_slug", "weplugins_execute_on_get_template_part_slug_event", 10, 3 );

Ensure you provide the same callback function name, priority, and number of arguments while removing the hook callback.

Example 3: Dynamic Hook with Parameters

Here’s a more advanced example where we pass multiple parameters to the hook:

function weplugins_execute_on_get_template_part_slug_event($slug, $name, $args){
   // Your custom code that uses the parameters
   // $slug: The slug name for the generic template
   // $name: The name of the specialized template
   // $args: Additional arguments passed to the template
}
// Add the action
add_action( "get_template_part_{$slug}", "weplugins_execute_on_get_template_part_slug_event", 10, 3 );

Below are the parameters required to use this hook:

  • $slug: (string) The slug name for the generic template.
  • $name: (string|null) The name of the specialized template.
  • $args: (array) Additional arguments passed to the template.

Access Premium WordPress Plugins

Need some customization or facing issues implementing this hook? Reach out to us for assistance at Contact Us.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.