0

Below is the code I have been working on to run a shortcode. I have tried implimenting an if() statement, however nothing has worked.

I have two outputs. What I am looking to do, is if the attributes

('img', 'user', 'text')

are present in the shortcode, display $output1.

Otherwise, if the attributes

('img', 'text', 'length', 'material', 'power')

are present in the shortcode, display $output2.

function hire_equipment_func($atts) {

    extract( shortcode_atts( array(
        'img' => '',
        'user' => '',
        'text' => '',
        'length' => '',
        'material' => '',
        'power' => '',
    ), $atts ) );

    $atts = array_change_key_case((array)$atts, CASE_LOWER);

    return $output1;

    $output1 =

    '<div class="hire-equipment-item">

        <div class="hire-equipment-item-img"><img src="' . $atts['img'] . '" height=200px" width="200px" alt=""></div><br />

        <div class="hire-equipment-item-user">' . $atts['user'] . '</div>

        <div class="hire-equipment-item-text">' . $atts['text'] . '</div>

    </div>';

    $output2 =

    '<div class="hire-equipment-item">

        <div class="hire-equipment-item-img"><img src="' . $atts['img'] . '" height=200px" width="200px" alt=""></div><br />

        <div class="hire-equipment-item-user">' . $atts['user'] . '</div>

        <div class="hire-equipment-more-information">

            <table class="hire-equipment-more-information-table" cellpadding="15px">

                <tr>

                    <th>Length:</th>
                    <th>Material:</th>
                    <th>Power:</th>

                </tr>

                <tr>

                    <td> ' . $atts['length'] . ' </td>
                    <td> ' . $atts['material'] . ' </td>
                    <td> ' . $atts['power'] . ' </td>

                </tr>

            </table>

        </div>

    </div>';

}
add_shortcode( 'hire_equipment', 'hire_equipment_func' );

Any help with this would be greatly appreciated!

1

I am not really sure I understand the question. You would like to display different markup depending on which fields are set in the $atts array? If this is the case, then this should work:

// Check so fields are set in the `$atts` array.
if (isset($atts['img'], $atts['user'], $atts['text'])) {
  return '<div class="hire-equipment-item">
            <div class="hire-equipment-item-img">
              <img src="' . esc_url($atts['img']) . '" height=200px" width="200px" alt="">
            </div><br />
            <div class="hire-equipment-item-user">' . $atts['user'] . '</div>
            <div class="hire-equipment-item-text">' . sanitize_text_field($atts['text']) . '</div>
         </div>';
} 
elseif (isset($atts['img'], $atts['text'], $atts['length'], $atts['material'], $atts['power'])) {
  // Second output goes here.
}

Notice that user may not be set in the second case else the first condition will always be true.

Since the markup for both conditions are very similar a better approach might be:

function hire_equipment_func($atts) {
  // Store merged result. 
  $atts = shortcode_atts(array(
    'img' => '',
    'user' => '',
    'text' => '',
    'length' => '',
    'material' => '',
    'power' => '',
  ), array_change_key_case($atts, CASE_LOWER));


  $output = '<div class="hire-equipment-item>';

  // Check if image should be appended to output.
  if (isset($atts['img'])) {
    $output .= '<div class="hire-equipment-item-img">
                  <img src="' . esc_url($attribs['img']) . '" height="200" width="200" alt="" />
                </div>';
  }
  // Check if user should be appended to output.
  if (isset($atts['user'])) {
    $output .= '<div class="hire-equipment-item-user">' . $atts['user'] . '</div>';
  }
  // Check if text should be appended to output.
  if (isset($atts['text'])) {
    $output .= '<div class="hire-equipment-item-text">' . $atts['text'] . '</div>';
  }
  // Check if we need to output more-information table.
  if (isset($atts['length']) || isset($atts['material']) || isset($atts['power'])) {
    $output .= '<div class="hire-equipment-more-information">
                 <table class="hire-equipment-more-information-table" cellpadding="15px">
                   <tr>
                     <th>Length:</th>
                     <th>Material:</th>
                     <th>Power:</th>
                   </tr>
                   <tr>
                     <td> ' . (isset($atts['length']) ? $atts['length'] : '') . ' </td>
                     <td> ' . (isset($atts['material']) ? $atts['material'] : '') . ' </td>
                     <td> ' . (isset($atts['power']) ? $atts['power'] : '') . ' </td>
                   </tr>
                 </table>
               </div>';
  }
  $output .= '</div>';

  return $output;
}

You might also need to use some sanitazion for your output. For instance use esc_url() for urls and sanitize_text_field() for strings and so on.

5
  • I am attempting the code that you have provided. I will update you in a few minutes if it works! :D – Anake.me Jul 24 '17 at 9:36
  • 1
    I ran into a few errors in my code, but once I corrected all the errors, it worked! Thank you!! – Anake.me Jul 24 '17 at 9:48
  • @Jesse - Glad I could help, I think you should have a look on my second solution, since I think this should be more dynamic. – Cyclonecode Jul 24 '17 at 9:49
  • This is what I ended up with :) I may rework the code later to what you have specified above if need be, but at the moment, this issue was the last I had to my website. jsfiddle.net/brzLhq4b/1 – Anake.me Jul 24 '17 at 9:51
  • @Jesse - I added an updated and hopefully working second solution =) – Cyclonecode Jul 24 '17 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.