Skip to content

Instantly share code, notes, and snippets.

@rupokify
Last active July 18, 2021 05:36
Show Gist options
  • Save rupokify/4ae1ea58743da854f47008927fb08cfb to your computer and use it in GitHub Desktop.
Save rupokify/4ae1ea58743da854f47008927fb08cfb to your computer and use it in GitHub Desktop.
Copy Text to Clipboard Shortcode for WordPress
<?php
add_shortcode( 'copycoupon', 'copy_text_shortcode' );
function copy_text_shortcode( $atts ) {
?>
<p id="text-to-copy"><?= $atts['coupon'] ?></p>
<button id="copybutton">Copy to Clipboard</button>
<?php
}
add_action( 'wp_footer', 'copy_to_clipboard' );
function copy_to_clipboard() {
?>
<script type="text/javascript">
jQuery(function($) {
$( "#copybutton" ).click(function() {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($("#text-to-copy").html()).select();
document.execCommand("copy");
$temp.remove();
});
});
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment