Skip Navigation

[Resolved] CRED questions

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

Last updated by WeiS2074 4 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1718925

Hi,

Based on my below function, I have some questions.
1. I have an edit form,

2. Is $_POST a wordpress global variable? If yes, why sometimes it needs to declare global $post before we use?

3. what is difference between $_POST and $post ?

4. what is difference between $form_data and $_Post?
My guess is that $form_data is the data haven't been saved into DB, it hasn't submit yet. However $_Post is saved.

5. Does any parameter passing to function are global variable? For example the parameters from below function.

6. How do I get the post id in below function? I tried $form_data['container_id'], get_post_id(), and they don't work.

7. what is the best way to see the value of the field while develop and debug?

function func_validate_custom_field($error_fields, $form_data){
//field data are field values and errors
list($fields,$errors)=$error_fields;

if ($form_data['id']==129 || $form_data['id']==310){
$args = array(
'meta_query' => array(
array('key' => 'wpcf-cf-url',
'value' => trim($_POST['wpcf-cf-url'])
)),
'post_type' => 'unwrap',
'posts_per_page' => -1
);
$posts = get_posts($args);

if (count($posts) > 0){
$ori_posts_id = $posts[0]->ID;

$ori_url = get_permalink( $ori_posts_id );
//set error message for my_field
$click_url = ' Click Me ';
$errors['wpcf-cf-url']= 'This wine has been posted. Go to the page. ' .$click_url ;
}
}

return array($fields,$errors);
}
add_filter('cred_form_validate','func_validate_custom_field',10,2);

#1719499

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

As per our support policy, we entertain only one question per ticket but I will try to answer all your questions here and kindly make sure to open a new ticket for your every new question you may have.

1. I have an edit form,
=>
OK

2. Is $_POST a wordpress global variable? If yes, why sometimes it needs to declare global $post before we use?
=>
You need to learn PHP.

$_POST is a superglobal variable of PHP. Please check the following:
- hidden link

$post is a global variable for WordPress:
- https://codex.wordpress.org/Global_Variables

3. what is difference between $_POST and $post ?
=>
As shared information in #2, $_POST is superglobal variable from PHP and $post belongs to WordPress.

4. what is difference betweec
My guess is that $form_data is the data haven't been saved into DB, it hasn't submit yet. However $_Post is saved.
=>
$form_data is the 2nd argument passed to the hook "cred_form_validate" which is build on top using $_Post. The form data is not submitted yet when you are using hook "cred_form_validate" with hook "cred_form_validate".

5. Does any parameter passing to function are global variable? For example the parameters from below function.
=>
No.

6. How do I get the post id in below function? I tried $form_data['container_id'], get_post_id(), and they don't work.
=>
What Post ID you would like to get? the ID of current post/page where you added your form?

7. what is the best way to see the value of the field while develop and debug?
=>
Please check the following links:
- hidden link

You can even use the var_dump(), for example:

var_dump($your_variable);
exit;
#1720585

5. Actually I saw many wordpress example, they just pass parameter into the function. What value do they pass in? or those parameter are name specified, for example $form_data is the current form data.
function func_validate_custom_field($error_fields, $form_data)

6. What Post ID you would like to get? the ID of current post/page where you added your form?
yes, I use get_the_id(), it works, does it has other way to fetch the current post id(base on my function example?

#1721745

Minesh
Supporter

Languages: English (English )

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

5. Actually I saw many wordpress example, they just pass parameter into the function. What value do they pass in? or those parameter are name specified, for example $form_data is the current form data.
function func_validate_custom_field($error_fields, $form_data)
==>
those parameters are depends what you pass to your filter or action hook function. You can check the full filter hook information with the following doc link:
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

6. What Post ID you would like to get? the ID of current post/page where you added your form?
yes, I use get_the_id(), it works, does it has other way to fetch the current post id(base on my function example?
==>
To get the current post ID within the "cred_form_validate" hook function, you can use:

global $post;
$current_post_id = $post->ID;
#1722455

My issue is resolved now. Thank you!