Elementor form custom webhook returns "error" message - forms

I'm using Elementor form with custom webhook but every time I submit it, I just get "error" message.
In my functions.php file I've got Form New Record Action according Forms API documentation.
// A send custom WebHook
add_action( 'elementor_pro/forms/new_record', function( $record, $handler ) {
$form_name = $record->get_form_settings( 'form_name' );
if ( 'test_form' !== $form_name ) {
return;
}
$raw_fields = $record->get( 'fields' );
$fields = [];
foreach ( $raw_fields as $id => $field ) {
$fields[ $id ] = $field['value'];
}
wp_remote_post( 'https://example.com', [
'body' => $fields,
]);
}, 10, 2 );
I have "wp_remote_post" there with URL I want to post the form to, but it does not redirect me or something, just returning "error" message.
On Elementor editor I added Webhook action after form submission
What could be wrong? Thanks

In the last example you need to fill in the wbbhook field at the appropriate URL
webhookfield

I had a similar error and it turned out it was a timeout.
By default, the timeout is set to only 5 seconds. This can be increased by using an add_filter in PHP. For example, if you are using make.com as the webhook, you can specify:
add_filter('http_request_timeout', function($timeout, $url = '') {
$start_with = 'https://hook.us1.make.com';
//return is_string($url) && str_starts_with($url, $start_with) // PHP 8
return is_string($url) && strncmp($url, $start_with, strlen($start_with)) === 0 // PHP 7 or older
? 30 // TODO: set appropriate timeout, WordPress default is 5 seconds
: $timeout; // return unchanged url for other requests
}, 10, 2);
This was the original issue I logged.
https://github.com/elementor/elementor/issues/20452

Related

Gravity Form Shortcode Confirmation

Hi I am echoing a shortcode of a gform but I need to check what the confirmation message is programmatically in php to call a function if successful and another if failed. Is it possible?
The confirmation message to be used is stashed in the GFFormDisplay::$submission property when the submission is processed so it can be retrieved when the form shortcode or block is processed on page render. You can access it like so:
$confirmation = rgars( GFFormDisplay::$submission, $form_id . '/confirmation_message' );
Alternatively you could use the gform_confirmation filter to override the confirmation to be used when the submission is being processed, before it is added to the GFFormDisplay::$submission property e.g.
add_filter( 'gform_confirmation', function ( $confirmation, $form, $entry ) {
if ( empty( $entry ) || rgar( $entry, 'status' ) === 'spam' ) {
// Return the default confirmation for spam.
return $confirmation;
}
// Check your condition here and replace the $confirmation if needed.
return $confirmation;
}, 11, 3 );

Yii2 Redirect to previous page after update

