Skip Navigation

[Resolved] E-Mail Alert

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted his users to be able to subscribe to a notification type. Lets say the user has a Job type and wants to notify user when they subscribe to a notification of a particular job type.

Solution:

This can be done by creating a secondary CPT for the notifications where the user can create the notification type they want for a job.

So on this notification cpt it should have attributes from the Jobs CPT that can be used to match a notification for a Job type. The notification CPT must also have an email field.

Then you can use this code below to send an email to a notification post based on a matching custom field criteria.

//Job Alarm function
add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data) {
    if ( $form_data['id'] == 67) {
        $job_type = get_post_meta($post_id, 'wpcf-position-normal',true);
        $job_stelle = (types_render_field( 'stelle-ist-geeignet-fur', array( 'id' => ''.$post_id.'') ));
        $notifications = get_posts('post_type=job-alarm');
        foreach ($notifications as $notification) {
            $email = get_post_meta($notification->ID, 'wpcf-e-mail', true);
            $notification_type = get_post_meta($notification->ID, 'wpcf-position-alarm',true);
            $notification_stelle = (types_render_field( 'stelle-ist-geeignet-fur-alarm', array( 'id' => ''.$notification->ID.'') ));
            if ( $job_type == $notification_type && count(array_intersect(explode(",",$job_stelle), explode(",",$notification_stelle))) > 0) {
                wp_mail($email, 'Dein Job Alarm: '.get_the_title($post_id), get_permalink($post_id)."HTML CODE CAN GO HERE");
            }
        }
    }
}
This support ticket is created 5 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 61 replies, has 2 voices.

Last updated by SteffenM1628 5 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#1084660

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steffen,

This would mean we wouldn't need the select option from the job correct?

Or will the job still be using the select option but the notification will be using the checkboxes.

Please let me know.
Thanks,
Shane

#1084663

the checkbox field for the alarm is identical to the job checkboxes.

sorry but i do not understand your question right now.

#1084721

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steffen,

So both will be using the checkbox then this is what you will need to have.

//Job Alarm function
add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data) {
    if ( $form_data['id'] == 67) {
        $job_stelle = get_post_meta($post_id, 'wpcf-stelle-ist-geeignet-fur',true);
        $notifications = get_posts('post_type=job-alarm');
        foreach ($notifications as $notification) {
            $email = get_post_meta($notification->ID, 'wpcf-e-mail', true);
            $notification_stelle = get_post_meta($notification->ID, 'wpcf-stelle-ist-geeignet-fur-alarm',true);
            if ( $job_stelle == $notification_stelle ) {
                wp_mail($email, 'Dein Job Alarm', get_permalink($post_id));
            }
        }
    }

This should work.

Thanks,
Shane

#1084728

this is allready the code i have with the two fields. but the email is not send:

//Job Alarm function
add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data) {
    if ( $form_data['id'] == 67) {
        $job_type = get_post_meta($post_id, 'wpcf-position-normal',true);
		$job_stelle = get_post_meta($post_id, 'wpcf-stelle-ist-geeignet-fur',true);
        $notifications = get_posts('post_type=job-alarm');
        foreach ($notifications as $notification) {
            $email = get_post_meta($notification->ID, 'wpcf-e-mail', true);
            $notification_type = get_post_meta($notification->ID, 'wpcf-position-alarm',true);
			$notification_stelle = get_post_meta($notification->ID, 'wpcf-stelle-ist-geeignet-fur-alarm',true);
            if ( $job_type == $notification_type || $job_stelle == $notification_stelle ) {
                wp_mail($email, 'Dein Job Alarm', get_permalink($post_id));
            }
        }
    }
#1084806

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steffen,

Now i'm a bit confused.

Are you going to send the notification based on 2 fields or are you going to use a Single checkbox field on both posts ? Remember we did a single field check for the $job_type and notification_type.

With that the notification can only be one type. I though using the custom checkbox field would allow the user to create a single notification to notify them of the various items that were posted.

Please let me know.

Thanks,
Shane

#1084869

Hey Shane,
I am so sorry for confusing you.
I want to replicate the same form like in the jobs post type - for the job alarm.
That means the job alarm has multiple fields.
Select fields (single select) and multi checkboxes.

The function works fine till now. But the Email should be triggered when the single select fields exactly match and when one or mor things of the multi checkbox is matching.

Hope this explains my needs.

Cheers.

#1085815

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steffen,

I adjusted the code now and it should now be working fine 🙂

//Job Alarm function
add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data) {
    if ( $form_data['id'] == 67) {
        $job_type = get_post_meta($post_id, 'wpcf-position-normal',true);
		$job_stelle = get_post_meta($post_id, 'wpcf-stelle-ist-geeignet-fur',true);
        $notifications = get_posts('post_type=job-alarm');
        foreach ($notifications as $notification) {
            $email = get_post_meta($notification->ID, 'wpcf-e-mail', true);
            $notification_type = get_post_meta($notification->ID, 'wpcf-position-alarm',true);
			$notification_stelle = get_post_meta($notification->ID, 'wpcf-stelle-ist-geeignet-fur-alarm',true);
			;
            if ( $job_type == $notification_type || count(array_intersect($job_stelle, $notification_stelle)) > 0 ) {
                wp_mail($email, 'Dein Job Alarm', get_permalink($post_id));
            }
        }
    }
}

