Home / Widgets / PRINT WP POST
Duplicate Snippet

Embed Snippet on Your Site

PRINT WP POST

Add this to print WP posts. It's very basic but it works beautifully.

Code Preview
php
<?php
add_filter('the_content', function($content) {
    if (!is_single()) return $content;
    $button = '<div id="print-button-container" style="text-align: center; margin: 20px 0;"><button onclick="window.print()" style="background-color: #0073aa; color: white; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer;">Print Post</button></div><script>window.addEventListener("beforeprint", function() { document.getElementById("print-button-container").style.display = "none"; }); window.addEventListener("afterprint", function() { document.getElementById("print-button-container").style.display = "block"; });</script>';
    $pos = strpos($content, '</p>');
    if ($pos !== false) {
        return substr_replace($content, $button, $pos + 4, 0);
    }
    return $button . $content;
});

Comments

Add a Comment