I wanted to add a print button to a client’s WordPress based website for a payment receipt page. There are many instances you may want to add a print button – even though all popular browsers have a simple Print command built right in for any web page.
I started by trying out the various WordPress plugins but they either did not print properly or they included advertising to the end-user once they print – which would be unacceptable in my case.
So I just created one myself and thought to share this with you, to make your life easier as well.
It is simple enough to add a link with some javascript:
<a class="print-link" href="javascript:window.print()">Print This Page</a>
You will need to add that to your text editor when it is in “Text” mode rather than “Visual” mode.
It doesn’t seem that you lose javascript anymore in the WordPress text editor when toggling back and forth between “Text” mode and “Visual” mode, but if you have an older version of WordPress or for other reasons do not want Javascript in your WordPress text editor, then you can add the following to your theme’s functions.php file, (or create one if one does not exist):
// [print_button]
function print_button_shortcode( $atts ){
return '<a class="print-link">Print This Page</a>';
}
add_shortcode( 'print_button', 'print_button_shortcode' );
And then just add the following shortcode to the page or post that you want the button, where ever on the page you want it:
[print_button]
I included a class in that code, “print-button” that you can add to your stylesheet and style as you like. For example if you wanted that to be a blue button to fades to orange when hovered over you could do this:
.print-link {
display:inline-block;
margin-bottom: 15px;
padding:18px 32px;
line-height:1em;
background-color: #005288;
color: #FFF !important;
font-size: 18px;
font-family:"Lucida Grande", 'LucidaSansRegular', Arial, Helvetica, sans-serif !important;
border-radius: 3px;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
text-shadow: 1px 1px 1px #11111169;
box-shadow: 1px 1px 1.2px #555555;
-moz-box-shadow: 1px 1px 1.2px #555555;
-webkit-box-shadow: 1px 1px 1.2px #555555;
transition: all 0.5s ease 0s;
-moz-transition: all 0.5s ease 0s;
-webkit-transition: all 0.5s ease 0s;
-ms-transition: all 0.5s ease 0s;
-0-transition: all 0.5s ease 0s;
}
.print-link:hover {
background-color: #C60;
color: #FFF;
text-decoration:none;
}
You could also just change the class assigned to the code to one that is already in use on your website for a button or other style you like.
Here is a sample of what it looks like, with the styles above added.
Print This PageFinally, if you want to customize the styling of your printed pages so that they do not print out elements of the page like backgrounds and headers and navigation, etc. that you do not want, you will need to create a print style sheet. For that I will send you to the following page on the WordPress website for more information http://codex.wordpress.org/Styling_for_Print.
Good luck. If this helped you or you have feedback, let me know below.
42 thoughts on “How to Add a Print Button to a WordPress Page or Post”
Hi. Great code. I want to use it but I have a left nav and when I go to “print the page” my left nav is visible and huge. Any thoughts?
You would have to customize the CSS for print or try a plugin that lets you customize the print stylesheet.
What if we want to only print a particular element on the page and not everything, how could that be done?
I don’t know.
Its really nice but what if we only have to print a single page,there is any way to print a single page ?
You would typically use your devices print dialog to specify that.
thanks alot 🙂
although yesterday i had problem and the code put text in my page today its ok it is a button like yours
thanks a lot again it works very nice
I just used your code on a page and it works
I want to know if there is a way to use this on a printer icon instead the phrase “print this page”
because in my page its not like a button but its text grey color and rose when mouse hover above
Thanks a lot for share this with us
Yes, you would just place your image or image code instead of the words. In the visual editor I think you could try this by adding your image, then making it a link and for the url for the link you would put: javascript:window.print()
Otherwise you would use the Text editor and add your image clicking the img button to add an image tag https://www.w3schools.com/tags/tag_img.asp
thanks a lot i ‘ll try this
how can we change apply button in posts page in wordpress from our own plugin?
Can’t say I understand what you are asking?
hi, i’m trying to change the hover and text but for some reason it won’t even though it’s showing the correct title and css.
You would have to provide a working sample for me to look at so I could offer any feedback.
I’m trying to print a popup window only but the page underneath prints also. Is there any way I can only print the popup?
Thanks for the great information.
Buddy
Hi Buddy,
Sorry. I don’t know the answer to that one.
Thanks a lot, works like a charm.
Glad to hear.
Hi,
Thank you for the wonderful tutorial! 🙂
I’m creating a multi-language site. How can I replace the “Print this page” text for the different language versions? Thanks!
That is a bit more complicated than I have time to support in a forum response. You might try contacting these guys at WPML.
this really helped alot
thanks
I just shows the word Print this but no clickability fora wordpress post.
It should work. I followed the post’s instructions just now and put a sample of the button at the end of the post showing it working.
If the words are showing but it is not clickable, then perhaps you added the javascript directly rather than in the functions.php file and perhaps you either added it in Text mode rather than Visual mode as described in the post, or the javascript part of the link was removed when you toggled between Text and Visual mode which is what happens in WordPress still (check your code and see what it says in Text mode if you did this). Best is to go for the functions file.
FYI – if you have a caching plugin on your site and upload the style sheet, you may have to purge the site cache and then wait a few minutes (depending on site size) for it to rebuild before the styles will show.
Hey,
I was just looking at your code and thank you for posting. All I am seeing in the javascript area is…Print This Page?
Thanks. That code keeps getting removed by accident. I fixed it.
Thanks!
How define what this button will print when click it.
POST TITLE : CONTENT : FOOTER : IMAGES
To customize what prints out you could would have to specify print rules in a print CSS file. You could checkout this page about printing and try this plugin. I have not used that plugin before but the one I used, Bunny’s has not been updated in 2 years.
Good luck.
if i want to add print link in wordpress loop then how can i add it
waiting for your reply
You should be able to just add the first piece of code in the above post, (after the words”It is simple enough to add a link with some javascript”), in your page template where you want it.
Hi. Just came across this great article. Ive got this working using the functions PHP. How could I add this functionality to a clickable button or image?
Thank you for sharing but it didn’t working for me. I was wondering where I went wrong and I was hoping you might be able to shed some for me. Placing the code in my child theme functions page, adding the CSS to my style sheet and the shrortcode into my WordPress page seems easy enough but it didn’t work. All I get is the button and it appears as intended with the hover affect as per the style sheet but no print action. Any help would be appreciated thanks.
Once again I must have visited this post in Visual mode, resulting in WP removing my javascript in the code example. Replace what you added to your functions.php file before with what is there now and it should work. My apologies.
Thanks. This is something I am looking for. 🙂
Hmmm… My site (using Genesis framework) does not print the HTML correctly. For some reason the shortcode prints as raw HTML (can’t show you the code) WP gets angry.. is what shows up. Weird…
Hi Scott,
I looked at the code in my post and it seems that some of the code was converted improperly. Instead of < for example it was showing < ;. Anyways, it is fixed now. Try the code above now and see if that fixes it.
Thanks, this was just what I needed!!
very nice, and it is working.. thanks for share.
Thank you for making this great bit of code available. So easy and so useful!
No sweat. I like your blog. This line made me laugh, in the context of the article you wrote, “Russian engineers, when faced with the same problem, decided to use a pencil.”
it is working very nice. thanks for share