tec_copy_to_clipboard_button( string $content_to_copy, bool $output_button = true, string $aria_label = '' )

Output a button to copy the content of an element to the clipboard.


Parameters

$content_to_copy

(string) (Required) The content to copy to the clipboard.

$output_button

(bool) (Optional) Whether to output the button or just the target element.

Default value: true

$aria_label

(string) (Optional) The aria-label attribute for the button.

Default value: ''


Top ↑

Return

(string)


Top ↑

Source

File: src/functions/utils.php

	function tec_copy_to_clipboard_button( string $content_to_copy, bool $output_button = true, string $aria_label = '' ): string {
		$cache_key = 'tec_copy_to_clipboard_counter';
		$counter   = tribe( 'cache' )->get( $cache_key, '', 1 );

		$target        = 'tec-copy-text-target-' . $counter;
		$notice_target = 'tec-copy-to-clipboard-notice-content-' . $counter;

		$aria_label = $aria_label ? $aria_label : __( 'Copy to clipboard', 'the-events-calendar' );
		tribe( 'cache' )->set( $cache_key, ++$counter, Tribe__Cache::NON_PERSISTENT );
		if ( $output_button ) :
			?>
			<a
				title="<?php echo esc_attr( $aria_label ); ?>"
				href="javascript:void(0)"
				aria-label="<?php echo esc_attr( $aria_label ); ?>"
				aria-describedby="<?php echo esc_attr( $notice_target ); ?>"
				data-clipboard-action="copy"
				data-notice-target=".<?php echo esc_attr( $notice_target ); ?>"
				data-clipboard-target=".<?php echo esc_attr( $target ); ?>"
				class="tec-copy-to-clipboard tribe-dashicons"
				role="button"
			>
				<input type="text" readonly value="<?php echo esc_attr( $content_to_copy ); ?>" aria-hidden="true" />
				<span class="dashicons dashicons-admin-page" aria-hidden="true"></span>
			</a>
			<?php
		endif;
		?>
			<span class="screen-reader-text <?php echo esc_attr( $target ); ?>"><?php echo esc_html( $content_to_copy ); ?></span>
			<div class="tec-copy-to-clipboard-notice">
				<div class="tec-copy-to-clipboard-notice-content <?php echo esc_attr( $notice_target ); ?>" aria-live="polite">
				</div>
			</div>
		<?php

		// When they want to print the button outside of this function they need to be aware of the target.
		return $target;
	}

Top ↑

Changelog

Changelog
Version Description
6.0.3 Introduced.