ButlerBlog

chad butler's weblog

  • About
  • Blog
  • WordPress Plugins
  • Contact

Changing the WordPress email “from” address without a plugin

By Chad Butler 39 Comments

One common question I often hear regarding WordPress email configuration is, “How do I change the email address from wordpress@mydomain.com?”  I suspect that most people are unaware that the WordPress email function wp_mail() has a default address that it sets as the “from” address.  If they are aware, most users opt for changing this with a plugin.

WordPress has no ability to manage email settings through the admin panel, but that does not mean you can’t manage the email configuration at all.  It actually is very flexible.  You can even change your WordPress email configuration to send via an SMTP server; a method much more reliable than the generic email script.  But I’m getting ahead of myself.

If your sole aim is to change the email “from” address to something that is not the default value (wordpress@yourdomain.com), this is far to simple for using a plugin (unless you have complicated needs and want more control over email configuration in the WordPress admin panel).

There are quite a few plugins that do this, and if you feel more comfortable doing it that way, by all means, add another plugin to the list of things your blog needs to load.  But if you are brave enough to do something quick, easy, and lightweight, then read on!

wp_mail and some relatives

WordPress relies on a function called wp_mail() to send email.  This function is essentially a wrapper for the phpmailer class.  The problem is that the wp_mail default email “from” address cannot be configured via the WordPress admin panel.  But the fortunate thing is that this function and its related functions are both pluggable and can be filtered.  Our example here is to simply opt for a filter.

wp_mail() relies on some other outside information, some of which is wp_mail_from (an email address) and wp_mail_from_name (the real name given to the email address).  Since that is all we want to change, we are just going to filter those.

Filtering the email address

Add a filter for the email address using ‘add_filter’:

add_filter( 'wp_mail_from', 'my_mail_from' );
function my_mail_from( $email ) {
return "change-this-to-your-email-address";
}

Filtering the email name

Now add a filter for the name of the email address:

add_filter( 'wp_mail_from_name', 'my_mail_from_name' );
function my_mail_from_name( $name ) {
return "My Name";
}

The functions.php file

That’s great, but what do I do with it?

I’m glad you asked.  Add these two filters and their accompanying functions to your theme’s functions.php file.  You don’t even need to mess with a file editor and ftp for this.  I would simply go to the Appearance > Editor menu in the WP Admin Panel, then find ‘functions.php’ in the list of theme files on the right.  Add these filters and you are in business.

Easy peasy lemon squeezy.  And no bloated plugin to slow down your site!

If you’d rather use a plugin to handle WordPress email configuration instead of the functions file, that’s covered in this post.

For more information on testing, troubleshooting, and changing your WordPress email configuration for wp_mail, here are some additional posts:

  • Testing your WordPress email settings for the wp_mail function – some information on wp_mail and a testing script you can use to make sure it is sending messages.
  • Troubleshooting wp_mail WordPress Email Configuration – not everything that can go wrong is directly a problem with WP. This post has information on host restrictions and other outside problems that should be checked.
  • WordPress Email Settings: Changing the wp_mail address with a simple plugin – here is a very simple and lightweight script you can load as a plugin to change the email address that WordPress sends email from.
  • Easy SMTP email settings for WordPress – how to change your WordPress email configuration to send email from a valid SMTP server with a simple script, no plugin required.
  • Facebook
  • Twitter
  • Email
  • Print
  • More
  • LinkedIn
  • Reddit
  • Tumblr
  • Pocket
  • Pinterest

Filed Under: WordPress, WP-Members Tagged With: code, functions, plugins, tips, WordPress, WordPress Email Configuration, WP-Members, wp_mail

Modify the ‘more’ link – removing the anchor link

By Chad Butler Leave a Comment

You have a blog running WordPress.  You like to use the ‘more’ link for post excerpts, but you have ads running at the top of the page and the ‘more’ link puts in an anchor by default that takes the user lower on the page.  How can you remove the anchor so the user goes right to the top of the page?  [Read more…]

  • Facebook
  • Twitter
  • Email
  • Print
  • More
  • LinkedIn
  • Reddit
  • Tumblr
  • Pocket
  • Pinterest

Filed Under: WordPress Tagged With: code, tips, webdev, WordPress

Write a simple WP plugin to add the trademark symbol (™) to your mark

By Chad Butler 1 Comment