Please let me know if it helps.

Thanks,
Shane

#1086539

hey Shane,

just tried your code but i do get no emails.

#1087508

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steffen,

It actually works for me.

Remember that your notification settings must match at least 1 criteria on the job that is posted.

If there isn't any match it doesn't work.

Thanks,
Shane

#1088232

hey shane, sorry you are right the alarm is working. just found out that an email is not sent out when you create the jobs in the back end. when the job is created in the front end emails are sending. Thank you.

just few question we still haveabout the function:
1. can i style the outgoing email in any way (html)?
2. can i bring the titel of the job alarm in the mail? So a user knows which alarm is triggered?
3. about the code. it is nearly perfect. but one thing was maybe not explained correctly from my site.
all fields have to match before sending an email out.
in our example:

//Job Alarm function
add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data) {
    if ( $form_data['id'] == 67) {
        $job_type = get_post_meta($post_id, 'wpcf-position-normal',true);
        $job_stelle = get_post_meta($post_id, 'wpcf-stelle-ist-geeignet-fur',true);
        $notifications = get_posts('post_type=job-alarm');
        foreach ($notifications as $notification) {
            $email = get_post_meta($notification->ID, 'wpcf-e-mail', true);
            $notification_type = get_post_meta($notification->ID, 'wpcf-position-alarm',true);
            $notification_stelle = get_post_meta($notification->ID, 'wpcf-stelle-ist-geeignet-fur-alarm',true);
            ;
            if ( $job_type == $notification_type || count(array_intersect($job_stelle, $notification_stelle)) > 0 ) {
                wp_mail($email, 'Dein Job Alarm', get_permalink($post_id));
            }
        }
    }
}

we have till now two fields (job_type and job_stelle)
so the code should proof if the job_type is matching (yes-> check the next field (job_stelle*) if matching) (no-> do not check the next field and end the function).
*The multiple fields (job_stelle) are working great and this is correct to check if at least one thing is matching, so the function can go on to check the next field if one thing is matching.

at this time the function does send out an mail everytime if only one single item is matching. but the better way for the end user is to get notified only when all their attributes are matching.

The function should check one field after another and if only one field is not matching the function should stop beeing executed.

thanks and cheers!

#1088427

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steffen,

//Job Alarm function
add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data) {
    if ( $form_data['id'] == 67) {
        $job_type = get_post_meta($post_id, 'wpcf-position-normal',true);
        $job_stelle = get_post_meta($post_id, 'wpcf-stelle-ist-geeignet-fur',true);
        $notifications = get_posts('post_type=job-alarm');
        foreach ($notifications as $notification) {
            $email = get_post_meta($notification->ID, 'wpcf-e-mail', true);
            $notification_type = get_post_meta($notification->ID, 'wpcf-position-alarm',true);
            $notification_stelle = get_post_meta($notification->ID, 'wpcf-stelle-ist-geeignet-fur-alarm',true);
            ;
            if ( $job_type == $notification_type && $job_stelle == $notification_stelle ) {
                wp_mail($email, 'Dein Job Alarm: '.get_the_title($post_id), get_permalink($post_id)."HTML CODE CAN GO HERE");
            }
        }
    }
}

