• I am trying to mail using a php switch structure according to category ID but I can’t seem to properly extract the ID that I am comparing, I am looking at the url /term.php?taxonomy=category&tag_ID=44 to get the actual ID I need but I always drop out of the bottom of the switch without evaluating true (herewith an abbreviated example). What am I doing wrong?

    	$categories = get_the_category($post_id);
     
    	if ( ! empty( $categories ) ) {
    		$category_id =( $categories[0]-> term_id );  
    	}
    
    	switch ($category_id) {
        case 44 : //"Announcements":
    	$mailgun_listaddress = "[email protected]";
            $subject = "An Announcement from CLE term8id=". $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 - id:'. $category_id;
          	wp_mail( '[email protected]', $subject, $message );
    	}
    • This topic was modified 7 years, 6 months ago by Steven Stern (sterndata). Reason: put code in backticks; moved to fixing from developing

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    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.

    Thread Starter patbell101

    (@patbell101)

    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 7 years, 6 months ago by patbell101.
    • This reply was modified 7 years, 6 months ago by patbell101.
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    If it’s empty, $category_id is defined, so yo have an error condition. This is just basic PHP debugging.

    Thread Starter patbell101

    (@patbell101)

    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’ );

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Category_id switch’ is closed to new replies.