這對許多 WordPress 網站似乎很有用,但我找不到完整的代碼。
我們有一個有效的代碼片段,可以在貢獻者的帖子發布時通知他們。但是由于我們在前端也有用戶提交的帖子,我想要一條不同的訊息在他們的帖子發布時通知訂閱者。
我能找到的大多數代碼都是基于當前用戶的,但這會回應誰發布了帖子,而不是誰寫的。
所以這是我們的嘗試 - 有 3 條訊息。但到目前為止,它總是發送最終訊息。有人可以發現錯誤嗎?
少校謝謝。
function notifyauthor($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "Post Published: ".$post->post_title."";
//Gets all the data of the author
$authorData = get_userdata( $author );
//checks if the post author has the role of Author or above
if (in_array( 'author', 'postseditor', 'editor', 'administrator', $authorData->roles)) {
//message for authors in Author role and above
$message = "
Dear ".$author->display_name.",
Your post, \"".$post->post_title."\" has just been published.
View your post: ".get_permalink( $post_id )."
the rest of my message 1"
; }
elseif (in_array( 'contributor', $authorData->roles)) {
//message for authors in Contributor role
$message = "
Dear ".$author->display_name.",
Your post, \"".$post->post_title."\" has just been published.
View your post: ".get_permalink( $post_id )."
the rest of my message 2"
; }
else {
//message for authors in Subscriber role
$message = "
Dear ".$author->display_name.",
Your post, \"".$post->post_title."\" has just been published.
View your post: ".get_permalink( $post_id )."
the rest of my message 3"
; }
wp_mail($author->user_email, $subject, $message);
}
add_action('publish_post', 'notifyauthor');
uj5u.com熱心網友回復:
看起來您的比較功能不正確。
根據您的其他評論,這會將指定的電子郵件發送給具有特定角色的用戶。
function notifyauthor( $post_id ) {
$post = get_post( $post_id );
$author = get_userdata( $post->post_author );
$subject = 'Post Published: ' . $post->post_title . '';
if ( in_array( 'author', (array) $author->roles, true ) ) {
// message for authors in Author role and above.
$message = '
Dear ' . $author->display_name . ',
Your post, "' . $post->post_title . '" has just been published.
View your post: ' . get_permalink( $post_id ) . '
the rest of my message 1';
} elseif ( in_array( 'contributor', (array) $author->roles, true ) ) {
// message for authors in Contributor role.
$message = '
Dear ' . $author->display_name . ',
Your post, "' . $post->post_title . '" has just been published.
View your post: ' . get_permalink( $post_id ) . '
the rest of my message 2';
} else {
// message for authors in Subscriber role.
$message = '
Dear ' . $author->display_name . ',
Your post, "' . $post->post_title . '" has just been published.
View your post: ' . get_permalink( $post_id ) . '
the rest of my message 3';
}
wp_mail( $author->user_email, $subject, $message );
}
add_action( 'publish_post', 'notifyauthor' );
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/364242.html
標籤:php WordPress的
