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 6 years, 3 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 6 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#1082997

Shane
Supporter

Languages: English (English )

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

Hi Steffen,

No problem 🙂

Yes we can critieria's based on a custom field or taxonomy. In this case I would recommend using a custom field since you can have Fulltime , Part Time or Both

Thanks,
Shane

#1083120
Job Alarm.png
job form.png

Hey Shane,

here is what i did.

1. Created a custom post type "jobs" with a form and a field called "positions" (slug: position-normal) to match with the job alarms field
2. Created a custom post type called "job alarms" with a form and an email-field (to collect the Email) and a field called "position (alarm)" (slug: position-alarm) with the identical select options like in the job post.

So whats the next steps to take? In the old post from 2012 is something mentioned to create another CRED for sending the mail? is this an empty CRED only with an E-Mail notification?

cheers

#1083144

Shane
Supporter

Languages: English (English )

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

Hi Steffen,

This should be fine now. Just for me to adjust the code for you.

Would you mind allowing me access so that I can personalise the code for your case?

Thanks,
Shane

#1083196

Shane
Supporter

Languages: English (English )

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

Hi steffen,

I tried logging in with the credentials but they don't seem to work.

Could you check on them and let me know.

Thanks,
Shane

#1083197

sorry found the mistake. Just try again.

PW: %uy@rfBiz8U&OD$P(IsEfh5C

#1083237

Shane
Supporter

Languages: English (English )

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

Hi Steffen,

The code should be working now.

This is the final version.


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);
        $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);
            if ( $job_type == $notification_type) {
                wp_mail($email, 'New post published', get_permalink($post_id));
            }
        }
    }
}

Please try and let me know if it works.
Thanks,
Shane

#1083564

Wow, great!
Thank you shane i will check the code this evening.
Could you just give me an example if i want to add more fields to the code?
And when there ate more than one field in the function. Will the email only be send when all fields are matching or just one of them?
Would prefer that all fields have to match.

I give you a shout when i have tested the function.
Again thanks a lot!!! ??
Cheers!!

#1083770

Shane
Supporter

Languages: English (English )

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

Hi Steffen,

Right now its set to only when the job-alarm and the position-normal fields are the same.

However we can add more conditions to the code as well.

Thanks,
Shane

#1083792

Hey shane,

your a genius!!!!! it works fine. You need to give this to your devs to build this into toolset!!!!! 😉

So i need to ad more fields. Could you do an example in the code?

cheers

#1083804

Shane
Supporter

Languages: English (English )

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

Hi Steffen,

When you say add more fields, you mean add more to the condition or add more to the actual email.

Please let me know.

Thanks,
Shane

#1083805

1. i need to know how i have to extend your code to add more conditions.
So maybe you can make an example.

2. Can i style the email? and add more text? What do you mean with fields in email?

cheers

#1083867

Shane
Supporter

Languages: English (English )

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

Hi Steffen,

1. To add more conditions it would be like this.

add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data) {
    if ( $form_data['id'] == 67) {
        // Add another line like this below changing the $job_type variable name to another name and change wpcf-position-normal to your custom field slug keeping the wpcf- prefix.
        $job_type = get_post_meta($post_id, 'wpcf-position-normal',true); 
      // Example
      //   $job_city = get_post_meta($post_id, 'wpcf-city',true); 
        $notifications = get_posts('post_type=job-alarm');
        foreach ($notifications as $notification) {
            $email = get_post_meta($notification->ID, 'wpcf-e-mail', true);
         // You will need to do the same for the line below as I did with Job city, to get the notification city to check if it matches.
            $notification_type = get_post_meta($notification->ID, 'wpcf-position-alarm',true);
        // Then you can add the condition for the job_city in the if statement below. Example 
            if ( $job_type == $notification_type || $job_city == $notification_city ) {
                wp_mail($email, 'New post published', get_permalink($post_id));
            }
        }
    }
}

2. For the customization the mail you will need to edit the wp_mail() function.

Example.

                wp_mail($email, 'New post published', get_permalink($post_id)." You Can write anything you want here :)");

Please let me know if this helps.

Thanks,
Shane

#1084410

Thanks shane,

I will try to extend the code this evening.

Do you know how this function reacts on a multi checkbox field? Do the fields have to match exactly, too?
Or is it possible for multi checkboxes only have to match one of the selected possibilities to trigger the mail. This is what i would prefer and maybe we can add this?

Cheers.

#1084649

Shane
Supporter

Languages: English (English )

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

Hi Steffen,

Multi checkbox would respond differently,

I would need to check how the multi field is returned but I should be able to accomplish this with one of the php functions by comparing the arrays to see if they are the same.

Thanks,
Shane

#1084657

Hey shane,

this would be so great. So when it is a multi checkbox only one of the selected have to match to trigger.

I have set up one multi select to the job alarm form

<div class="form-group">
		<label>Stelle ist geeignet für (Alarm)</label>
	[cred_field field='stelle-ist-geeignet-fur-alarm' value='' urlparam='' output='bootstrap']
	</div>

and built it in the function:

//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));
            }
        }
    }

can you modify it?

cheers