Ask your WordPress questions! Pay money and get answers fast! (more info)

Post notification button function in post editor WordPress

  • SOLVED

Hello,

I am after a postbox button in the post editor that sends a notification email to a address.

I would like to be able to put the code in my functions.php

The box needs to look like this and appear below the publish box... [[LINK href="http://i.imgur.com/nIAfj3o.png"]]http://i.imgur.com/nIAfj3o.png[[/LINK]]

From using the code in the publish box, I have stripped out everything I don't need and am left with this...


<div id="submitdiv" class="postbox">
<div class="handlediv" title="Click to toggle"><br></div>
<h3 class="hndle"><span>Email Notification</span></h3>
<div class="inside">
<div class="submitbox" id="submitpost">
<div id="major-publishing-actions">
<div id="publishing-action">
<span class="spinner" style="display: none;"></span>
<input name="original_publish" type="hidden" id="original_publish" value="Update">
<input name="save" type="submit" class="button button-primary button-large" id="publish" accesskey="p" value="Send Email Notification">
</div>
<div class="clear"></div>
</div>
</div>
</div>
</div>



I would like the email body/subect to read this...


$firstname = get_post_meta($post->ID, 'first_name', true);
$surname = get_post_meta($post->ID, 'surname', true);
$email = get_post_meta($post->ID, 'surname', true);

To: $email
Subject: Your page has been updated.
From: admin email

'Hello '.$firstname.',\n\n
Your page has been updated.\n
<a href="'.get_permalink().'" target="_blank">Click here to view your page.</a>\n\n
From the team.\n
<a href="'.get_bloginfo('url').'" target="_blank">'.get_bloginfo('url').'</a>';



I'm not sure how the final code will work, but if the ajax spinner can be included somehow that would be cool so the user knows the mail has been sent. If not then no worries.

Fully working code that I choose to use will get the full prize pot.

Thanks
Josh

Answers (2)

2013-02-11

Christianto answers:

Please try this code
[[LINK href="http://pastebin.com/TvP1fVSZ"]]http://pastebin.com/TvP1fVSZ[[/LINK]]

We need to edit $posttype if this only appear on certain post type
function my_custom_notification_box() {
$posttype = array( 'post', 'page' );
foreach ($posttype as $type) {
add_meta_box(
'post_email_notification',
__( 'Email Notification'),
'print_my_custom_notification_box',
$type,
'side',
'core'
);
}
}


and please check your email address meta key, is it correct using key 'surname'?
$firstname = get_post_meta($postID, 'first_name', true);
$surname = get_post_meta($postID, 'surname', true);
$email = get_post_meta($postID, 'surname', true);


Josh Cranwell comments:

Hi Chritianto

This works perfect!

But the message outputs like this in the email. I'll have a little dig as to why but if you get this and know how to output as html that will help. Do I have to wrap the email in html tags?


Hello Stefano,\n\n Your page has been updated.\n <a href="http://mysite.com/individual/stefano/" target="_blank">Click here to view your page.</a>\n\n From the team.\n <a href="http://mysite.com" target="_blank">http://mysite.com</a>


Many Thanks


Christianto comments:

sorry, I didn't notice it when copy paste your message code,
special character in $message should be in double quote.

please change $message line, so it will use double quote:
$message = "Hello ".$firstname.",\n\n Your page has been updated.\n <a href='".get_permalink($postID)."' target='_blank'>Click here to view your page.</a>\n\n From the team.\n <a href='".get_bloginfo('url')."' target='_blank'>".get_bloginfo('url')."</a>";


Josh Cranwell comments:

Hi Christianto, it's all good, I used this below to format my emails.


$firstname = get_post_meta($postID, 'first_name', true);
$surname = get_post_meta($postID, 'surname', true);
$email = get_post_meta($postID, 'login_email', true);

$subject = get_bloginfo('name').' Notification';

$headers = 'From: '.get_bloginfo('name').' <'.get_bloginfo('admin_email').'>' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = '<html><body>';
$message .= 'Hello '.$firstname.',<br/><br/>';
$message .= 'Your page has been updated.<br/><br/>';
$message .= '<a href="'.get_permalink($postID).'" target="_blank">Click here to view your page.</a><br/><br/>';
$message .= 'From the team<br/>';
$message .= '<a href="'.get_bloginfo('url').'" target="_blank">'.get_bloginfo('url').'</a>';
$message .= '</body></html>';

