Last active
July 18, 2021 05:36
-
-
Save rupokify/4ae1ea58743da854f47008927fb08cfb to your computer and use it in GitHub Desktop.
Copy Text to Clipboard Shortcode for WordPress
This file contains hidden or 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 | |
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