Created
December 10, 2024 21:57
-
-
Save bhubbard/d27811cc90b1d22271eccff2f0411b2c to your computer and use it in GitHub Desktop.
The Deferred Hook Execution Pattern is useful when you want to delay the execution of a hook until a certain condition is met during the request lifecycle, such as when waiting for external data to load or deferring certain tasks until after the main action completes. This pattern prevents running resource-intensive operations too early and help…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Deferred Hook Execution Class | |
* | |
* Defers hook execution until a specified condition is met during the lifecycle of a WordPress request. | |
*/ | |
class Deferred_Hook_Execution { | |
/** | |
* The name of the WordPress hook. | |
* | |
* @var string | |
*/ | |
private $hook_name; | |
/** | |
* The callback function to execute. | |
* | |
* @var callable | |
*/ | |
private $callback; | |
/** | |
* The condition to check before executing the callback. | |
* | |
* @var callable | |
*/ | |
private $condition; | |
/** | |
* Constructor. | |
* | |
* @param string $hook_name The WordPress hook name. | |
* @param callable $callback The callback to execute. | |
* @param callable $condition The condition to check before executing the callback. | |
*/ | |
public function __construct( $hook_name, $callback, $condition ) { | |
$this->hook_name = $hook_name; | |
$this->callback = $callback; | |
$this->condition = $condition; | |
// Attach the check function to the hook. | |
add_action( $this->hook_name, [ $this, 'check_and_execute' ], 10, 2 ); | |
} | |
/** | |
* Checks the condition before executing the callback. | |
* | |
* @param mixed ...$args Arguments passed by the hook. | |
*/ | |
public function check_and_execute( ...$args ) { | |
if ( call_user_func( $this->condition ) ) { | |
call_user_func_array( $this->callback, $args ); | |
} | |
} | |
} | |
/** | |
* Example Use Case: Deferring Execution of Custom Function Until After Content is Loaded | |
*/ | |
// Deferred execution of a function to update post meta, only after certain condition is met. | |
new Deferred_Hook_Execution( | |
'save_post', // WordPress hook | |
function( $post_id ) { | |
// Custom logic to run after the post is saved | |
if ( ! wp_is_post_revision( $post_id ) ) { | |
update_post_meta( $post_id, '_custom_meta', 'Some value' ); | |
} | |
}, | |
function() { | |
// Condition to defer execution: Only run after a specific custom post type is queried | |
return is_singular( 'custom_post_type' ); | |
} | |
); | |
// Deferred execution of an admin function after the page is fully loaded. | |
new Deferred_Hook_Execution( | |
'admin_init', // WordPress hook | |
function() { | |
// Custom admin functionality | |
error_log( 'Admin panel is now fully loaded and ready!' ); | |
}, | |
function() { | |
// Condition to defer execution: Only run after an admin page is loaded | |
return isset( $_GET['page'] ) && $_GET['page'] === 'my_plugin_page'; | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How It Works | |
1. Initialization: | |
• The Deferred_Hook_Execution class binds a callback to a WordPress hook and allows a condition to be checked before executing the callback. | |
2. Condition Check: | |
• The condition provided in the constructor is evaluated before the callback executes. | |
• If the condition is met (e.g., a specific post type is being queried, an admin page is loaded), the callback is executed. | |
3. Use Cases: | |
• Post Meta Updates: You might need to update post meta only when a custom post type is being viewed or saved. | |
• Admin Panel Initialization: You can delay execution of certain actions (like logging or custom admin logic) until the page is fully loaded and certain URL parameters are set. | |
# Practical Use Cases | |
1. Defer Post Meta Updates: If you want to defer updating post meta values only after a custom post type is saved. | |
2. Delay Admin-Only Functions: If you want to execute admin-specific functions only when certain conditions are met (e.g., on certain admin pages or after user settings are loaded). | |
3. Optimize Resource-Intensive Operations: Defer expensive operations, such as external API calls or heavy processing, until after the page is rendered, ensuring it doesn’t slow down page load times. | |
This pattern allows you to fine-tune when specific hooks are executed, improving performance and user experience. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment