• I’m trying to do in my new website what I already have in my old website

    In my admin dashboard I need to add a quick link the open an email with a predefined object, content, and in corrispondence of a word in the content, it prints the code of the apartment (post) + name of the apartment (post meta)

    Adding a link is simple, I already have this code https://pastebin.com/wiQcL4wU

    but clicking on it I have to open an email

    the predefined content come from 3 custom meta fields like: get_the_author_meta( 'periodo_affitti_en', $user->ID )

    1 field is for the email address to which sending the mail, a second field is for the object of the email and the 3rd field for the content

    Inside the content in my old website there is in a certain point this conventional code %CODPROD%? that means that in that place the code and the name of the apartment (post) are recalled and printed

    how can I afford this?

    The page I need help with: [log in to see the link]

Viewing 12 replies - 1 through 12 (of 12 total)
  • Moderator bcworkz

    (@bcworkz)

    The way to do this depends on what sort of experience you want. Maybe the best way is to, on click, open a modal (or pop-up) form. The message content is auto-composed from the post’s data. You’d simply enter an email address and click “Send”. This approach is almost entirely JavaScript based.

    There is a way to have a more PHP focused approach. The link would take you to a custom admin page, passing the post’s ID as part of the link. This page would contain a form similar to the modal form described above. Again, you’d enter an email address and click “Send”. The disadvantage of this approach is you’d be taken away from the post list table. You’d need to navigate back to it. Whereas with a modal, it simply disappears and you’re back on the list table without any need to reload it.

    Thread Starter sacconi

    (@sacconi)

    In my current website admin, I just do one click and I open the pre-composed email, even with the email address already written (every apartment is connected with an agency /author so that the system should understand the recipient email address), so I’d like to do the same…maybe as first step I could modify this? https://pastebin.com/wiQcL4wU and make a new email box open?

    Moderator bcworkz

    (@bcworkz)

    the system should understand the recipient email address

    Ah, very good then. I was misunderstanding where recipients were coming from. A one click solution is indeed possible. The link can lead to a custom URL. The link passes all the email data the server needs to send the email. The link can have its action interrupted by JavaScript. The script can send the data to the server as an Ajax request so you needn’t leave the page. Following the link without JavaScript interruption can also work if you wish to avoid JavaScript. The destination can send the email, then redirect back to the page you were on. This causes the source page to reload, but it’s still a one click solution.

    A third option would be to create a mailto: link which contains all the necessary email data. On click, this will launch your local email client with all the provided data already filled in. You merely need to click “Send” in your mail client. A two click solution of sorts. The main advantage of this is it’s the easiest to implement. The From: address in this case will be whatever your mail client is configured for. If you let the server send the email, the From: address can be whatever you want. Mail from your mail client might be less likely to be discarded as spam than server email.

    Anyway, which ever approach you choose, the first step is to add the link to each post’s row actions. The exact nature of the link will depend on which approach you choose.

    Thread Starter sacconi

    (@sacconi)

    To tell the truth my currect website has a 2 steps solution: when I click on the link I open the precompiled email and then I have to click to “send” as for anyelse email. A good start would be creating a link that opens the email box with the right recipient, that is to say the agency (author) connected with the apartment (post). In each agency/ author editpage I have a specific field for the email address

    Moderator bcworkz

    (@bcworkz)

    Your code to add the action link to each post could fetch the recipient’s email address from where ever it is saved and use it to compose a HTML link like so:
    $mailto = "<a href=\"mailto:$recipient?subject=Your+inquiry&body=Email+message+body\">Send email</a>";
    where $recipient is assigned the email address. Subject and body must be URL encoded, that’s why there are + characters instead of spaces. Your mail client will restore the spaces. You can still edit the message before it is sent if so desired. Not possible with a true one click solution.

    Thread Starter sacconi

    (@sacconi)

    I sterted with https://pastebin.com/XfTsKMH0 but this code alone breacks the site, probably because $recipient is not defined (or there are other errors?) The recipient should be taken from https://sacconicase.com/wp-admin/user-edit.php?user_id=5&wp_http_referer=%2Fwp-admin%2Fusers.php%3Frole%3Dauthor

    the field in which I find the recipient is among the default fields of the contact information section, it’s the email address of the “author”/ agency

    Moderator bcworkz

    (@bcworkz)

    Get the author ID from $post, then get the user_email property from the object returned by get_userdata(). Assign it to $recipient.

    Instead of assigning the HTML to $mailto, assign it to $actions[email].

    Thread Starter sacconi

    (@sacconi)

    Moderator bcworkz

    (@bcworkz)

    You don’t need global $post. $post is passed to your function by the filter hook.

    You should only pass $author_id to get_userdata(). It’s an integer, not an object. The function returns a WP_User object, from which you can get their email. For example:
    $recipient = get_userdata( $author_id )->user_email;

    You’ll probably want to modify subject=request+customer&body=Email+message+body to reflect the subject and message you really want to send.

    Thread Starter sacconi

    (@sacconi)

    Everything is working fine! Now I have to refine the content of the body. First of all I would like the content of a specific field that will be found in the edit author page to be inserted in the body, as each author (agency) that receives an email may need a specific message (for some agencies the message will be in Italian, for others in English). And then the code and name of the product for which I am sending the request must appear in a certain point (both of them have a meta custom field), this is the typical message:

    Dear Alan

    I got the following request:

    customer’s name:
    number of people:
    period:
    animals:
    apt: HR-LUS-00114 Anja 3

    thank you

    with best regards

    Axxxxx Sxxxxxxxx
    tel:xxxxxxxxxxxxxxxxx
    fax:xxxxxxxxxxxxxxxx
    https://www.sacconicase.it

    Moderator bcworkz

    (@bcworkz)

    Be aware that there is some upper limit to how long an URL is. I don’t know what it is, but I doubt your intended message would exceed the allowable length. It probably doesn’t matter since it’s essentially a local link. All that really matters is that your mail client accepts it. FYI

    Collect all variable content from where ever it is saved and assign it all to variables. Compose your message as desired, using the variables where you want the data to appear. Be sure the message is defined with double quotes. Run it through urlencode() and assign the entire string to another variable. Use that variable in the final URL composition. Partial example:

    $owner = get_post_meta( $post->ID, 'owner', true );
    $customer = get_post_meta( $post->ID, 'customer', true );
    $message = urlencode("Dear $owner,
    
    I got the following request:
    
    customer’s name: $customer
    etc....";
    $author_id = $post->post_author;
    $recipient = get_userdata( $author_id )->user_email;
    $actions[email] = "<a href=\"mailto:$recipient?subject=Customer+Request&body=$message\">Send email</a>";

    Line breaks in your message should persist through to the received email message, assuming it is sent as plain text and not HTML. If your message should have any double quotes in it, they will need to be escaped with a backslash \, similar to how the double quotes within the <a> link tag are escaped. This is how PHP can tell the difference between a quote in the message and a quote demarcating the end of the string.

    Thread Starter sacconi

    (@sacconi)

    This would be my final work: https://pastebin.com/Fuj9wMyt , I have doubts about “get the ID” and “get the user”

Viewing 12 replies - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.