Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a wordpress plugin installed called Postie - it enables me to post to my wordpress by sending an email to a specific email address.

I need help with some coding in order to make it so it will post to a specific category depending on the sender's @domain address from their email.

so if the email is from XXX.com, post all of their posts to X category, but if its from ZZZ.com, post it to Z category.

--- this is the sample filter file ----

<?php
/*
Plugin Name: Postie Filter
Plugin URI: http://blog.robfelty.com/plugins/postie
Description: Adds my own custom filter to messages posted by postie
Version: 1.4
Author: Robert Felty
Author URI: http://blog.robfelty.com/
*/

/* 
* Any filter function you write should accept one argument, which is the post
array, which contains the following fields:
'post_author' 
'comment_author' 
'comment_author_url' 
'user_ID' 
'email_author' 
'post_date' 
'post_date_gmt' 
'post_content' 
'post_title' 
'post_modified' 
'post_modified_gmt' 
'ping_status' 
'post_category' 
'tags_input' 
'comment_status' 
'post_name' 
'post_excerpt' 
'ID' 
'customImages' 
'post_status' 

Your function can modify any of these fields. It should then return the array
back.

Two example functions are provided here
*/

function filter_content($post) {
  //this function prepends a link to bookmark the category of the post
  $this_cat = get_the_category($post['ID']);
  //var_dump($this_cat);
  $link = '<a href="' . get_category_link($this_cat[0]->term_id) . 
  '">Bookmark this category</a>' . "\n";
  $post['post_content'] = $link . $post['post_content'];
  return ($post);
}

function filter_title($post) {
  //this function appends "(via postie)" to the title (subject)
  $post['post_title']= $post['post_title'] . ' (via postie)';
  return ($post);
}

function auto_tag($post) {
  // this function automatically inserts tags for a post
  $my_tags=array('cooking', 'latex', 'wordpress');
  foreach ($my_tags as $my_tag) {
    if (stripos($post['post_content'], $my_tag)!==false)
      array_push($post['tags_input'], $my_tag);
  }
  return ($post);
}

function add_custom_field($post) {
  //this function appends "(via postie)" to the title (subject)
  add_post_meta($post['ID'], 'postie', 'postie');
  return ($post);
}

add_filter('postie_post', 'filter_title');
add_filter('postie_post', 'filter_content');
add_filter('postie_post', 'add_custom_field');
add_filter('postie_post', 'auto_tag');
share|improve this question
2  
You realize that WordPress has post-by-email functionality built into the Core, right? Look at Settings->Writing. –  s_ha_dum Apr 19 '13 at 23:20
    
Yes but it doesn't allow HTML email postings, only text. I need it to allow HTML email format, and the Postie plugin allows that –  r jones Apr 19 '13 at 23:28
    
I assume you have looked around the hooks in that subsystem? –  s_ha_dum Apr 20 '13 at 0:09
    
I don't even know what that means.. –  r jones Apr 20 '13 at 0:16
    
:) WordPress is full of "hooks". You can attach functions to these hooks to alter the behavior of substantial parts of the WordPress Core. I am not very familiar with that email-to-post system but I'd would be surprised if it didn't have hooks. You may be able to alter that system to do what you want rather that scratch something together from nothing. –  s_ha_dum Apr 20 '13 at 0:19

1 Answer 1

I didn't install the plugin to test this code.

You have to search the code to confirm it but the 'email_author' seems to be the author email for this post. Why it is not named 'author_email' is anyone's guess.

We should be able to add some code that checks for the incoming email:

if ( 'zzz.com' == $post['email_author'] ) {
    // Add category Z.
} else if ( 'yyy.com' == $post['email_author'] ) {
    // Add category Y.
}

But, first, let's back up a bit. Your filter functions are all being called by the same hook ('postie_post'). (A filter is a hook. The other type of WordPress hook is an action.) So you can place everything in the same function:

add_filter( 'postie_post', 'postie_custom_changes' );

/**
 * Make several custom changes to a Postie post.
 */
function postie_custom_changes( $post ) {

    if ( 'zzz.com' == $post->'email_author' ) {
        // Add category X.
    } else if ( 'zzz.com' == $post->'email_author' ) {
        // Add category Y.
    }

    // Prepend a link to bookmark the category of the post.
    $category = get_the_category( $post['ID'] );
    $link = '<a href="' . get_category_link( $category[0]->term_id ) . '">Bookmark this category</a>' . "\n";
    $post['post_content'] = $link . $post['post_content'];

    // Appends "(via postie)" to the title (subject).
    $post['post_title'] = $post['post_title'] . ' (via postie)';

    // Insert tags for a post.
    foreach ( $my_tags as array( 'cooking', 'latex', 'wordpress' ) ) {

        if ( stripos( $post['post_content'], $my_tag ) !== false )
            array_push( $post['tags_input'], $my_tag );
    }

    // Append "(via postie)" to the title (subject).
    add_post_meta( $post['ID'], 'postie', 'postie' );

    return $post;
}

The other changes I made were to the comments, the indentation and the I changed $this_cat to $category because abbreviating the word category bugs the heck out of me. :)

It looks like the 'postie_post' filter is run after the post has been created. A Google search of the Internet pipes and tubes turns up the WordPress function wp_set_post_categories()

The first parameter is a is the post ID and the second is an array containing the category IDs you want to set the post to. From your code it looks like you are expecting only one category per post, so let's look at code to change the post category, not add it.

// Replace categories on some posts.
if ( 'zzz.com' == $post['email_author'] ) {
    wp_set_post_categories( $post['ID'], array( 2 ) );

} else if ( 'yyy.com' == $post['email_author'] ) {
    wp_set_post_categories( $post['ID'], array( 1 ) );
}

Assuming that category Y has an id of 1 and category Z has an id of 2.

You might also want to change the $post['category'] setting as well.

// Replace categories on some posts.
if ( 'zzz.com' == $post['email_author'] ) {
    wp_set_post_categories( $post['ID'], array( 2 ) );
    $post['category'] = array( 2 );

} else if ( 'yyy.com' == $post['email_author'] ) {
    wp_set_post_categories( $post['ID'], array( 1 ) );
    $post['category'] = array( 1 );
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.