$send_success = wp_mail( $email, $subject, $message, $headers, $attachments );

2013-02-10

phppoet answers:

use something like this .

function post_status($lateststatus, $previousstatus, $post) {
global $current_user;
global $post;

$author = get_userdata($post->post_author);
$aditionalmessage = get_post_meta( $post->ID, 'authormessage', true );
if ($previousstatus == 'pending' && $lateststatus == 'publish' && $current_user->ID!=$contributor->ID) {

$emailsubject='['.get_option('blogname').'] "'.$post->post_title.'" '.__('post approved','wepguest').'';
$notify="{$author->display_name},\n\n ".__('Congratulations , Your Submitted post at','wepguest')."".get_option('blogname')." ".__('has been approved.You Can View Your Post at','wepguest')."
".get_permalink($post->ID)." .\n\n";
$notify.="".__('Additional Message For You','wepguest').":-\n\n";
$notify.="".$aditionalmessage."\n\n";
$notify.="".__('Email Sent Via wp guest posting plugin','wepguest').".";
wp_mail( $author->user_email, $emailsubject, $notify);

} elseif ($previousstatus == 'pending' && $lateststatus == 'pending' && $current_user->ID!=$author->ID) {

$emailsubject='['.get_option('blogname').'] "'.$post->post_title.'" '.__('Post Rejected','wepguest').'';
$notify="{$author->display_name},\n\n ".__('Sorry, Your Submitted post at','wepguest')."".get_option('blogname')." ".__('has been rejected.You can Resubmit Your post at','wepguest')." ".get_option('siteurl')."/wp-admin/post.php?action=edit&post={$post->ID} ( ".__('only for contributors','wepguest').").\n\n";
$notify.="".__('Additional Message For You','wepguest').":-\n\n";
$notify.="".$aditionalmessage."\n\n";
$notify.="".__('Email Sent Via wp guest posting plugin','wepguest').". ";
wp_mail( $author->user_email, $emailsubject, $notify);
}

}

add_filter('transition_post_status', 'post_status',10,3); function post_status($lateststatus, $previousstatus, $post) {
global $current_user;
global $post;

$author = get_userdata($post->post_author);
$aditionalmessage = get_post_meta( $post->ID, 'authormessage', true );
if ($previousstatus == 'pending' && $lateststatus == 'publish' && $current_user->ID!=$contributor->ID) {

$emailsubject='['.get_option('blogname').'] "'.$post->post_title.'" '.__('post approved','wepguest').'';
$notify="{$author->display_name},\n\n ".__('Congratulations , Your Submitted post at','wepguest')."".get_option('blogname')." ".__('has been approved.You Can View Your Post at','wepguest')."
".get_permalink($post->ID)." .\n\n";
$notify.="".__('Additional Message For You','wepguest').":-\n\n";
$notify.="".$aditionalmessage."\n\n";
$notify.="".__('Email Sent Via wp guest posting plugin','wepguest').".";
wp_mail( $author->user_email, $emailsubject, $notify);

} elseif ($previousstatus == 'pending' && $lateststatus == 'pending' && $current_user->ID!=$author->ID) {

$emailsubject='['.get_option('blogname').'] "'.$post->post_title.'" '.__('Post Rejected','wepguest').'';
$notify="{$author->display_name},\n\n ".__('Sorry, Your Submitted post at','wepguest')."".get_option('blogname')." ".__('has been rejected.You can Resubmit Your post at','wepguest')." ".get_option('siteurl')."/wp-admin/post.php?action=edit&post={$post->ID} ( ".__('only for contributors','wepguest').").\n\n";
$notify.="".__('Additional Message For You','wepguest').":-\n\n";
$notify.="".$aditionalmessage."\n\n";
$notify.="".__('Email Sent Via wp guest posting plugin','wepguest').". ";
wp_mail( $author->user_email, $emailsubject, $notify);
}

}

add_filter('transition_post_status', 'post_status',10,3);


Josh Cranwell comments:

Thanks PHPpoet for your answer

Though I used Christianto answer