Filter
uncanny-automator-pro
automator_pro_magic_link_html
Filter the HTML output of the magic link Filters the HTML output of the magic link before it's displayed, allowing for modifications to its appearance and attributes.
add_filter( 'automator_pro_magic_link_html', $callback, 10, 2 );
Description
Filter the generated HTML for Uncanny Automator's magic link shortcode. Developers can modify the `$link_html` output and access the original `$atts` array to customize the link's appearance and behavior before it's rendered on the page.
Usage
add_filter( 'automator_pro_magic_link_html', 'your_function_name', 10, 2 );
Parameters
-
$link_html(string) - The HTML output of the magic link
-
$atts(array) - The shortcode attributes
Return Value
string - The filtered HTML output of the magic link
Examples
// Example of how to hook into 'automator_pro_magic_link_html' to modify the magic link's HTML.
// This example adds a specific CSS class to the link if a certain attribute is present.
add_filter( 'automator_pro_magic_link_html', 'my_custom_magic_link_html', 10, 2 );
/**
* Modifies the magic link HTML by adding a custom CSS class based on shortcode attributes.
*
* @param string $link_html The original HTML output of the magic link.
* @param array $atts The attributes passed to the magic link shortcode.
*
* @return string The potentially modified HTML output of the magic link.
*/
function my_custom_magic_link_html( $link_html, $atts ) {
// Check if a specific attribute like 'highlight' is set to 'true'
if ( isset( $atts['highlight'] ) && $atts['highlight'] === 'true' ) {
// Use DOMDocument to parse and manipulate the HTML string.
// This is a robust way to modify HTML without breaking its structure.
$dom = new DOMDocument();
// Suppress warnings for malformed HTML, as we're only adding a class.
@$dom->loadHTML( $link_html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
// Find the anchor tag. Assuming the magic link is always an <a> tag.
$links = $dom->getElementsByTagName('a');
if ( $links->length > 0 ) {
$link_element = $links->item(0);
// Get the current class attribute or initialize it if it doesn't exist.
$current_classes = $link_element->getAttribute('class');
$new_classes = 'my-highlighted-magic-link';
// Append the new class, ensuring no duplicate classes are added.
if ( ! empty( $current_classes ) ) {
$class_array = explode( ' ', $current_classes );
if ( ! in_array( $new_classes, $class_array ) ) {
$new_classes = $current_classes . ' ' . $new_classes;
} else {
// If class already exists, don't add it again.
$new_classes = $current_classes;
}
}
$link_element->setAttribute('class', $new_classes);
// Save the modified HTML.
$link_html = $dom->saveHTML();
}
}
// Always return the HTML, whether modified or not.
return $link_html;
}
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:254
public static function automator_link( $atts ) {
static $script_added = false;
$atts = shortcode_atts(
array(
'id' => 0,
'is_ajax' => 'no',
'text' => esc_html_x( 'Click here', 'Magic Link label', 'uncanny-automator-pro' ),
'success_message' => esc_html_x( 'Done', 'Magic Link Success Message', 'uncanny-automator-pro' ),
'submit_message' => esc_html_x( 'Processing...', 'Magic Link Submit message', 'uncanny-automator-pro' ),
'css_class' => '',
'link_only' => 'no',
),
$atts,
'automator_link'
);
if ( empty( $atts['id'] ) || 0 === $atts['id'] ) {
return '';
}
$nonce = wp_create_nonce( AUTOMATOR_PRO_ITEM_NAME );
$query_args = array(
'automator_trigger_id' => $atts['id'],
'automator_nonce' => $nonce,
'automator_action' => 'automator_magic_link_action',
'automator_referrer' => rawurlencode( add_query_arg( null, null ) ),
);
global $post;
if ( ! empty( $post ) && isset( $post->ID ) && isset( $post->post_title ) ) {
$query_args['automator_button_post_id'] = $post->ID;
}
$link = 'yes' === $atts['is_ajax'] ? '#' : add_query_arg( $query_args, get_permalink() );
// Generate / validate the css class name.
$css_class = self::get_css_class_name( $atts, 'automator_link' );
$link_html = '<a href="' . esc_url( $link ) . '" class="' . esc_attr( $css_class ) . '" data-nonce="' . $nonce . '" data-automator-id="' . esc_attr( $atts['id'] ) . '" data-is-ajax="' . esc_attr( $atts['is_ajax'] ) . '" data-success-message="' . esc_attr( $atts['success_message'] ) . '" data-submit-message="' . esc_attr( $atts['submit_message'] ) . '">' . esc_html( $atts['text'] ) . '</a>';
// If AJAX is enabled for any link, enqueue the script
if ( 'yes' === $atts['is_ajax'] && ! $script_added ) {
add_action( 'wp_footer', array( __CLASS__, 'add_magic_link_script' ) );
$script_added = true; // Ensure script is added only once
}
// If the link_only attribute is set to yes, return the link only
if ( 'yes' === $atts['link_only'] ) {
return $link;
}
// Add the link and nonce to the attributes for the filter.
$atts['link'] = $link;
$atts['nonce'] = $nonce;
/**
* Filter the HTML output of the magic link
*
* @param string $link_html The HTML output of the magic link
* @param array $atts The shortcode attributes
*
* @return string - The filtered HTML output of the magic link
*/
return apply_filters( 'automator_pro_magic_link_html', $link_html, $atts );
}