TheWebs
TheWebs

Reputation: 12923

WordPress Email, Custom Post types and Email Subscriptions

I have something a client wants me to build, and I can with wp_mail, but I am wondering if how it should be built is fessable - no they dont want to use third party websites or software.

Essentially a widget will take in the clients email address, with this we can:

Have some kind of interface so we can say that send out 5, 10, 15 posts of category x, y, x on a daily, weekly or monthly basis

Thats not hard, but the question is: how would I store the emails that come in? a new column?

Use these emails and a custom post type to create email templates, newsletters and so on that could be sent to a set of emails (in this case all emails stored for now) at a specified time.

This one isn't hard either, its the custom post type part, how would I create a custom post type that when a post is published the post is not published the same way a post is, or a page. but instead its stored like one, but I can use its content in an email body instead of displaying it like a post or page.

essentially I shouldn't be able to go to: site.come/email_templates/post_id

So the second one is a bit more complicated but I am wondering how you guys might approach this situation or idea.

Upvotes: 0

Views: 1094

Answers (1)

rafiki_rafi
rafiki_rafi

Reputation: 1237

Here are some thoughts when it comes to the e-mail subscription part. As for the custom post types - I don't have much experience with those, sorry :)

If you want a quick and easy solution for the e-mail subscriptions, create a wp option (see http://codex.wordpress.org/Function_Reference/add_option) that is essentially a hash table that maps categories to keys in the table.

For each category in the hash table, store an array of userIDs and/or e-mails of the users that are subscribed to that category.

Once you have this data structure in place, it's fairly easy to manipulate and use in with wp_mail. Here is some example code that I've written for one of my plugins:

        $subscribers = get_option('subscribers');
        $categories = get_the_category($post->ID);

        if( !empty($categories) && !empty($subscribers)){
            $emails = array();
            //Go through each category and accumulate the necessary e-mail addresses
            foreach($categories as $category){
                $catID = $category->term_id;
                if( !empty($subscribers[$catID]) ){
                    foreach($subscribers[$catID] as $userID => $trash){
                        $user      = get_userdata($userID);
                        $userEmail = array( $userID => $user->user_email );
                        if( !in_array($userEmail, $emails) ){
                            $emails   = $emails + $userEmail; 
                            //you can use something like implode(", ", $emails)
                            //in the Bcc: part when you send out the e-mail.
                        }
                    }
                }
            }
        }

Some things to note:

  • This is a quick and dirty solution. If the number of categories and number of subscribers grows big, you're better of creating a table in the database and maintaining it that way
  • Make sure to think of situations when categories are deleted (i.e. hook into actions when categories are deleted) and how that will affect your datastructure
  • The hash table approach works well assuming categories are NOT deleted/added frequently

Good luck!

Upvotes: 1

Related Questions