I've adjusted the code above. You should now get the post title as well as the notification should only send when all the fields are the same as the jobs.

I was under the impression that at least one field has to match.

Thanks,
Shane

#1088575

hey shane, oh man how can i explain more in detail how the alarm should behave.

first of of all the code is working fine. But i did modified it. because the multiselect field (notification_stelle) needs to have at least one attribute like in the multiselect from the job (job_stelle).
but the code count(array_intersect($job_stelle, $notification_stelle)) > 0 ) does only check if one random attribute (it doesn´t matter if there is a match or not) is selected.

here is the code i modified:

//Job Alarm function
add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data) {
    if ( $form_data['id'] == 67) {
        $job_type = get_post_meta($post_id, 'wpcf-position-normal',true);
        $job_stelle = get_post_meta($post_id, 'wpcf-stelle-ist-geeignet-fur',true);
        $notifications = get_posts('post_type=job-alarm');
        foreach ($notifications as $notification) {
            $email = get_post_meta($notification->ID, 'wpcf-e-mail', true);
            $notification_type = get_post_meta($notification->ID, 'wpcf-position-alarm',true);
            $notification_stelle = get_post_meta($notification->ID, 'wpcf-stelle-ist-geeignet-fur-alarm',true);
            ;
            if ( $job_type == $notification_type && count(array_intersect($job_stelle,  $notification_stelle)) > 0 ) {  //this must check that at least one attribute of the multiselect is matching to the job_stelle but it only checks if one is selected 
                wp_mail($email, 'Dein Job Alarm: '.get_the_title($post_id), get_permalink($post_id)."HTML CODE CAN GO HERE
				<h1>get_permalink($post_id</h1>");
            }
        }
    }
}

The function should check one field after another but if only one field is not matching the function should stop being executed.
When the fields do match check the next field.

So the problem is only the multi select field. it has to match at least one attribute to the job multi select attributes.

cheers

#1088577

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steffen,

It seems that you didn't use the latest code that I provided here https://toolset.com/forums/topic/e-mail-alert/page/3/#post-1088427

You're still using the one with the array_intersect function when I changed it.

Use this code here:

//Job Alarm function
add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data) {
    if ( $form_data['id'] == 67) {
        $job_type = get_post_meta($post_id, 'wpcf-position-normal',true);
        $job_stelle = get_post_meta($post_id, 'wpcf-stelle-ist-geeignet-fur',true);
        $notifications = get_posts('post_type=job-alarm');
        foreach ($notifications as $notification) {
            $email = get_post_meta($notification->ID, 'wpcf-e-mail', true);
            $notification_type = get_post_meta($notification->ID, 'wpcf-position-alarm',true);
            $notification_stelle = get_post_meta($notification->ID, 'wpcf-stelle-ist-geeignet-fur-alarm',true);
            ;
            if ( $job_type == $notification_type && $job_stelle == $notification_stelle ) {
                wp_mail($email, 'Dein Job Alarm: '.get_the_title($post_id), get_permalink($post_id)."HTML CODE CAN GO HERE");
            }
        }
    }
}

Notice that the IF statement was changed, so that it only fires if both conditions are met.

Thanks,
Shane

#1089220

yes i know shane but the function does not proof if only one multiselect attribute is matching.
it proofs that all multi select fields has to exactly match.

e.g. so the job has 5 multi select attributes (A,B,C,D,E) now in the function proofs that the job alarm has always to have A,B,C,D,E selected to trigger the mail.

But i need for the multi select to trigger the mail when at least one attribute matches.
so the alarm has only A,C,E and the mail should trigger. 3 attributes are matching to the job
so the alarm has only A the mail should trigger. 1 attribute is matching to the job
so the alarm has only F,G the should not trigger. 0 attributes

make sense now?

#1089509

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steffen,

I believe I get it now. I thought it should only trigger when everything matches.

Change the if statement to this

            if ( $job_type == $notification_type && count(array_intersect($job_stelle,  $notification_stelle)) > 0) {

This is what I had it to originally. What this statement above means is that it will only trigger if job type and notification type is the same and at least 1 of the notification stelle matches the job stelle.

Thanks,
Shane

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.