• I maintain WP post to PDF Enhanced, and have recently been made aware of an issue raised by Participants Database regarding the added code in the url to get the [pdb_single] shortcode to work.

    Currently, my plugin is getting the url via a basic method:

    get_permalink($post->ID);

    Unfortunately, that won’t do for generating a PDF of $post when we have the added ?pdb=nn or &pdb=nn tacked on. I tried:

    home_url(add_query_arg(array(),$wp->request));

    but I’m not capturing the full URL. Do you think I’m going to need to resort to a pure PHP method, or is there some way using the WP API I might get it? (I know, this isn’t strictly a question related to this plugin, though it is relevant.)

    Any help would be greatly appreciated!

    Cheers

    Lewis

    http://wordpress.org/plugins/participants-database/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author xnau webdesign

    (@xnau)

    There’s two ways, the easy way and the “right” way…basically, you just add the variables, but you need to know which character to use to join it. If it’s just on your site, and you know you are using pretty permalinks, you can just go with get_permalink($post->ID).'?pdb=nnn' but the right way is to check for the question mark:

    <?php $link = get_permalink($post->ID) . (strpos(get_permalink($post->ID), '?') !== false ? '&' : '?') . 'pdb=nnn'; ?>

    There isn’t any simpler way to get that done that I know of.

    Thread Starter Lewis Rosenthal

    (@lewisr)

    I already check for the ? vs the &:

    $qst = get_permalink($post->ID);
                $qst = parse_url($qst);
                if (isset($qst['query']))
                    $qst = '&format=pdf';
                else
                    $qst = '?format=pdf';

    The problem is that I don’t want to just hardcode for your plugin by adding pdb=nnn, so I was looking for a more universal dynamic solution. (I do like your syntax better, though; thanks for the tip!)

    I’ll keep poking at it. 😉

    Cheers

    Lewis

    Plugin Author xnau webdesign

    (@xnau)

    Well, if I knew more about the context in which you’re trying to create this link, I might be able to help.

    Thread Starter Lewis Rosenthal

    (@lewisr)

    🙂

    Yeah, kind of difficult to see it in the abstract; my apologies.

    We render an icon with a link to create a PDF of the existing page. To create the link, we append

    ?format=pdf

    or

    &format=pdf

    to the permalink, depending upon whether we see a prettified url or not (or really, whether we see a “?” or not).

    The existing code (block quoted, above) gets just the permalink, but obviously doesn’t see the appended

    ?pdb=nnn

    I’ve just played around with swapping:

    get_permalink($post->ID)

    for

    $_SERVER["REQUEST_URI"]

    which almost works, though I haven’t considered any potential downside to it.

    More complete code (original):

    // Change querystring separator for those who do not have pretty URL enabled
                $qst = get_permalink($post->ID);
                $qst = parse_url($qst);
                if (isset($qst['query']))
                    $qst = '&format=pdf';
                else
                    $qst = '?format=pdf';
    
                return '<a rel="noindex,nofollow">ID) . $qst . '" title="Download PDF">' . $this->options['imageIcon'] . '</a>';

    I don’t want you to spend a lot of time on this, really. Yours is the first plugin I’ve seen (close-up) to actually require a tweak to the url in order to render the proper data from the shortcode, so I thought you might be be a good place to start my investigation. 🙂

    Your input is most appreciated.

    Cheers

    Lewis

    Thread Starter Lewis Rosenthal

    (@lewisr)

    Actually, add_query_arg looks promising.

    Thread Starter Lewis Rosenthal

    (@lewisr)

    Indeed, I think that’s the ticket:

    return '<a class="wpptopdfenh" target="_blank" rel="noindex,nofollow" href="' . add_query_arg( 'format', 'pdf' ) . $qst . '" title="Download PDF">' . $this->options['imageIcon'] . '</a>';

    No muss, no fuss. On the back end, it actually utilizes $_SERVER["REQUEST_URI"] (if the third param is blank) and then automatically determines the proper separator on the line.

    Unless you have a better suggestion, xnau?

    Thanks for kicking this around with me!

    Cheers

    Lewis

    (PS – Please feel free to mark this as resolved unless you have something to add or to suggest, but I’ll leave that to you.)

    Thread Starter Lewis Rosenthal

    (@lewisr)

    Oops; missed a little bit:

    return '<a class="wpptopdfenh" target="_blank" rel="noindex,nofollow" href="' . add_query_arg( 'format', 'pdf' ) . '" title="Download PDF">' . $this->options['imageIcon'] . '</a>';

    That should do (straggling remnant of old code removed).

    Plugin Author xnau webdesign

    (@xnau)

    Looks like you’re missing the $qst variable, which looks like it should be in front of the add_query_arg but otherwise looks like it will work. If you want to get a specific variable out of the URL, you can access it using the $_GET array.

    Thread Starter Lewis Rosenthal

    (@lewisr)

    Thanks. In my case, I did away with

    // Change querystring separator for those who do not have pretty URL enabled
                $qst = get_permalink($post->ID);
                $qst = parse_url($qst);
                if (isset($qst['query']))
                    $qst = '&format=pdf';
                else
                    $qst = '?format=pdf';

    completely, and am able to just use the one (replaced) line (without the $qst variable at all). I’ve tested it against a post with [pdb_single] in it, after manually calling the page with the extra query string on the url. It worked both with and without a pretty url, so I guess this is it!

    Thanks for the tip about using $_GET to extract components, too!

    Cheers

    Lewis

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Suggestion for extracting the complete url when appending ?pdb= (or &pdb=)?’ is closed to new replies.