Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if ( ! defined( 'ABSPATH' ) ) { exit; } elseif ( defined( 'WP_CLI' ) && WP_CLI ) { return; }
- add_action( 'plugins_loaded', 'wpmudev_forminator_add_custom_number_to_redirect_url_func', 100 );
- function wpmudev_forminator_add_custom_number_to_redirect_url_func() {
- if ( class_exists( 'Forminator' ) ) {
- class WPMUDEV_Forminator_Add_Entry_ID_To_Redirect_URL{
- //enter form_id here
- private $form_id = 1398;
- // fields to be calculated
- private $fields_to_add = array(
- 'text-1',
- 'text-2',
- );
- //enter your redirect urls here
- // later in code they are refered as $redirect_urls[X]
- // where X is number from 0 up
- private $redirect_urls = array(
- 'http://somesite.com/redirect1/',
- 'http://somesite.com/redirect2/',
- 'http://somesite.com/redirect3/',
- );
- // don't change these
- private $total_length = '';
- private $do_redirect = '';
- public function __construct(){
- add_action( 'forminator_custom_form_submit_before_set_fields', array( $this, 'calculate_length' ), 10, 3 );
- add_filter( 'forminator_replace_form_data', array( $this, 'add_length_to_redirect_url'), 20, 1 );
- }
- public function calculate_length( $entry, $form_id, $form_data_array ){
- if( $this->form_id == $form_id ){
- foreach ( $form_data_array as $form_data_element ) {
- if ( in_array( $form_data_element['name'], $this->fields_to_add ) ) {
- $this->total_length = $this->total_length + strlen( $form_data_element['value'] );
- }
- }
- }
- }
- public function add_length_to_redirect_url( $content ){
- if ( $this->total_length !== '' ) {
- // below you set conditions
- // which URL (see private $do_redirect earlier in code) to redirect to
- if ( $this->total_length < 5 ) {
- $this->do_redirect = $this->redirect_urls[0];
- } elseif ( $this->total_length < 8 ) {
- $this->do_redirect = $this->redirect_urls[1];
- } else {
- $this->do_redirect = $this->redirect_urls[2];
- }
- $content = add_query_arg('total_length', $this->total_length, $this->do_redirect);
- }
- return $content;
- }
- }
- $run = new WPMUDEV_Forminator_Add_Entry_ID_To_Redirect_URL;
- }
- }
Add Comment
Please, Sign In to add comment