• Resolved blueshark

    (@blueshark)


    Hi there,

    for each page I want to add a print button. This button makes the site reload with other style sheets, so that is fine for printing.

    I am using post name permalink setting, so a link to a page or post looks like http://my-url.de/sample-post/:
    <a href="<?php print get_permalink().'?print=true'; ?>" title="Printversion">...</a>

    As a fist solution I added just a ?print=true to the link and then check for $_GET[‘print’]:
    <?php if( !isset($_GET['print']) || $_GET['print']!=true || !(is_single()||is_page()) ): ?>...

    This works fine so far, but it is not a clean solution. When changing the permalink structure this might not work, since it will end in somethink like http://my-url.de/?p=123?print=true

    So what might a nice solution for that issue?

    Thanks

    Blueshark

Viewing 4 replies - 1 through 4 (of 4 total)
  • Have you considered using something like a print stylesheet? That way, if the print button is pressed in the browser, it will also print nicely and doesn’t require a page reload.

    Thread Starter blueshark

    (@blueshark)

    I do have a print stylesheet. But I prefer also a preview, so that the user can see what he is printing. So I want to reload the page with the print stylesheet. This stylesheet is triggered with the ->print<- option.

    Moderator bcworkz

    (@bcworkz)

    You basically need to check for an existing ‘?’ in the permalink to decide to add a '?print=true' or a '&print=true' to the URL. I wouldn’t call the code needed to do this “nice and clean”, but it is necessary.

    You could use add_query_arg() to add your print query arg. This hides the necessary code in core so at least your code is nice and clean 🙂

    OK, what I would do then is:

    1. Add a new query var in WordPress

    function query_vars($query_vars) {
    	array_push($query_vars, 'print');
    	return $query_vars;
    }
    
    add_filter(
    	'query_vars',
    	'my_query_vars'
    );

    2. Add a function to build the print permalink

    function get_print_permalink($permalink) {
    	$url = parse_url($permalink);
    	if ($url['query'] === '') {
    		echo $permalink . '?print=true';
    	} else {
    		echo $permalink . '&print=true';
    	}
    }

    3. Retrieve query var value from WordPress

    This is how you’d check whether the query var is set or not:

    get_query_var('print, 'false');

    It will return false if it doesn’t exist or true if it’s set to true.

    The code above is untested, so let me know if you’re running into issues.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Print Button -> print.css’ is closed to new replies.