Hi
I managed to use access plugin to display form only for the logged in users using the if condition. What I would like to do is instead of not displaying the form at all for NOT logged in user but display the form and user can fill the form and after clicking on submit :
i want to check if user is logged in or not
If Not logged in then Login form to be displayed -- I am using social login plugin for the users to use gmail, facebook and etc.
I tried to use cred_before_save_data inside the plugin I created for Cred-recipient
Basically I want only logged in users can create a POST else display the form and allow the user to insert the data but not create the POST.
But I am getting an error and not able to proceed. Please help me.
Hi,
What is the "social login plugin" you mentioned above, is there any shortcode for rendering the Login form within the "social login plugin"?
If there is, you can use Views shortcode [wpv-conditional] to check if current user is a logged in user, if not, then display the login form shortcode, for example:
[wpv-conditional if="('[wpv-current-user info='id']' eq '')"]
Non logged in users, display the login form shortcode here
[/wpv-conditional]
[wpv-conditional evaluate="false" if="('[wpv-current-user info='id']' eq '')"]
Logged in users, display the CRED form shortcode here
[/wpv-conditional]
More help:
Conditional HTML Output in Views
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
And there is a built-in login form shortcode within Views plugin:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-login-form
This shortcode displays a standard WordPress login form.
For your reference
Thanks Luo.
But I actually want to display the form for all the users but wants to validate after clicking on submit of the form.
If you want to do custom validation after user submits the CRED form, please try CRED filter hook:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
This hook provides custom validation for form fields.
Hi Luo
I am trying to use this filter as you suggested but getting an error : syntax error, unexpected 'id' (T_STRING), expecting ']'
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$error_fields;
//uncomment this if you want to print the field values
//print_r($fields);
//validate if specific form
if ($form_data['id']==1)
{
if ($fields['wpv-current-user info]['id']!='')
{
//set error message for my_field
$errors['wpv-login-form redirect_url="hidden link" allow_remember="true" remember_default="true"']='Wrong Value';
}
}
}
My aim is simply validate if user logged in or not after clicking on submit.
Since it is a custom PHP codes problem, if you still need assistance for it, please provide a test site with the same problem, and fill below private detail box with login details and FTP access, also point out the problem page URL and CRED form URL, and where I can edit you PHP codes, I need a live website to test and debug, thanks
Since you have not pointed out the problem CRED form URL and page URL, so I assume we are talking about the CRED form "luoyang", ID "49297".
I have done below modifications in your website:
1) Enable guest user to use CRED form "luoyang":
Dashboard-> Toolset-> Access Control-> CRED Forms
In column 'Create Custom Post with CRED Form "LuoYang"', Enable option for "Guest"
2) Edit the CRED form "luoyang":
hidden link
disable option "AJAX submission"
The "AJAX submission" feature won't work in your case.
2) Create a page(ID 49307) to display login form:
hidden link
3) Create a page to display the CRED form "luoyang":
hidden link
[cred_form form="luoyang"]
4) Modify the PHP codes in your plugin file "Cred_Recipients", as below:
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$error_fields;
//uncomment this if you want to print the field values
//print_r($fields);
//validate if specific form
if ($form_data['id']==49297)
{
$current_user_id = get_current_user_id();
if($current_user_id == 0){ // It is a guest user
$login_form_url = get_the_permalink(49307); // login form URL
header('Location: ' . $login_form_url);
exit;
}
}
return array($fields,$errors);
}
Hi Luo
Thanks for this. It has solved 90% of the problem.
I changed //$login_form_url = get_the_permalink(49307); // login form URL to $login_form_url = 'hidden link';
Now If the user is not logged in and submit the form - User gets redirected to hidden link
But after user logged in :
It doesn't create the Post
Instead it takes the user to the Admin Page.
How can after logging in : Post should be created and user should be taken to the created post page?
Thanks
No, there isn't such kind of feature within CRED form, and the filter hook "cred_form_validate" can do validation of user inputs, and it will prevent the post from creating if it is not logged-in user, it can not achieve what you want:
Post should be created and user should be taken to the created post page
See the document I mentioned above:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
This hook provides custom validation for form fields.
But after user logged-in, you can redirect user to the page of CRED form for creating post:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-login-form
redirect_url (opt):
An absolute URL
If redirect_url attribute is set, visitors will be redirected to the specified URL.
For example, you can modify the login form shortcode to:
[wpv-login-form redirect_url="hidden link"