• Is it possible to pull in a referring post authors email and/or name into a URL?

    For example:

    I have a link on a post.

    When people click on that link, I would love for the link to include the post authors email and/or name.

    The reason I ask, is… The link that people will click on is a link to a form. That form can pull information and autofill based on parameters in the URL.

    I am hoping to pull [Referring Author Email] and [Referring Author Name] into that form.

    If you can do that, can someone help me figure out how?

    • This topic was modified 4 years, 5 months ago by Jan Dembowski.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    Yes you can. Arbitrary data can be added to URLs as query strings. For example:
    https://example.info/awesome-form/?author=cool-dude&[email protected]
    The query string values, when constructed in PHP, must be run through urlencode() prior to output as certain literal chars are not allowed and must be encoded.

    On the form’s template, the passed values will be available as $_GET['author'] and $_GET['email']. Values may need to be run through stripslashes() to deal with any escaped chars. The passed values could be used as a form field’s value attribute when constructing the form in PHP after running the values through the WP esc_attr() function.

    As you can see, it’s important for data to be properly formatted and escaped as it is used in different ways in the process. Once the form data is submitted for processing, it too must be validated and sanitized prior to using it further. In other words, always properly escape data when it comes in and before it goes out.

    Remember that anyone can compose any sort of URL they like. You are responsible for ensuring passed data, especially email data, is valid and appropriate for purpose. For this reason, I’d advise against passing literal author data as illustrated above. I would simply pass the author or post ID and use that ID to fetch the related data from WP for use on the form.

    Thread Starter Jjones37

    (@jjones37)

    @bcworkz Thank you for replying!

    Thank you for your perspective. I agree, I should pass the Author ID.

    I understand the premise of using a parameter in the URL and how to pull the parameter out of the URL.

    I think what I am looking for, is help passing the referring page/post author INTO the URL as a parameter.

    I am not sure what shortcode or logic in the URL that will grab the referring page/post author.

    In your example: https://example.info/awesome-form/?author=cool-dude&[email protected]

    I am not sure what to put after the “author=” portion to automatically pull the page/post author.

    For this example, I would love to learn how to pull:
    -Referring Post/Page Author Name
    -Referring Post/Page Author ID

    Thanks so much for your help!!

    Moderator bcworkz

    (@bcworkz)

    It depends on what mechanism is outputting the link. I’m guessing something like the_permalink(). In which case it also depends on what object is linked — page, post, CPT. If a page, use the filter “page_link” to alter the link. The link’s object ID is passed to your callback. Use get_post() to get the object itself, from which you can get the author’s ID from its “author” property.

    Thread Starter Jjones37

    (@jjones37)

    @bcworkz Great question. Thank you for your reply and help!

    My use case:

    – I have a button on every page/post of my site. This button leads to a page on my site with a form. The form allows for users/guests of the site to submit feedback regarding the page or post (like errors, typos, information updates) etc.

    – Currently that form has a short code that pulls in [Referring Page] to a field so that I know what page they are referring to.

    – I know the form will also pull in parameters that I put in the URL to the page. I have tested that (as you listed above) and it works great.

    – My hope is that I can built into the button link/url additional parameters that would give the me the Page/Post Authors information so that they can be copied on the submission of the form.

    Does that help?

    Again, thank you so much for sticking with me and offering your help!

    Thread Starter Jjones37

    (@jjones37)

    @bcworkz no rush or priority, wondered if you had additional thoughts based on my use case?

    Moderator bcworkz

    (@bcworkz)

    Sorry for the late reply, something happened to the notification. Probably something I did by accident, but I’m blaming my mail client anyway ??

    On the post page with the button, the current post object has the author ID which could be passed to the form through the URL’s query string. Or in the form’s shortcode handler, since you have the referring URL, you could query for the related post object using its slug, then get its author ID from the object. With the author ID you can get the related user data with get_userdata() and get_user_meta() to populate the form fields.

    If you want to append the author ID to the form URL used by the button, how to do so again depends on how the form’s link is generated. Assuming you have the form URL in a variable, you can use add_query_arg() to append the author ID.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘URL Parameters’ is closed to new replies.