{"id":151,"date":"2015-02-19T17:43:10","date_gmt":"2015-02-19T17:43:10","guid":{"rendered":"http:\/\/www.eminozlem.com\/?p=151"},"modified":"2015-06-06T21:29:59","modified_gmt":"2015-06-06T21:29:59","slug":"notify-user-for-post-status-change","status":"publish","type":"post","link":"https:\/\/eminozlem.com\/tr\/notify-user-for-post-status-change\/","title":{"rendered":"Notify user for post status change"},"content":{"rendered":"<pre class=\"lang:php decode:true  \">&lt;?php function notify_post_status( $post_id = NULL, $user = '' ) {\r\n\tif ( ! isset($_POST[\"post_id\"]) ) {\r\n\t\t$post_id = $_POST[\"post_id\"];\r\n\t}\r\n\telse {\r\n\t\treturn $post_id;\r\n\t}\r\n\t$post = get_post($post_id);\r\n\t$status = $post-&gt;post_status;\r\n\t$type = $post-&gt;post_type;\r\n\t$author_id = intval($post-&gt;post_author);\r\n\t$user = get_userdata( $author_id ); \r\n\t\r\n\t\/\/ignore admin\r\n\tif( $author_id == 1 ) return;\r\n\t\r\n\t\/\/ Which statuses should we send email for ?\r\n\t$send_statuses = array(\"publish\",\"draft\");\r\n\t\/\/ Send mails only for published (approved) || draft (rejected)\r\n\tif( ! in_array($status,$send_statuses)) return;\r\n\t\/\/ Only send for a specific custom post type ?\r\n\t\/\/if( $type != \"my_custom_post_type\" ) return $post_id;\r\n\r\n\t$title = $post-&gt;post_title;\r\n\t$content  = $post-&gt;post_content;\r\n    $blogname = get_bloginfo( 'name' );\r\n  \/\/  $to_admin = get_bloginfo( 'admin_email' );\r\n    $to = $user-&gt;user_email;\r\n\t$frommail = 'noreply@my_domain.com';\r\n    $permalink = get_permalink( $post_id );\r\n\r\n    $headers = sprintf( \"From: %s &lt;%s&gt;\\r\\n\", $blogname, $frommail );\r\n\t$subject = sprintf( __( 'Your post status : %s','my_textdomain' ), $status );\r\n\t$msg = sprintf( __( 'Your post titled: %s ','my_textdomain' ), $title ) . \"\\r\\n\";\r\n\t$msg .= sprintf( __( 'status has been changed to : %s','my_textdomain' ), $title ) . \"\\r\\n\";\r\n\t\r\n\tif($status == \"publish\" ) {\r\n\t\t$msg .= sprintf( __( 'You can view it here : %s' ), $permalink ) . \"\\r\\n\";\r\n\t}\r\n\r\n\t\/\/ Save sent email for each status so it doesnt send email every time\r\n\t$sent_mails = get_post_meta($post_id,'post_notif_sent_mails',true);\r\n\t\r\n\tif($sent_mails) {\r\n\t\t\/\/ have sent mail for this status ?\r\n\t\tif ( in_array($status,$sent_mails) ) {\r\n\t\t\treturn;\r\n\t\t}\r\n\t\telse {\r\n\t\t\t$sent_mails[] = $status;\r\n\t\t}\r\n\t}\r\n\telse {\r\n\t\t\/\/ no mail has been sent for this status, crate array, insert stat\r\n\t\t$sent_mails = array($status);\r\n\t}\r\n\t\/\/ send the mail, update the metadata\r\n\tupdate_post_meta($post_id,\"post_notif_sent_mails\",$sent_mails);\r\n\twp_mail( $to, $subject, $msg, $headers );\r\n}\r\n\r\n?&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;?php function notify_post_status( $post_id = NULL, $user = &#8221; ) { if ( ! isset($_POST[&#8220;post_id&#8221;]) ) { $post_id = $_POST[&#8220;post_id&#8221;]; } else { return $post_id; } $post = get_post($post_id); $status = $post-&gt;post_status; $type = $post-&gt;post_type; $author_id = intval($post-&gt;post_author); $user = get_userdata( $author_id ); \/\/ignore admin if( $author_id == 1 ) return; \/\/ Which statuses should&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1,2],"tags":[42,43,44],"class_list":["post-151","post","type-post","status-publish","format-standard","hentry","category-web","category-wordpress","tag-notification","tag-notify","tag-post-status"],"acf":[],"_links":{"self":[{"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/posts\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/comments?post=151"}],"version-history":[{"count":0,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/posts\/151\/revisions"}],"wp:attachment":[{"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/media?parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/categories?post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eminozlem.com\/tr\/wp-json\/wp\/v2\/tags?post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}