Home Forums Add-ons Repeater Field Show field x if field y contains…


Show field x if field y contains…

  • I’m using ACF to manage a booking availability form.
    At present, all links go to webA, so the form is coded to suit webA’s input.

    I have some clients who need bookings through webB so need an alternate format on their pages.

    I’m trying to write something along the lines of:

    if $bookingfield contains ‘’
    echo “”
    else if $bookingfield conatins ‘’
    don’t show the form

    I’m happy with the if / else logic, but I’m not sure where to start with specifying that $bookingfiled contains “x” or “y” etc.

    Would be veyr grateful for any advice



  • What type of field is the $bookingfield and what is the name of the field?

  • Hi John
    $bookingfield is a url.
    field name is accom_booking_link

    So depending which website the $bookingfield ponts to, I want to present a different url structure.
    i.e. if show
    if show

    and so on.

    Does that make more sense?

    $bookingfield = get_field('accom_booking_link');
    // full url as returned by acf will be needed in if statements
    // I would use a switch because it's a url field the user could enter anything
    switch ($bookingfield) {
      case '':
        // do something
      case '':
        // do something else
        // not one of the sites
        echo 'Invalid Booking Site';

    I would not use a URL field here, the main reason being that you are only providing for 2 choices but the user can select from an unlimited number of urls.

    I would us a select field, or a radio field so they can only select from the ones I’m prepared to support. In this case the logic would be the same because you can always add more choices later. You would just change out the cases in the switch statement base on the values of the select field.

  • Hi John
    I’ve explained my problem badly I think.

    The user can only choose a checkin and a checkout date and press ‘submit’.

    PropertyA uses BookingSiteA
    PropertyB uses BookingSiteB
    both have different syntax for selecting dates

    The user selects dates then the dates are input into a link ($bookingfield)
    on SUBMIT they are sent to that URL.

    So if the use is on PropertyA the syntax is

    If the user is on PropertyB the syntax is

    PropertyA and PropertyB booking links are managed from the backend – the user can’t touch them – they’re set.

    So what I’m trying to do is find a flexible way of checking what is contained in the booking link field and depending on the result, compiling the URL in a different way.

    I’ve only given 2 examples of link syntax for simplicity, i’d like to be able to cover 7 or 8 different possibilities in an IF ELSEIF ELSE statement.

    This is the current code, that ONLY goes to

    $linkout = get_field('accom_booking_link');
    $datein = (isset($_GET['checkin']) ? $_GET['checkin'] : null);
    $dateout = (isset($_GET['checkout']) ? $_GET['checkout'] : null);
    <?php if (strpos($linkout, 'booking') !== false){ ?>
    <form id="datepick" method="GET" action="<?php echo $linkout . ';checkin=' . $datein . ';checkout=' . $dateout . ';' ?>">
        <li><label>Check In </label><input type="text" id="checkin" name="checkin" value=""/></li>
        <li><label>Check Out </label><input type="text" id="checkout" name="checkout" value="" /></li>
        <li class="submit"><input type="submit" onClick="ga('send', 'event', 'BookingCom', 'availabiliity', '<?php echo $name; ?>' );"></li>
    <?php }
  • You should look at the first code example that I gave then with the switch case based on the url field. A switch/case statement is the same as and if/elseif statement. The main difference is that it’s a lot cleaner.

    Think of it like this

    $bookingfield = get_field('accom_booking_link');
    if ($bookingfield == 'first url') {
    } elseif ($bookingfield == 'second url') {
    } elseif ($bookingfield == 'third case') {
    } elseif ($bookingfield == 'fourth case') {
    } else {
      // default case

    This gets quite cumbersome after several elseif’s and is horrible to look at an manage, but can be done this way if you want. When dealing with many possibilities a switch statement can drastically reduce the amount of typing you need to do to code it.

  • Thanks John
    I have to admit, I’ve never used Switch statements so I’ll have to take a proper look at them.

    The code you’ve provided looks like it’ll do the trick.

    My last issue is that each url has a unique property identifier on it.


    I’ll have to try it out and see if I can use wildcards / regex within ACF.

    Thanks again for your help

  • Just to update.

    of course it was simple in the end…

    if  (strpos($linkout, '') !== false){ ?>
    //do something
    } elseif (strpos($linkout, '') !== false){ ?>
    //do something else

    Thanks for sending me in the right direction John


Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Show field x if field y contains…’ is closed to new replies.