根据电子邮件域发布到类别,用于电子邮件发布插件

时间:2013-04-19 作者:r jones

我安装了一个名为posite的wordpress插件,它使我能够通过向特定的电子邮件地址发送电子邮件来发布到wordpress。

我需要一些编码方面的帮助,以便它能够根据发件人的@域地址从他们的电子邮件中发布到特定类别。

所以如果邮件来自XXX.com, 将其所有帖子发布到X类别,但如果是从ZZZ.com, 将其发布到Z类别。

---这是示例筛选器文件----

<?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\');

1 个回复
SO网友:Charles Clarkson

我没有安装插件来测试这段代码

你必须搜索代码才能确认,但“email\\u author”似乎就是这篇文章的作者电子邮件。为什么它不被命名为“author\\u email”,这是任何人的猜测。

我们应该能够添加一些代码来检查传入的电子邮件:

if ( \'zzz.com\' == $post[\'email_author\'] ) {
    // Add category Z.
} else if ( \'yyy.com\' == $post[\'email_author\'] ) {
    // Add category Y.
}
但是,首先,让我们后退一点。您的筛选函数都被同一个钩子(“posite\\u post”)调用。(过滤器是一个钩子。另一种类型的WordPress钩子是一个动作。)因此,您可以将所有内容放在同一个函数中:

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;
}
我所做的其他更改是对注释、缩进和$this_cat$category 因为缩写“类别”这个词让我烦透了

“postie\\u post”筛选器似乎在创建帖子后运行。谷歌搜索互联网上的管道可以打开WordPress功能wp_set_post_categories()

第一个参数是a是帖子ID,第二个参数是一个数组,其中包含要将帖子设置为的类别ID。从您的代码来看,每个帖子似乎只需要一个类别,所以让我们看看更改帖子类别的代码,而不是添加它。

// 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 ) );
}
假设该类别Y id为1 和类别Z id为2.

您可能还想更改$post[\'category\'] 设置也一样。

// 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 );
}

结束

相关推荐

Functions.php Problem

当我在函数顶部添加第3-19行时。php,然后尝试更新图像。phpI get the error \"Warning: Cannot modify header information - headers already sent\" when I try to update my image.php.第3-19行的代码为“我的图像库”创建自定义的下一个/上一个链接,并将最后一个链接重定向到“更多库”页面。我的代码可以在这里看到:http://pastebin.com/Yen04t2z