25% off ProSNIPC25OFF

Print-This-Page Toolbar

Inserts a discreet print and save-as-PDF toolbar below post content with print-specific styles that hide navigation, sidebars, and ads.

Module3 parts · by SnipCraft

PHPLogic

php
<?php
// Auto-insert on single posts/pages, or use [scseed_print_toolbar] shortcode
add_filter( 'the_content', 'scseed_print_toolbar_append', 30 );
function scseed_print_toolbar_append( $content ) {
    if ( ! is_singular() || ! in_the_loop() || ! is_main_query() ) { return $content; }
    return $content . scseed_print_toolbar_html();
}

add_shortcode( 'scseed_print_toolbar', 'scseed_print_toolbar_sc' );
function scseed_print_toolbar_sc( $atts ) {
    return scseed_print_toolbar_html();
}

function scseed_print_toolbar_html() {
    return '<div class="scseed-print-bar scseed-print-hide">'
        . '<span class="scseed-print-bar-label">' . esc_html__( 'Share this article:', 'scseed' ) . '</span>'
        . '<button class="scseed-print-btn" type="button" onclick="window.print()" aria-label="' . esc_attr__( 'Print this page', 'scseed' ) . '">'
        . '<svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="6 9 6 2 18 2 18 9"/><path d="M6 18H4a2 2 0 0 1-2-2v-5a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2h-2"/><rect x="6" y="14" width="12" height="8"/></svg>'
        . esc_html__( 'Print', 'scseed' ) . '</button>'
        . '</div>';
}

CSSStyles

css
.scseed-print-bar {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
    margin: 1.5rem 0 0.5rem;
    padding: 0.75rem 1rem;
    background: #f8f9fb;
    border: 1px solid #e8eaed;
    border-radius: 6px;
    font-size: 0.875rem;
}
.scseed-print-bar-label { color: #666; font-weight: 500; }
.scseed-print-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.35rem 0.875rem;
    background: #fff;
    border: 1px solid #d0d0d0;
    border-radius: 5px;
    font-size: 0.8rem;
    font-weight: 600;
    color: #444;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
    font-family: inherit;
}
.scseed-print-btn:hover { background: #f0f0f0; border-color: #aaa; color: #111; }

/* Print media: show only article content */
@media print {
    .scseed-print-hide,
    header, footer, nav, aside,
    .site-header, .site-footer, .site-navigation,
    .widget-area, .sidebar, #sidebar,
    #wpadminbar, .admin-bar-placeholder,
    .comments-area, .post-navigation,
    [class*="advertisement"], [class*="ads-"] {
        display: none !important;
    }
    body { background: #fff !important; color: #000 !important; font-size: 12pt; }
    a { color: #000 !important; text-decoration: underline; }
    a[href]::after { content: " (" attr(href) ")"; font-size: 10pt; color: #555; }
    img { max-width: 100% !important; page-break-inside: avoid; }
    h1, h2, h3, h4, h5, h6 { page-break-after: avoid; }
    p, blockquote, pre { page-break-inside: avoid; }
}

JavaScriptScript

js
(function() {
    'use strict';
    // Add keyboard shortcut Ctrl/Cmd+P hint tooltip on hover
    document.addEventListener('DOMContentLoaded', function() {
        document.querySelectorAll('.scseed-print-btn').forEach(function(btn) {
            var isMac = navigator.platform && navigator.platform.indexOf('Mac') !== -1;
            btn.setAttribute('title', ( isMac ? 'Cmd' : 'Ctrl' ) + '+P to print');
        });
    });
}());
#ux#accessibility#print#pdf#toolbar