Tips and Tricks HQ Support Portal › Forums › WP eMember › eMember Smart Form Integration
- This topic has 7 replies, 2 voices, and was last updated 7 years ago by
wzp.
-
AuthorPosts
-
February 8, 2018 at 12:58 am #14688
msaohiostate
MemberHi,
I am trying to get Smartforms to work with eMember as I would like to limit a form to one time submissions. The developer gave me a code that will work if used by standard wp members but I am not sure if it is going to work with eMember. I would greatly appreciate any advice on how to make it work or how to proceed in general.
Heres the code
<?php
/**
Plugin Name: One time submission
*/
add_filter(‘sf_before_saving_form’,’smart_forms_one_time_submission’);
function smart_forms_one_time_submission($preInsertEntry)
{
if ($preInsertEntry->FormId != 162)
{
return;
}
require_once SMART_FORMS_API;
$query = new SmartFormsQuery(162);
$query->AddCondition(‘_UserId’,’=’, get_current_user_id());
$result = $query->GetResults();
if (count($result) > 0)
{
$preInsertEntry->ContinueInsertion = false;
$preInsertEntry->AddAction(new ShowMessageInsertEntryAction(“Sorry you can not submit this form again”));
}
}
Thanks
February 8, 2018 at 2:14 am #77248wzp
ModeratorThe developer gave me a code that will work if used by standard wp members but I am not sure if it is going to work with eMember.
$query->AddCondition('_UserId','=', get_current_user_id());
This is the code segment that is WordPress User ID specific. There are 2 ways to handle this:
- Create matching WordPress User ID for each eMember user, and have them automatically logged into WordPress, when they log into eMember.
- Replace the get_current_user_id() function with the equivalent eMember PHP calls. Refer to the section “Get member id and his/her membership level” here:
https://www.tipsandtricks-hq.com/wordpress-membership/wp-emember-miscellaneous-tweaks-248
February 9, 2018 at 3:19 am #77249msaohiostate
MemberDo you think you’d be able to alter it for me please. If tried several times but I still can’t seem to get it to work. What do I have to add to this code to make it work.
Thanks
February 9, 2018 at 5:03 am #77250wzp
ModeratorIn the code that you were provided, replace this line:
$query->AddCondition('_UserId','=', get_current_user_id());
With the following:
$emember_auth = Emember_Auth::getInstance();
$query->AddCondition('_UserId','=', $emember_auth->getUserInfo('member_id'));February 9, 2018 at 6:49 am #77251msaohiostate
MemberStill not working unfortunately
February 9, 2018 at 1:04 pm #77252wzp
ModeratorPlease be more specific than “Still not working.”
- Are you a programmer?
- Does the code that you “received from the developer” of the other plugin actually work, with regular WP users?
- Where are you inserting this code?
- What exactly happens when you execute this code?
If you are not a programmer, you might want to consider using our custom coding service:
February 13, 2018 at 3:09 pm #77253msaohiostate
MemberI apologize for the ambiguity.
This plugin is called Smart Forms, its a plugin that allows for more complex forms to be inserted in to WP. We are currently using this plugin to run our elections as a student organization. We are currently collecting applications which is working fine however in the near future we will be voting and we would like to restrict voting to a one time submission. I reached out to the developer and he adjusted to code to only allow one submission per WP user however since we use WP eMember for our database it does seem to be compatible. He advised us to alter the code according to your php shortcodes etc however that didn’t solve the issue. The current issue is that eMembers can still submit more than one submission.
Here is the code for to php files, the 1st is part of the original plugin that should connect to the current user database and the 2nd is the full code for the extension for one time submission validation.
1st:
private function InsertEntryData(){
$emember_auth = Emember_Auth::getInstance();
$user_id = $emember_auth->getUserInfo(‘user_name’);
$currentuser=$user_id;
global $wpdb;
$result= $wpdb->insert(SMART_FORMS_ENTRY,array(
‘form_id’=>$this->FormId,
‘user_id’=>$currentuser->ID,
‘date’=>date(‘Y-m-d H:i:s’),
‘data’=>$this->FormString,
‘ip’=>$_SERVER,
‘reference_id’=>$this->ReferenceId
));
if($result==false)
return false;
$this->EntryId=$wpdb->insert_id;
$this->FormEntryData[“_formid”]=$this->EntryId;
$this->InsertedValuesString[“_formid”]=$this->EntryId;
$result=$this->ParseAndInsertDetail($this->EntryId,$this->FormEntryData,$this->GetFormElementsDictionary());
return $result;
}
2nd:
<?php
/**
Plugin Name: One time submission
*/
add_filter(‘sf_before_saving_form’,’smart_forms_one_time_submission’);
function smart_forms_one_time_submission($preInsertEntry)
{
if ($preInsertEntry->FormId != 21)
{
return;
}
require_once SMART_FORMS_API;
$query = new SmartFormsQuery(21);
$emember_auth = Emember_Auth::getInstance();
$user_id = $emember_auth->getUserInfo(‘user_name’);
$query->AddCondition(‘_UserId’,’=’, $user_id);
$result = $query->GetResults();
if (count($result) > 0)
{
$preInsertEntry->ContinueInsertion = false;
$preInsertEntry->AddAction(new ShowMessageInsertEntryAction(“Sorry you can not submit this form again”));
}
}
If we need to proceed with custom coding we have no problem however if its a simple fix we appreciate the help.
February 13, 2018 at 6:00 pm #77254wzp
ModeratorIf we need to proceed with custom coding we have no problem however if its a simple fix we appreciate the help.
Here is the “simple” fix. It is “simple,” because it corrects an obvious typecasting error in the code…
Please look back at the original code that you posted to this thread. In it, you have a line of code that reads:
$query->AddCondition('_UserId','=', get_current_user_id());
The function get_current_user_id() returns the User ID of the current WP User. Per the WP Codex, this is an INTEGER value:
https://codex.wordpress.org/WPMU_Functions/get_current_user_id
Now then, in the code modification I suggested; I told you to replace that line of code with:
$emember_auth = Emember_Auth::getInstance();
$query->AddCondition('_UserId','=', $emember_auth->getUserInfo('member_id'));Which was supposed to use the eMember ID of the current eMember; which is also an INTEGER value.
Instead of using the code I suggested, you insisted on using:
$emember_auth = Emember_Auth::getInstance();
$user_id = $emember_auth->getUserInfo('user_name');
$query->AddCondition('_UserId','=', $user_id);It doesn’t work, because you are using the member’s name STRING value, instead of the member’s ID INTEGER value.
Now then, I am making the assumption that you were provided working code that already works; and what you needed from us, was to show you how to make the code use the eMember ID, instead of the WP User ID.
I also assume that you know where to place the code, the other developer provided you.
But if you are not a programmer, or are simply “throwing code at the screen, to see what works,” then you need our custom coding service.
- Create matching WordPress User ID for each eMember user, and have them automatically logged into WordPress, when they log into eMember.
-
AuthorPosts
- You must be logged in to reply to this topic.