I have a application where after update user should be redirected to previous page from pagination.
let's say there is a gridview and user is at page 3. Then he update some record at that page. There should be a redirect to index page 3. What if, while user is updating record, before save, he opens another controller/action in new tab. Then ReturnUrl is now that new action and after save the record he is updating, he is redirected to that new url.
I've tried to set in every action "index" Url::remember(); and then in action "update" - return $this->goBack().
Also return $this->redirect(Yii::$app->request->referrer);, but it stays at same page.
There is a way to store every index URL in session, but in large project that means many sessions.
You could provide the returnUrl to the link, say:
Url::to(['update','id'=>$model->url,'returnUrl'=> Yii::$app->request->url]);
Then in your controller, use $this->request->queryParams['returnUrl'] to redirect to the previousUrl.
To take it one step further, to always provide the returnUrl, you could extend the Url Helper class:
namespace app\helpers;
use yii\helpers;
class Url extends yii\helpers\Url
public function toRouteAndReturn($route, array $params = [], $scheme = false) {
$params['returnUrl'] = Yii::$app->request->url;
return parent::toRoute($route,$params,$scheme);
}
You could provide in your main config:
'on afterAction' => function($event) {
if(!Yii::$app->getResponse()->isSent && !empty(Yii::$app->getRequest()->queryParams['returnUrl']) {
Yii::$app->getResponse()->redirect(Yii::$app->getRequest()->queryParams['returnUrl']);
}
}
Then you could use app\helpers\Url::toRouteAndReturn() instead of yii\helpers\Url::toRoute() to have it return to the previous url.
You can try below Solution.
First in your index page, get current page url and encode it.
$current_url=base64_encode(\Yii::$app->request->getUrl());
Append this url with your update link as below.
'urlCreator' => function ($action, $model, $key, $index) use ($current_url) {
if ($action === 'update') {
$url = Yii::$app->request->baseUrl . '/controllerName/update?id=' . $model->id.'&prev='.$current_url;
return $url;
}
// ......
}
In Controller, in Update method decode url as below and use for redirection.
public function actionUpdate($id)
{
$model = $this->findModel($id);
$prev=base64_decode($_REQUEST['prev']);
// ......
return $this->redirect($prev); // you will redirect from where update method is called
// ......
}
Isn't it quite easy to pass page param into your update url (<model/update>) like <model>/update?id=<id>&page=<page>?
in your index.php view, edit your ActionColumn as follow:
[
'class' => 'yii\grid\ActionColumn',
'urlCreator' => function ($action, $model, $key, $index) {
return \yii\helpers\Url::to([$action, 'id' => $model->id, 'page' => Yii::$app->request->getQueryParam('page', null)]);
},
],
As you can see, I'm getting page param from request url and pass it to models' action buttons
And when you click to update model, the page that we entered from is stored/placed in url.
Controller:
public function actionUpdate($id, $page = null)
{
$model = $this->findModel($id);
...
if($model->save()) {
return $this->redirect(['index', 'page' => $page]);
}
...
}
Finally, after we successfully update the model, the action redirects us to previous index page.

How to use Gravity Forms gform_validation to ensure at least one of email or phone are entered

I'm a designer rather than a developer.
I'm using Gravity Forms. I have a simple Gravity Form:
[name]
[phone]
[email]
[message]
I'd like to ensure at least one of [phone] or [email] have been entered, rather than requiring both to be filled in.
Gravity Forms support say I can use gform_validation but I don't know how to build the code to validate the form such that if both [phone] and [email] are empty a message is displayed: please enter either phone or email.
Help appreciated.
In my opinion, it might be easier to do it this way:
Verify that one of phone or email inputs is filled by submitting the input data to a script. It could be JS or a PHP script. This can be done easily by using logical operators to check if both are empty.
Then use https://www.gravityhelp.com/documentation/article/gform_validation/#2-send-entry-data-to-third-party
For a singular form, using ID's to require atleast 1 of 2 fields being filled
This works great on smaller sites who only have 1 Gravity Form with an email field and phone field. Easily customizable. Most of the code is explained with comments.
<?php
// 1 = ID of form
add_filter( 'gform_validation_1', 'custom_validation' );
function custom_validation( $validation_result ) {
$form = $validation_result['form'];
// Our desired input fields
$phone = rgpost( 'input_6' );
$email = rgpost( 'input_7' );
// Fields that must be empty
if ( empty( $phone ) && empty( $email )) {
// Looping through the fields
foreach( $form['fields'] as &$field ) {
// Finds the field with ID of 7
// This is the field where the validation message will appear, can add multiple inbetween with ||-operator
if ( $field->id == '7' /*|| $field->id == '6'*/ ) {
$field->failed_validation = true;
$field->validation_message = 'Please enter either an email address or phone number.';
$validation_result['is_valid'] = false;
}
}
}
// Assign modified $form object back to the validation result
$validation_result['form'] = $form;
return $validation_result;
}
?>
Dynamic script that will run on all forms with input type="email" & input type="tel"
This works great on larger sites with multiple Gravity Forms. The script below will affect all Gravity Forms. Code is explained with comments.
<?php
add_filter( 'gform_validation', 'custom_validation' );
function custom_validation( $validation_result ) {
$form = $validation_result['form'];
// Finds current page
$current_page = rgpost( 'gform_source_page_number_' . $form['id'] ) ? rgpost( 'gform_source_page_number_' . $form['id'] ) : 1;
// Initiated when $current_page is true
if ( $current_page ) {
// Loops through all fields
foreach( $form['fields'] as &$field ) {
// Input types
$field_phone = $field["type"] == 'phone';
$field_email = $field["type"] == 'email';
// Accessing field value with rgpost()
$field_value = rgpost("input_{$field['id']}");
if ( $field_phone ) {
// Assigning the field value of field type phone
$field_phone_type = $field_value;
}
if ( $field_email ) {
// Assigning the field value of field type email
$field_email_type = $field_value;
// Only runs if theres both a field type email AND field type phone
if (isset( $field_email_type ) && isset( $field_phone_type )) {
// If both the email and phone fields are empty
if ( empty( $field_phone_type ) && empty( $field_email_type )) {
// Validation message is applied to $field_email field only - can be modified to be both
$validation_result['is_valid'] = false;
$field->failed_validation = true;
$field->validation_message = 'Please enter either an email address or phone number.';
}
}
}
}
}
// Assign modified $form back to the validation result
$validation_result['form'] = $form;
return $validation_result;
}
?>

Wordpress, redirect a user directly to a custom post edit screen after login

currently I got this:
function redirect_companies()
{
if ( current_user_can( 'ca_company' ) )
{
$screen = get_current_screen();
if ( $screen->post_type != 'unternehmen' && $screen->id != 'profile' )
{
global $current_user;
$current_users_posts = get_posts(
array(
'post_type' => 'unternehmen',
'author' => $current_user->ID
)
);
if ( count( $current_users_posts ) > 1 )
{
$redirect = admin_url( 'edit.php?post_type=unternehmen' );
}
else
{
$redirect = get_edit_post_link( $current_users_posts[0]->ID );
}
wp_redirect( $redirect, 301 );
}
}
}
add_action('current_screen', 'redirect_companies');
What it should do: A user with role 'ca_company' logs into wordpress backend and instantly gets redirected to either the overview screen of the custom post type posts of "unternehmen" or, if only one post by this user exists, to the edit screen of that one post.
Also, it should perform this redirect routine, if the user is trying to access any page that is not from post type "unternehmen" and is not the user-profile-edit screen.
I successfully tested this when I already was logged in as auch user and then trying to access for example the dashboard. This works.
But if I completely log out of WP and then log in again, wordpress is performing this:
http://i.stack.imgur.com/M60aJ.png
... and then my browser is telling me, that there is a redirecting error. Infinite redirecting loop. But why? Why does it even go into that "if" where I check for post type "unternehmen". Because if I log in, I am first getting to dashboard...
Hope someone can help :)
Use this action 'add_action('wp_login', 'do_anything');'. And in callback function you can give link to wp_redirect('link') where you want to redirect your screen.

