• Resolved dsb0328

    (@dsb0328)


    Hello. I am trying to figure out how to add custom subject and body text to an email from a mailto link from a WordPress page. I would like it to populate the subject with part(s) of the page, like the title. For instance, the subject line would say something like “Info request for “Name/Title of the WordPress Page”” and the body would be something like “Hello, I am looking to receive more information about “Name/Title of WordPress Page”. Please tell me more.” Where in both cases the name of the page in WordPress would be pulled into the subject line and the body copy. I assume I have to do this in my functions.php file, but not sure and not sure how. I know I can do it in the mailto html code itself, but that would have to be changed for each page and I am trying to make it automated so that when new pages based on the same template page are created, it will take from the page title itself and add it into the subject and body when the mailto link is clicked. Hope that makes sense.

    • This topic was modified 4 years, 10 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 12 replies - 1 through 12 (of 12 total)
  • Is the mailto link part of the contents of the Page from the editor or is it in a php template file of your theme?

    Thread Starter dsb0328

    (@dsb0328)

    The mailto link would be a part of a page, like a button or linked text. That page would be duplicated and used to create new pages, like a template. Hoping that there’s some sort of code that could work to pull from the page title or something.

    Alright, try the following in functions.php

    add_shortcode( 'custom_mailto_title', 'custom_mailto_title' );
    
    function custom_mailto_title( $atts ) {
        return esc_attr( get_the_title( get_the_ID() ) );
    }
    

    Please make sure the functions.php file begins with <?php without any spaces before.

    Then in the editor the mailto would be as follows (code/text edit mode):

    Some text <a target="_blank" href="mailto:[email protected]?subject=Info request for [custom_mailto_title]&body=I would like to know more about [custom_mailto_title]" rel="noopener noreferrer">Link text</a>
    

    Finally, please note that the href attribute of the above code is not formatted properly. It should be URL encoded (spaces become %20 etc.) but it might work just fine, so give it a try.

    • This reply was modified 4 years, 10 months ago by Valentin Bora.
    • This reply was modified 4 years, 10 months ago by t-p.
    Thread Starter dsb0328

    (@dsb0328)

    @valentinbora Thanks a lot for this answer. I will try it out asap and let you know how it goes.

    Thread Starter dsb0328

    (@dsb0328)

    This is going to work perfectly. Thank you very much for your expert help!

    Thank you Valentin Bora for your great tip, it was what i was looking for.

    I have a question:

    would it be possible to add not only the title of the current page/post to the email but as well the feature image (thumbnail) to the body of the mail?

    Thank you for any reply…

    This worked perfectly for me also. Many, many thanks.

    I might add that I was somewhat confused about where to put this function.php file or whether to include it in one of the many existing function.php files in the wordpress directory. I ended up adding the function at the end of the function.php file located in the /themes/twentyfifteen/ directory that contains functions for the theme that I use for my website. I suspect this will no longer work if I change themes and I have no idea where to put it to make it universally applicable.

    @wrknight you can create a folder under wp-content called mu-plugins and add a file like wp-content/mu-plugins/my-custom-functions.php

    Within the file, add a header docblock to tell WordPress about the plugin and your code afterwards:

    <?php
    /*
    Plugin Name: My Custom Functions
    Description: Some description
    Version: 1.0.0
    */
    
    add_shortcode( 'custom_mailto_title', 'custom_mailto_title' );
    
    function custom_mailto_title( $atts ) {
        return esc_attr( get_the_title( get_the_ID() ) );
    }

    Make sure to avoid duplicating this code within theme functions.php and the plugin, or your site will fail with an error due to a duplicate function.

    These are called Must-Use Plugins and cannot be disabled within WordPress admin, so you can rely on them to always be available irrespective of the theme in use.

    I think you can simply create html tag as below
    <a href="mailto:[email protected]?subject=Subject&body=Body Part">Send Email</a>
    Source: MailTo HTML anchor tag Link Inlcuding Subject

    Hi

    Can someone help with the code for Woocommerce and products.
    I want to have an email link on each product page.

    Thank you
    Best Regards, Edvard

    • This reply was modified 4 years, 1 month ago by edlar.

    Mailto using long-form email addresses

    Be patient with me–I’ve been creating web pages in raw HTML since 1995, mostly without benefit of a CMS, but with occasional use of Drupal or commercial systems like MyClassOnline and ClubExpress. WordPress is a new experience. I’ve been tasked as a volunteer to move a website of 40 pages to WordPress. The site includes many sets of email addresses using mailto.

    The original specification for email structure, RFC-822, and later versions allow for short-form email addresses like <B>[email protected]</B> and for long-form addresses like <B>John Jones <[email protected]></B>.

    The complete long-form mailto looks like:
    “>Send mail to John
    The angle brackets for the long-form address are tricky. The outer set are HTML delimiters. The inner set are RFC-822 delimiters. If you enter it as above, the <A tag ends at the first > and “> appears as part of the clickable link.

    So instead, you enter the mailto: link as
    “>Send mail to John
    This has always worked for me in raw HTML and in Drupal. But it does not work in WordPress. The WordPress code editor “helpfully” changes the symbols to angle brackets.

    Am I doing something wrong, or is this a bug?

    Larry

    Ugh. This editor has caused the same problem as the one I was asking about, so my examples are unreadable.

    The problem is that certain symbols, namely & l t ; and & g t ; (spaced out to avoid the editor making undesired changes) are being converted to angle brackets when the editor saves text. They should stored and maintained as entered and delivered to the viewer’s browser for conversion there.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Populate mailto links subject and body with page name’ is closed to new replies.