How can I create a shortcode from an html and php code written together

by Sadiq11111   Last Updated May 30, 2019 15:08 PM

How can I create a shortcode from an html and php code like below:

function hire_feedback_surveyyy() {
$product_id = 21876;

// We get all the Orders for the given product ID in an arrray
$orders_ids_array = retrieve_orders_ids_from_a_product_id( $product_id );

$output = '';
$output .= '<select>';
foreach($orders_ids_array as $value) {
      $order = wc_get_order( $value );
      $sp_name = $order->get_billing_company();
      $order_id = $order->get_id();

$output .=  '<option value="'.$order_id.'">'.$sp_name.'</option>';
}
$output .= '</select>';

return $output;
}
add_shortcode( 'show_hire_analytics', 'hire_feedback_surveyyy');

I also tried this

function hire_feedback_surveyyy() {
    ob_start();
    $product_id = 21876;

// We get all the Orders for the given product ID in an arrray
$orders_ids_array = retrieve_orders_ids_from_a_product_id( $product_id );

?>
<select>
  <?php foreach($orders_ids_array as $value) {
      $order = wc_get_order( $value );
      $sp_name = $order->get_billing_company();
      $order_id = $order->get_id();
  ?>
    <option value="<?php echo $order_id ?>"><?php echo $sp_name ?></option>
  <?php }?>
</select>

<?php
        $content = ob_get_contents();
    ob_end_clean();
    return $content;
}
add_shortcode( 'show_hire_analytics', 'hire_feedback_surveyyy');

I have tried different things but nothing is working. It seems the foreach statement in the code is preventing the shortcode from working. Any help will be greatly appreciated.

Tags : php shortcode html


Related Questions



WordPress Shortcode and Dynamic CSS

Updated May 16, 2016 08:03 AM


How do I use Shortcodes inside of HTML tags?

Updated March 18, 2017 22:08 PM