Facebook Graph API - Get Event that a Page have created

How can I get all the Events a Page have created?
I've tried the following:
https://graph.facebook.com/PAGEID/events
But I don't get any data back.
Can someone help me?
I've run in to the same issue and also updated the bug mentioned by ivan.abragimovich.
I'm not proud of it, but here is what I did as a work around.
$accessToken = "..."; // your OAuth token
$uid = "..."; // the id of the page you are using
$feed = $this->facebook->api("/$uid/feed", "GET", array('access_token' => $accessToken,
'limit' => 20));
// temp method of retrieving events until the page events bug is fixed.
// #see http://bugs.developers.facebook.com/show_bug.cgi?id=10399
if (array_key_exists('data', $feed) && is_array($feed['data']))
{
foreach($feed['data'] as $item)
{
if ($item['type'] == "link" && strpos($item['link'], "eid=") !== false)
{
preg_match('/eid=(\d+)/', $item['link'], $urlMatches);
if (count($urlMatches) == 2)
{
$eventId = $urlMatches[1];
$event = $this->facebook->api("/$eventId", 'GET', array('access_token' => $accessToken));
print_r($event);
}
}
}
}
Are you sure there are events that are associated with the Page? You might want to check if you can retrieve the feed. Just swap "events" with "feed" in the URL.