How to use Actions and Filters to customize Simply Static
WordPress hooks (actions and filters) are fundamental components of the WordPress architecture that allow developers to modify or extend the functionality of a WordPress site without directly modifying the core code. Hooks enable customization and flexibility in themes and plugins, allowing developers to add, modify, or remove functionality in a modular and organized manner.
Please note: The Simply Static team is not able to provide support for custom code added to your site that modifies the behavior of Simply Static.
Hooks
Hooks in WordPress are specific points in the execution of code where developers can attach their custom functions or code. There are two types of hooks: action hooks and filter hooks.
Actions
Action hooks allow you to execute custom code at specific points in the WordPress execution process, such as when a post is published or when a theme is loaded. Developers can use the hooks available in Simply Static to trigger actions to execute custom code. Common actions include wp_head, wp_footer, init, save_post, etc.
Example of a WordPress Action hook:
/* Action hook */ add_action('wp_head', 'custom_action_callback'); /* Your Filter callback function */ function custom_action_callback() { // Your custom code here }
More information about WordPress Actions: https://developer.wordpress.org/plugins/hooks/actions/
Filters
Filters allow developers to modify the output or behavior of functions, variables, or content. They provide a way to manipulate content or variables before it is displayed or processed.
Example of a WordPress Filter hook:
/* Filter hook */ add_filter('the_title', 'custom_filter_callback'); /* Your Action callback function */ function custom_filter_callback($title) { // Modify $title here return $title; }
More information about WordPress Filters: https://developer.wordpress.org/plugins/hooks/filters/
Here's a general guide on how to add an Filter (or Action) to your site:
1. Identify the Hook: Determine which WordPress hook you want to filter. Hooks are predefined places in WordPress where you can add your own code. Common hooks include actions and filters. For filters, you'll be looking for functions prefixed with apply_filters()
.
2. Create a Function: Write a PHP function that will be applied to the filter hook. This function will take the data provided by the hook, modify it as needed, and return the modified data. Use the add_filter()
function to attach your custom function to the filter hook.
4. Add the Code to WordPress:
(Option 1) Place Code in the Child Theme's functions.php: Open the functions.php
file in your child theme directory (create it if it doesn't exist), and add your filter code there. Here is a link to more information about what a child theme is: https://developer.wordpress.org/themes/advanced-topics/child-themes/
(Option 2) Add the code using a plugin: You can also add custom code to your WordPress site using a plugin like Code Snippets or WP Code.
- Link to more information about Code Snippets: https://wordpress.org/plugins/code-snippets/
- Link to more information about WP Code: https://wordpress.org/plugins/insert-headers-and-footers/
Example Filter code to modify the frontend URL used by Simply Static:
Let's say you want to modify the frontend URL that Simply Static uses to parse your WordPress website. This can be useful if your WordPress admin URL is different than the frontend URL.
php // Step 2: Create a Function function my_custom_origin_url($url) { $url = 'https://mywebsite.com'; // Change to the desired URL return $url; } // Step 3: Add the Filter add_filter('ss_origin_url', 'my_custom_origin_url');
Place this code in your child theme's functions.php
file. Now, the URL Simply Static will use to export a static version of your website will be changed.
Remember, the specifics of your filter function will depend on what you're trying to achieve. The key is to identify the appropriate hook and craft your function accordingly.