当用户创建帖子(挂起)时,发送允许他们发布的确认链接

时间:2013-09-05 作者:Cas

我还有一个问题要问你们!

我需要以下东西:

用户创建帖子(自动将其设置为待审批)向用户发送一封带有确认链接的电子邮件。用户单击该链接后,帖子将设置为已批准并自动发布。如何创建?Wordpress中是否有可用的挂钩来创建这样的确认链接?

1 个回复
最合适的回答,由SO网友:gmazzap 整理而成

您需要一个钩住post status transitions 发送和发送电子邮件,其中包含一个带有一些验证变量的链接和一个接受这些变量的函数,检查它们,如果一切正常,则更新帖子状态。

我认为以下代码是不言而喻的,注释提供了额外的帮助:

add_action(\'new_to_pending\', \'send_approve_link\');
add_action(\'draft_to_pending\', \'send_approve_link\');
add_action(\'auto-draft_to_pending\', \'send_approve_link\');
add_action(\'init\', \'approve_post\');

function set_html_content_type() { return \'text/html\'; }

function send_approve_link( $post ) {
  // get author of post
  $author = new WP_User($post->post_author);
  // create a key for security check and save as meta
  $check = md5( $author->user_email . $post->ID  );
  update_post_meta( $post->ID, \'_approve_key\', $check);
  // set the content for the email
  $content = \'<p>Please click following link to allow the publishing of your post: &quot;\';
  $content .= esc_html( get_the_title($post->ID) ) . \'&quot;</p>\';
  // create an url vor verify link with some variables: key, post id, and email
  $url = add_query_arg( array(\'email\'=>$author->user_email,\'approve\'=>$post->ID), site_url() );
  $content .= \'<p><a href="\' . $url . \'">Click to Confirm</a></p>\';
  $from = get_bloginfo(\'name\');
  $from_email = get_option(\'admin_email\');
  $headers = \'From: \' . $from . \' <\' . $from_email . \'>\' . "\\r\\n";
  // sending email in html format
  add_filter( \'wp_mail_content_type\', \'set_html_content_type\' );
  wp_mail( $author->user_email, \'Confirm your Post\', $content, $headers);
  remove_filter( \'wp_mail_content_type\', \'set_html_content_type\' );
}

function approve_post( ) {
  if ( isset($_GET[\'approve\']) && isset($_GET[\'email\']) ) {
    // have we all variable needed?
    if ( empty($_GET[\'approve\']) || ! filter_var($_GET[\'email\'], FILTER_VALIDATE_EMAIL) ) return;
    // get post
    $post = get_post($_GET[\'approve\']);
    // this post has an author?
    if ( ! isset($post->post_author) ) return;
    // is the post already published?
    if ( $post->post_status == \'publish\' ) {
      wp_redirect( get_permalink($post->ID) );
      exit();
    } elseif ( $post->post_status != \'pending\' ) { // was the post deleted by admin?
      return;
    }
    // get the author
    $author = new WP_User($post->post_author);
    // check author variable
    if ( ! isset($author->user_email) ) return;
    // verify email
    if ( $_GET[\'email\'] != $author->user_email ) return;
    // verify key
    $key = get_post_meta( $post->ID, \'_approve_key\', true);
    if ( $key != md5( $author->user_email . $post->ID ) ) return;
    // ok, update status
    $post_data = array(\'ID\' => $post->ID, \'post_status\' => \'publish\');
    $update = wp_update_post($post_data);
    // update failed...
    if ( ! $update ) return;
    // delete verify key
    delete_post_meta($post->ID, \'_approve_key\');
    // work finished, view the post
    wp_redirect( get_permalink($post->ID) );
    exit();
  }
}
就这些了。请注意,代码为untested.

结束

相关推荐

当与“SETUP_POSTATA”一起使用时,“GET_POSTS”和“wp_GET_RENECT_POSTS”有什么不同?

这可能是一个完全“我对WP还是新手”类型的问题,但在使用get_posts() 和wp_get_recent_posts() 在我编写的自定义函数中setup_postdata(). 这是我的函数中的内容。php文件:<?php function getRecentPosts($total_posts = 2) { $total_posts = intval($total_posts); global $post; $args