You have a trademark you need to mark on your WordPress site, but you don’t want to search through the site to find each occurrence and update each post?  I have developed a plugin for you that will ™ or ® your content on the fly.  But if you are the do-it-yourselfer type, then here’s a way to make a simple plugin to filter every instance of your trademark in posts, pages, and titles.

It is assumed that the reader has a basic working knowledge of PHP as I am not going to go into explaining the particulars of the PHP constructs.  While you could just copy and paste, it will be easier if you know the basics.

Filter the Content

The first function will go through the_content and replace every instance of your trademark with your trademark(tm).  We will call this tm_the_content.  WP stores the content of a post or a page in the variable $content, which we will pass to the function.

function tm_the_content( $content )
{
   ... we'll add code here later ...
}

We will use str_replace to find the instances of Our-Trademark (replacing “Our-Trademark” with the actual trademark text you are working with) in $content and replaced it with Our-Trademark(tm).  Note, I am using &#0153 for the trademark symbol, but you could put in place <sup> superscript or <div> and CSS, or what suits your fancy.  I like keeping it simple, though.  (Note: you can also replace &#0153 with &#174; to get the registered trademark symbol (r) ).

$content = str_replace("Our-Trademark", "Our-Trademark™", $content);

Now $content has all instances of Our-Trademark replaced with our trademark(tm), but we need to send back the filtered content:

return $content;

Functions and Filters:

function tm_the_content($content)
{
  $content = str_replace("Our-Trademark", "Our-Trademark&#0153", $content);
  return $content;
}

WordPress stores the title of a post or a page in the variable $title.  Filtering $title is exactly the same as filtering $content, so we can build our function tm_the_title by essentially copying tm_the_content and changing $content to $title:

function tm_the_title($title)
{
  $title = str_replace("Our-Trademark", "Our-Trademark&#0153", $title);
  return $title;
}

And you thought this was going to be hard.  😉

Now we need to fire these functions at the appropriate time.  As I mentioned earlier, in WordPress, we do this by using add_filter.

In case you were unaware, WordPress has a built-in function called add_filter() that will fire a function to filter your content.  We will use two instances of add_filter() to filter both the_content, the function that gets the $content of a post or a page, and the_title, the function that gets, you guessed it, the $title.

When you use add_filter(), you need to send the function two things, what you are filtering, and the function to use to do the filtering:

add_filter( $tag, $function_to_add, $priority, $accepted_args );

The last two parameters are optional, and in this case, we will only be using the first two.  We will use this twice in our plugin to fire each of our functions:

add_filter('the_content', 'tm_the_content');
add_filter('the_title', 'tm_the_title');

Put it all together:

<?php
add_filter('the_content', 'tm_the_content');
function tm_the_content($content)
{
  $content = str_replace("Our-Trademark", "Our-Trademark&#0153", $content);
  return $content;
}

add_filter('the_title', 'tm_the_title');
function tm_the_title($title)
{
  $title = str_replace("Our-Trademark", "Our-Trademark&#0153", $title);
  return $title;
}
?>

Here is a sample of the code.

Well that’s it.  I hope that you will find this snippet of code useful.

  • Facebook
  • Twitter
  • Email
  • Print
  • More
  • LinkedIn
  • Reddit
  • Tumblr
  • Pocket
  • Pinterest

Filed Under: Blogging Tips, WordPress Tagged With: blogging, code, plugins, tips, tools, tutorials, WordPress

  • 1
  • 2
  • Next Page »

Join Us!

I will never share your information. No spam. No junk. No kidding. Unsubscribe anytime.


Recent Posts:

  • The Right Product at the Right Time
  • Top 3 Time Wasters Keeping You From Success
  • Top 8 Tips to Create Your Own Website Easily With WordPress
  • How to Fix wp_mail Settings for WordPress Email
  • 7 Reasons Why Social Networking Can Help Your Business
  • Understanding WordPress wp_mail and how to fix it
  • Prevent WordPress email sent to spam with this
  • Easy wp mail SMTP settings for WordPress
  • The Importance of Supporting Developers of Free Open Source Software
  • How to Run an Effective Meeting





Archives

  • About
  • Blog
  • Archive
  • Contact

Site powered by WordPress, running on the Genesis Framework from StudioPress.

Unless otherwise noted, content on this site is © 2006-2021 ButlerBlog and may not be reproduced without express written permission from the author.

Some content may include affiliate links for which this site receives a small commission.

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.