• Resolved Venci

    (@fabrixweb)


    Hello,

    In the function editor I have this custom function:

    function revision($rev_laptops) {
    $rev_laptops = array("rev.1", "rev.2", "rev.3", "");
    $rand_keys = array_rand($rev_laptops, 2);
    $newrev = $rev_laptops[$rand_keys[0]] . ""; //  random rev.1, rev.2 or rev.3
    echo $newrev;
    }

    I want to get this $newrev and pass it to the Text editor but in IF/Contains statemet. Is this possible?

    Somethig like this:

    [IF({newrev[contains(.,'rev.1')]})] Show Text 1 with some field from the csv file {name[1]} [ENDIF]
    [IF({newrev[contains(.,'rev.2')]})] Show Text 2 with some field from the csv file {name[1]} [ENDIF]
    [IF({newrev[contains(.,'rev.3')]})] Show Text 3 with some field from the csv file {name[1]} [ENDIF]

    Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author WP All Import

    (@wpallimport)

    Hey @fabrixweb,

    Unfortunately, you couldn’t pass a PHP function to an XPath IF statement like that. Instead, you should just handle all of it from within the function itself, e.g.:

    function revision( $name ) {
      $rev_laptops = array( "rev.1", "rev.2", "rev.3", "" );
      $rand_keys = array_rand( $rev_laptops, 2 );
      $newrev = $rev_laptops[ $rand_keys[0] ] . ""; //  random rev.1, rev.2 or rev.3
      $str_to_return = '';
      
      switch ( $newrev ) {
        case "rev.1":
          $str_to_return = "Show text 1 with some field from the CSV file {$name}";
          break;
    
        case "rev.2":
          $str_to_return = "Show text 2 with some field from the CSV file {$name}";
          break;
    
        case "rev.3":
          $str_to_return = "Show text 3 with some field from the CSV file {$name}";
          break;
    
        default:
          $str_to_return = "";
          break;
      }
    
      return $str_to_return;
    }

    With this, you’d pass the field from the CSV file to the function:

    [revision({name[1]})]

    Thread Starter Venci

    (@fabrixweb)

    You are awesome, thank you. I will try it as soon as possible.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘IF Contanis for custom function’ is closed to new replies.