Skip Navigation

[Resolved] Post form for unsubscribe

This support ticket is created 5 years, 4 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 12 replies, has 2 voices.

Last updated by martinH-10 5 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1282927

Hi, I have got a Post form which creates a new custom post (mail-agent) with custom field checkbox. This CPT is not public. It contains email and it is used for autmatic mailing of news.

I need to set up 3 things:

1) Create public post form called Unsubscribe wich edit the CPT and place it on page called unsubscribe from list
2) Create field where user add its email which he wants to unsubscribe (email also should be added from url parameter)
3) After user clicks on send button, the post form do this:
- Look for every post in post type called Mail Agents if it contains the email
- When post contains the mail in custom field called email, then check the checkbox called unsubscribe.

How can I achieve this?

#1282941

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

When you say you will create post edit form - you need to set what entry/post you want to edit with the edit form. In your case I think you should add one dummy entry to display with edit form and display the form by creating the new page:

[cred_form form="form-slug" post="999"]

Where:
999 is your dummy entry to edit the post

Now, when user submit the form, you should use the Toolset Form's hook: cred_form_validate or cred_save_data to check the email in database and if found update the checkbox:
- you should use the hooks that suits your need
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#1283117
chekbox.png

Hi, thanks for the reply.

I created page for unsubscribe: hidden link
I also created one Dummy post wich is used to display the form.

I am not sure if I understand it well. I need to change the ID of form every time. Or this Dummy post form can work for every user?

I created this:

add_filter('cred_form_validate','unsub_mail',10,2);
function unsub_mail($error_fields, $form_data)
{
    list($fields,$errors)=$error_fields;
    //validate if specific form
    if ($form_data['id']==687)
    {
        //check my_field value
        if ($fields['wpcf-email-agent']['value']!='correct_value')
        {
            //set error message for my_field
            $errors['wpcf-email-agent']='Wrong Value';
        }
    }
    //return result
    return array($fields,$errors);
    add_post_meta($post_id, 'wpcf-odhlaseni', $_POST['my_custom_field'], true);
}

I am not sure how to save checkbox value in add_post_meta. I am sending screenshot of chekbox setting.

#1283139

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I am not sure if I understand it well. I need to change the ID of form every time. Or this Dummy post form can work for every user?
=> You need to add your original form ID , the dummy post should work for every user as logically user will submit the form with email and we will search that email in the database and if found we will checkmark the checkbox.

Can you please share access details and the checkbox field name?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1283693

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please remove the duplicate entries added using the email ID info@alessykora.cz.

Then try to add a brand new entry with email ID example abc@yahoo.com and then go to this form:
=> hidden link

Add the email value as abc@yahoo.com and you will see the checkbox is automatically checked as it has found the post with the email abc@yahoo.com.

I've added the following code to "Custom Code" section of Toolset:

add_action('cred_save_data', 'func_check_sub_email',10,2);
function func_check_sub_email($post_id, $form_data){
  
  		
    // if a specific form
    if ($form_data['id']==687){
      
      $args   =   array(
					'posts_per_page'   => -1,
					'post_type'     => 'agent',
					'meta_query'    => array(
    							array(
        						'key'       => 'wpcf-email-agent',
        						'value'     => $_POST['wpcf-email-agent'],
        						))
                     );
			$query = new WP_Query($args);
            if ( $query->have_posts() ) {
              	update_post_meta($query->pos->ID, 'wpcf-odhlaseni', 1);
            }else{
              	update_post_meta($query->pos->ID, 'wpcf-odhlaseni', 0);
            } 
        
    }
}
#1283829

I deleted All entries. Created new dummy and changed the id to 718 everywhere.

I created new post with email test@test.cz

Then I used the form and it doesnt work.

It changed the email in the dummy post (which should stay unmodified) and it doesnt changed the test@test.cz post. It does nothing with checkbox too.

#1283891

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now, I've adjusted the code a little bit.

add_action('cred_save_data', 'func_check_sub_email',10,2);
function func_check_sub_email($post_id, $form_data){
  
  update_post_meta($post_id, 'wpcf-email-agent','');
  		
    // if a specific form
    if ($form_data['id']==687){
      
      $args   =   array(
					'posts_per_page'   => -1,
					'post_type'     => 'agent',
					'meta_query'    => array(
    							array(
        						'key'       => 'wpcf-email-agent',
        						'value'     => $_POST['wpcf-email-agent'],
        						))
                     );
			$query = new WP_Query($args);
      		
            if ( $query->have_posts() ) {
             	if(metadata_exists('post', $query->post->ID, 'wpcf-odhlaseni'))
              		update_post_meta($query->post->ID, 'wpcf-odhlaseni', 1);
                else
                  add_post_meta($query->post->ID, 'wpcf-odhlaseni', 1);
                  
              
            }else{
              	update_post_meta($query->post->ID, 'wpcf-odhlaseni', 0);
            } 
        
    }
}

- You do not need to modify anything with the above code.

To test:
- You should create a new entry in backend and add the email address: abc@yahoo.com
- Go to the form on frontend and submit the email address abc@yahoo.com:
--- hidden link
- Check in the backend that checkbox is checked or not.

#1283895
2019-07-04_11-52-06.png
form.png

Ok, now, when I use front end form (id:216), to create new post, it dont save the custom field email.

Form is located here (for example - see the screenshot): hidden link

But When I create the post in the backend, it works.

Lets figure out, why the front end form not saving the email now. It use the custom code: dynamic-title with it only.

#1283925

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now, its working. I just put the following line of code inside the if condition: if ($form_data['id']==687){

   update_post_meta($post_id, 'wpcf-email-agent','');
#1283929

MAN! You are the boss of me!! Thank you sooooooo much!!! <3

#1283931

One last question,

Is it possible to fill the mail field from urlparam?

Just adding the urlparam="" to cred field?

And another one, do this check the checkbox on all custom posts agent? If someone add his email 2 times (two posts) it will be nice, tu unsubscribe him on both posts...

#1283953

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've split the ticket. Please mark resolve this one and we will continue with your new question using split ticket here:
=> https://toolset.com/forums/topic/split-post-form-for-unsubscribe-multiple-email-and-display-form-field-with-url-param-value/

#1287359

My issue is resolved now. Thank you!