
WordPress Plugin Development Cookbook
By :

After making a number of changes to the page header, the generator meta tag, and the site favicon, this recipe takes a more active role by 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 and post content. This allows our custom function to append custom output code to all entries that get displayed on the screen.
Follow these steps to create a plugin that adds an email link at the end of all posts and pages:
plugins
directory of your development installation.ch2-email-page-link
.ch2-email-page-link.php
.Chapter 2 - Email Page Link
.ch2-email-page-link
directory, giving it the name mailicon.png
.add_filter( 'the_content', 'ch2epl_email_page_filter' );
ch2epl_email_page_filter
function:function ch2epl_email_page_filter ( $the_content ) { // build url to mail message icon $mail_icon_url = plugins_url( 'mailicon.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="email_link">'; $new_content .= '<a title="Email article link"'; $new_content .= 'href="mailto:[email protected]?'; $new_content .= 'subject=Check out this interesting '; $new_content .= 'article entitled '; $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="Email icon" src="'; $new_content .= esc_url( $mail_icon_url ); $new_content .= '" /></a></div>'; // Return filtered content for display on the site return $new_content; }
Figure 2.4 – The newly added Email article link icon
Figure 2.5 – The email generated after clicking on the link
As seen in Figure 2.5, 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.
Similar to the previous recipe, 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. When the filter function is called, the first action that it performs is to create a URL to the email icon that was downloaded in the recipe. 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.
This recipe also introduces a pair of useful WordPress utility functions to get access to the current item's content.
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).