Search

שליחת אימייל כשמפורסם פוסט חדש באתר וורדפרס

הנה סניפט קצר שמאפשר לשלוח אימייל כאשר אתם מפרסמים פוסט חדש בבלוג שלכם. קוד זה יכול לעבוד גם עבור סוגי תוכן מותאמים (Custom Post Types).

add_action('future_to_publish', 'send_emails_on_new_event');
add_action('new_to_publish', 'send_emails_on_new_event');
add_action('draft_to_publish' ,'send_emails_on_new_event');
add_action('auto-draft_to_publish' ,'send_emails_on_new_event');
 
 
/**
 * Send emails on event publication
 *
 * @param WP_Post $post
 */
function send_emails_on_new_event($post)
{
    $emails = "email_1@mail.com, email_2@mail.com"; //If you want to send to site administrator, use $emails = get_option('admin_email');
    $title = wp_strip_all_tags(get_the_title($post->ID));
    $url = get_permalink($post->ID);
    $message = "Link to post: \n{$url}";
 
    wp_mail($emails, "New post published: {$title}", $message);
}

התוצאה תהיה משהו בסגנון הבא:

[Title] New post published: Your Post
    
Link to post:
http://site.co.il/your-post/

אם אתם רוצים לאפשר זאת עבור סוג תוכן מותאם כלשהו, תוכלו להוסיף תנאי בתוך הפונקציה כבדוגמה הבאה:

...
if(get_post_type($post->ID) === 'page') //post, page, attachment or whatever other CPT you may have
{
    //use wp_mail() here!
}
...
מצאתם טעות בקוד? הסניפט לא עובד לכם? רישמו לי בתגובות ואני מבטיח לטפל בכך במהרה ולספק סניפט תקין...
5 תגובות...
  • alon 12 ספטמבר 2018, 11:56

    היי,

    ניסיתי את מה שכתבת ומשום מה הייתי מקבל מיילים גם כשעדכנתי פוסטים.
    כרגע משתמש בסניפט שמצאתי בסטאקאוברפלואו שעובד יפה :

    
    /**
     * Send an email notification to the administrator when a post is published.
     * 
     * @param   string  $new_status
     * @param   string  $old_status
     * @param   object  $post
     */
    function wpse_19040_notify_admin_on_publish( $new_status, $old_status, $post ) {
        if ( $new_status !== 'publish' || $old_status === 'publish' )
            return;
        if ( ! $post_type = get_post_type_object( $post->post_type ) )
            return;
    
        // Recipient, in this case the administrator email
        $emailto = "EMAIL@gmail.com , EMAIL2@gmail.com ";
    
        // Email subject, "New {post_type_label}"
        $subject = "פוסט חדש ";
    
        // Email body
    	$url = get_permalink($post->ID);
        $message = "קישור לפוסט החדש: \n{$url}";
    
        wp_mail( $emailto, $subject, $message );
    }
    
    add_action( 'transition_post_status', 'wpse_19040_notify_admin_on_publish', 10, 3 );
    
  • רוב 15 דצמבר 2020, 9:21

    איך אפשר לעשות את זה לאנשים שנרשמים לאתר ולא למנהלים או מייל קבוע?
    (רק אחרי שכתבתי בפוסט אחר שלך, הצלחתי למצוא את הפוסט הזה)

    • רועי יוסף 19 דצמבר 2020, 15:09

      היי,

      לצערי התשובה רחבה יותר ואיני יכול לספק זו בתגובות אלו… בהצלחה 🙂

      • רוב 20 דצמבר 2020, 8:59

        תודה, אתה מכיר אולי הסבר בשביל זה?

תגובה חדשה

Up!
לבלוג