What happens if $categories is empty? Do a vardump or print_r on $categories before you do the test. If it’s not emtpy, dump out $category_id right before you do the switch.
Well… if its empty, I want it to do the default, which it always does – probably because it IS empty… return print_r($categories) just returns array() many times.OK so what am I doing wrong , how can I get the actual new post category ID?
I seem to get some info from custom posts (Nextgen) when I disable thies it seems to get stuck in a loop mailing indefinitely
The rest of the code is below.
function my_project_updated_send_email( $post_id ) {
// If this is just a revision, don’t send the email.
if ( wp_is_post_revision( $post_id ) )
return;
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
if ( ! empty( $categories ) ) {
$category_id =( $categories[0]-> term_ID );
}
return print_r($categories);
$categories = get_the_category($post_id);
$message = “A post has been updated on your website:\n\n”;
$message .= $post_title . “: ” . $post_url . $category_id;
switch ($category_id) {
case 44 : //”Announcements”:
$mailgun_listaddress = “[email protected]”;
$subject = “An Announcement from CLE”. $category_id;
wp_mail( ‘[email protected]’, $subject, $message );
break;
case 9 : //”News”:
$mailgun_listaddress = “[email protected]”;
$subject = “News from CLE”. $category_id;
wp_mail( ‘[email protected]’, $subject, $message );
break;
default:
$mailgun_listaddress = “[email protected]”;
$subject = ‘An unhandled post has been posted €’. $category_id;
wp_mail( ‘[email protected]’, $subject, $message );
}
}
add_action( ‘save_post’, ‘my_project_updated_send_email’ );
-
This reply was modified 8 years, 9 months ago by
patbell101.
-
This reply was modified 8 years, 9 months ago by
patbell101.
If it’s empty, $category_id is defined, so yo have an error condition. This is just basic PHP debugging.
Sorry my first venture into actions,hooks and debugging. So basic is what I am. Trying to step beyond just implementation and falling at the first hurdle it seems.
I can’t get debug messages to appear at all now. Can’t see anything in the browser or the webconsole. Got the following in wp_config too.
define( ‘WP_DEBUG’, true );
define( ‘WP_DEBUG_LOG’, true );
define( ‘WP_DEBUG_DISPLAY’, true );
Nothing current in my debug.log or my server logs
Help! Surely the following fires on new and updated posts and MUST have the current $post_id? Not a lot to do wrong it seemed.
function my_project_updated_send_email( $post_id ) {
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
$categories = get_the_category($post_id);
return print_r($post_title);
return print_r($post_url);
return print_r($categories);
}
add_action( ‘save_post’, ‘my_project_updated_send_email’ );