How to add share via email functionality after each item’s content using filters
Adding a link to each post or page, allowing visitors to email a link to the article that they are currently viewing. This functionality is implemented using a filter hook attached to the page or post or to both the contents. This allows our custom function to append custom output code to all entries that get displayed on the screen.
How to do it…
Follow these steps to create a plugin that adds an email link at the end of all posts or all pages or both:
- Navigate to the WordPress plugins directory of your development installation.
- Create a new directory called
append-mailto-functionality
- Navigate to this directory and create a new text file called
mailto-functionality.php
- Open the new file in a code editor and add an appropriate header at the top of the plugin file.
/* Plugin Name: Mailto Funtionality Plugin URI: Description: Mailto Functionality to share the post to others or to self. Version: 1.0 Author: Vinay Vengala Author URI: https://www.vengalavinay.com/contact-us/ License: GPLv2 */
- Visit an icon download website, such as iconarchive.com or flaticon.com or any, and download an email icon with a small resolution (32 x 32 pixels) in PNG format to the
append-mailto-functionality
directory, giving it the namemail-icon.png
- Add the following line of code at the end of the file to register a function that will be called when WordPress is preparing data to display the content of a post or page:
add_filter( 'the_content', 'v_mailto_functonality_filter' );
- Add the following code section to provide an implementation for the
v_mailto_functionality_filter
function:function v_mailto_functionality_filter ( $the_content ) { // build url to mail message icon $mail_icon_url = plugins_url( 'mail-icon.png', __FILE__ ); // Set value of $new_content to previous content $new_content = $the_content; // Append image with mailto link after content, // including the item title and permanent URL $new_content .= '<div class="mailto_link">'; $new_content .= '<a title="Email article link"'; $new_content .= 'href="mailto:[email protected]?'; $new_content .= 'subject=Check out this blog post '; $new_content .= get_the_title(); $new_content .= '&body=Hi!%0A%0AYou might '; $new_content .= 'enjoy this article entitled '; $new_content .= get_the_title() . '.%0A%0A'; $new_content .= get_permalink(); $new_content .= '%0A%0AEnjoy!">'; $new_content .= '<img alt="Mailto icon" src="'; $new_content .= esc_url( $mail_icon_url ); $new_content .= '" /></a></div>'; // Return filtered content for display on the site return $new_content; }
If you want to add conditional statement to check whether it is post or page or any other post type. Refer to the codex documentationget_post_type()
or add the following code in the place of
return $new_content
if ( get_post_type( get_the_ID() ) == 'post' ) { // change the post type as per the need return $new_content; }else{ return $the_content; }
- Save and close the plugin file.
- Log in to the WordPress Dashbaord.
- Click on Plugins in the left-hand navigation menu.
- Activate your new plugin.
- Visit your website to see the new mail icon at the end of each post and page. You may need to click on an article or page to see the icon.
- Click on one of the mail links:
your default mail client will come up with information about the item you were reading. The only information that needs to be updated is the recipient address before visitors can quickly send an email.
How it works…
This plugin uses the add filter function to register a custom function to be called by WordPress as it prepares an item’s content to be displayed on a page or post or any custom-post-type. When the filter function is called, the first action that it performs is to create a URL to the email icon that was downloaded. It then goes on to modify the original content by appending the HTML code to display a mailto link. The same technique can be used to create links to popular social media and link sharing sites, with simple changes to the syntax of the link. Once the new content is ready, it is returned to WordPress to be sent to any other registered filters and subsequently displayed on the site.
There’s more…
This recipe also introduces a pair of useful WordPress utility functions to get access to the current item’s content.
The get_the_title
and get_permalink
functions
While these two functions are mainly seen within theme template files, they can also be used by plugins to get easy access to the content of items that are currently being processed. More specifically, the two utility functions that are used in this recipe are as follows:
get_the_title()
: This function gives us quick access to the item’s title.
get_permalink()
: This is a function that returns the item’s permalink (a URL that is always associated with this post or page, even after it is no longer featured on a website’s front page or blog page).
get_post_type()
: This function gives us the name of the post-type whether it is post or page or any custom-post-type.
Leave a Reply
You must be logged in to post a comment.