copyContent
// Add Shortcode
function custom_shortcode() {
function copy_text_shortcode($atts, $content = null) {
$atts = shortcode_atts(array(
'label' => '复制', // 按钮的标签
), $atts, 'copytext');
$html = '<span id="copy-text" style="display:none;">' . $content . '</span>';
$html .= '<button id="copy-btn" onclick="copyText()">' . esc_html($atts['label']) . '</button>';
$html .= '<script>
function copyText() {
var text = document.getElementById("copy-text").innerText;
navigator.clipboard.writeText(text).then(function() {
alert("文本已复制到剪贴板");
}, function(err) {
console.error("无法复制文本", err);
});
}
</script>';
return $html;
}
add_shortcode('copytext', 'copy_text_shortcode');
}
add_shortcode( 'copyContent', 'custom_shortcode' );