adczk

Forminator - redirect depending on fields string lenghts

May 3rd, 2022 (edited)
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.30 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) { exit; } elseif ( defined( 'WP_CLI' ) && WP_CLI ) { return; }
  4.  
  5. add_action( 'plugins_loaded', 'wpmudev_forminator_add_custom_number_to_redirect_url_func', 100 );
  6.  
  7. function wpmudev_forminator_add_custom_number_to_redirect_url_func() {
  8.     if ( class_exists( 'Forminator' ) ) {
  9.         class WPMUDEV_Forminator_Add_Entry_ID_To_Redirect_URL{
  10.            
  11.             //enter form_id here
  12.             private $form_id = 1398;
  13.                        
  14.             // fields to be calculated
  15.             private $fields_to_add = array(
  16.                 'text-1',
  17.                 'text-2',
  18.             );
  19.            
  20.                         //enter your redirect urls here
  21.             // later in code they are refered as $redirect_urls[X]
  22.             // where X is number from 0 up
  23.             private $redirect_urls = array(
  24.                 'http://somesite.com/redirect1/',
  25.                 'http://somesite.com/redirect2/',
  26.                 'http://somesite.com/redirect3/',
  27.             );             
  28.                
  29.             // don't change these
  30.             private $total_length = '';
  31.             private $do_redirect = '';
  32.  
  33.             public function __construct(){
  34.                 add_action( 'forminator_custom_form_submit_before_set_fields', array( $this, 'calculate_length' ), 10, 3 );
  35.                 add_filter( 'forminator_replace_form_data', array( $this, 'add_length_to_redirect_url'), 20, 1 );
  36.             }
  37.  
  38.  
  39.             public function calculate_length( $entry, $form_id, $form_data_array ){
  40.                
  41.                 if( $this->form_id == $form_id ){
  42.                    
  43.                     foreach ( $form_data_array as $form_data_element ) {
  44.                        
  45.                         if ( in_array( $form_data_element['name'], $this->fields_to_add ) ) {
  46.                            
  47.                             $this->total_length = $this->total_length + strlen( $form_data_element['value'] );
  48.                                                                
  49.                         }
  50.                     }
  51.                                            
  52.                 }
  53.  
  54.             }
  55.  
  56.             public function add_length_to_redirect_url( $content ){
  57.                                
  58.                 if ( $this->total_length !== '' ) {
  59.                     // below you set conditions
  60.                     // which URL (see private $do_redirect earlier in code) to redirect to
  61.                     if ( $this->total_length < 5 ) {
  62.                         $this->do_redirect = $this->redirect_urls[0];
  63.                     } elseif ( $this->total_length < 8 ) {
  64.                         $this->do_redirect = $this->redirect_urls[1];
  65.                     } else {
  66.                         $this->do_redirect = $this->redirect_urls[2];
  67.                     }
  68.                
  69.                     $content = add_query_arg('total_length', $this->total_length, $this->do_redirect);
  70.                
  71.                 }
  72.                
  73.                 return $content;
  74.             }
  75.  
  76.         }
  77.  
  78.         $run = new WPMUDEV_Forminator_Add_Entry_ID_To_Redirect_URL;
  79.     }
  80. }
Add Comment
Please, Sign In to add comment