Action
uncanny-automator-pro
automator_magic_button_action
Fires when the magic button action is triggered, passing the trigger and user IDs for custom processing.
add_action( 'automator_magic_button_action', $callback, 10, 2 );
Description
Fires when the Uncanny Automator Magic Button is clicked. Developers can use this hook to execute custom actions or modify the default behavior triggered by the Magic Button. It receives the trigger ID and the current user ID as parameters. Use this for custom logic tied to Magic Button interactions.
Usage
add_action( 'automator_magic_button_action', 'your_function_name', 10, 2 );
Parameters
-
$trigger_id(mixed) - The `$trigger_id` parameter represents the unique identifier of the trigger that initiated this magic button action.
-
$user_id(mixed) - This parameter contains the ID of the trigger that initiated the magic button action.
Examples
add_action( 'automator_magic_button_action', 'my_custom_magic_button_handler', 10, 2 );
/**
* Handles the automator_magic_button_action hook to perform custom actions.
*
* This function is triggered when the Uncanny Automator Magic Button is clicked.
* It receives the trigger ID and the user ID, and can be used to execute
* custom logic based on these parameters.
*
* @param int $trigger_id The ID of the trigger that was activated.
* @param int $user_id The ID of the user who activated the trigger.
*/
function my_custom_magic_button_handler( $trigger_id, $user_id ) {
// Log the event for debugging purposes
error_log( "Magic button action triggered for Trigger ID: {$trigger_id} by User ID: {$user_id}" );
// Example: If a specific trigger ID is activated, perform a custom task.
if ( 123 === $trigger_id ) {
// Perform a custom action for trigger ID 123
// For example, send a notification to an administrator
$user_info = get_userdata( $user_id );
if ( $user_info ) {
$admin_email = get_option( 'admin_email' );
$subject = "Magic Button Activated: Trigger {$trigger_id} by {$user_info->display_name}";
$message = "The Magic Button was activated for trigger ID {$trigger_id} by user: {$user_info->display_name} ({$user_info->user_email}).";
wp_mail( $admin_email, $subject, $message );
error_log( "Custom notification sent for Trigger ID: {$trigger_id}" );
}
}
// Example: Add some metadata to the user if a certain condition is met
if ( 456 === $trigger_id && $user_id > 0 ) {
update_user_meta( $user_id, 'magic_button_activated_trigger_456', current_time( 'mysql' ) );
error_log( "User meta updated for User ID: {$user_id} and Trigger ID: {$trigger_id}" );
}
// You can add more complex logic here based on $trigger_id and $user_id
// For instance, you could fetch associated automator data, interact with other plugins, etc.
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
uncanny-automator-pro/src/core/classes/magic-button.php:159
uncanny-automator-pro/src/core/classes/magic-button.php:345
public static function automator_magic_button_action() {
if ( 'init' !== current_action() ) {
_doing_it_wrong(
esc_html( __FUNCTION__ ),
'This function should only be called from the init action.',
esc_html( AUTOMATOR_PRO_PLUGIN_VERSION )
);
return;
}
$nonce = automator_filter_input( 'automator_nonce', INPUT_POST );
$action = automator_filter_input( 'action', INPUT_POST );
// Check if the action is being called by the magic button action.
if ( 'automator_button_action' !== $action ) {
return;
}
// Check for the nonce for security
if ( ! wp_verify_nonce( $nonce, AUTOMATOR_PRO_ITEM_NAME ) ) {
wp_die( 'Invalid nonce' );
}
$trigger_id = automator_filter_input( 'automator_trigger_id', INPUT_POST );
$user_id = get_current_user_id();
// Perform the action hook
do_action( 'automator_magic_button_action', absint( $trigger_id ), absint( $user_id ) );
// If the request is an AJAX request, send a JSON response
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
wp_send_json_success(
array(
'message' => 'Triggered successfully',
'trigger_id' => $trigger_id,
)
);
}
}