Skip Navigation

[Resolved] Need to create a shortcode function, with “echo” content out

This thread is resolved. Here is a description of the problem and solution.

Problem:
Need to create a shortcode function, with "echo" content out

Solution:
You should add the custom shortcode to your current theme's functions.php file or Custom Code section offered by Toolset.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/need-to-create-a-shortcode-function-with-echo-content-out/#post-1344827

Relevant Documentation:
=> https://toolset.com/documentation/adding-custom-code/how-to-create-a-custom-shortcode/
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

This support ticket is created 5 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 14 replies, has 2 voices.

Last updated by fred-r.M 5 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1344765

Tell us what you are trying to do?

On all those tourist spots, eg hidden link we have under the image/video a grey part with information. Most of those information are filled out per tourist spot, but some are not used either, depend of the tourist spot.

What I need from You is a short code function, where I "echo" out this:

<ul class="contact">
 	<li><strong><i class="fas fa-taxi"></i> Distance From Bismarcks Paradise:</strong> [types field='distance-from-sakoo' format='FIELD_VALUE'][/types]</li>
 	<li><strong><i class="fas fa-taxi"></i> Distance From Central Phuket:</strong> [types field='distance-from-phuket-town' format='FIELD_VALUE'][/types]</li>
 	<li><strong><i class="far fa-star"></i> Bismarck Rating:</strong> [types field="bismarck-rating" target="_blank"][/types]</li>
 	<li><strong><i class="far fa-money-bill-alt"></i> Price Range</strong> [types field="price-from" format="FIELD_VALUE"][/types] - [types field="price-to" format="FIELD_VALUE"][/types] THB</li>
 	<li><strong><i class="fas fa-users"></i> Number of Tables</strong> [types field="number-of-tables" target="_blank"][/types]</li>
 	<li><strong><i class="far fa-clock"></i> Opening Hours</strong>[types field="opening-hours" separator=", "][/types]</li>
 	<li><strong><i class="fas fa-location-arrow"></i> Address</strong> [types field="address-text" target="_blank"][/types]</li>
 	<li><strong><i class="fas fa-phone"></i> Telephone</strong> [types field="telephone" target="_blank"][/types]</li>
 	<li><strong><i class="fas fa-external-link-alt"></i> Website</strong> [types field="website" target="_blank"][/types]</li>
 	<li><strong><i class="fab fa-facebook-square"></i> Facebook</strong> [types field="facebook-url" target="_blank"][/types]</li>
 	<li><strong><i class="fas fa-envelope"></i> Contact Email</strong> [types field="contact-email" target="_blank"][/types]</li>
 	<li><strong><i class="fas fa-utensil-spoon"></i> Bismarck's Review of [wpv-post-title]</strong>
<strong>Food </strong>[wpv-conditional if="( $(wpcf-bismarck-rating-food) eq 'Excellent' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/five-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-food) eq 'Good' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/four-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-food) eq 'Average' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/three-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-food) eq 'Ok' )"]<img class="bis_star" src="/wp-content/uploads/2019/09/two_star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-food) eq 'Not Good' )"]<img class="bis_star" src="/wp-content/uploads/2019/09/one_star1.png" alt="Star Rating" />[/wpv-conditional] <strong>Service </strong>[wpv-conditional if="( $(wpcf-bismarck-rating-service) eq 'Excellent' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/five-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-service) eq 'Good' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/four-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-service) eq 'Average' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/three-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-service) eq 'Ok' )"]<img class="bis_star" src="/wp-content/uploads/2019/09/two_star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-service) eq 'Not Good' )"]<img class="bis_star" src="/wp-content/uploads/2019/09/one_star1.png" alt="Star Rating" />[/wpv-conditional] <strong>Ambiance </strong>[wpv-conditional if="( $(wpcf-bismarck-rating-ambiance) eq 'Excellent' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/five-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-ambiance) eq 'Good' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/four-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-ambiance) eq 'Average' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/three-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-ambiance) eq 'Ok' )"]<img class="bis_star" src="/wp-content/uploads/2019/09/two_star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-ambiance) eq 'Not Good' )"]<img class="bis_star" src="/wp-content/uploads/2019/09/one_star1.png" alt="Star Rating" />[/wpv-conditional]</li>
 	<li><strong><i class="fas fa-pencil-alt"></i> Comments</strong> [types field="bismarck-review" target="_blank"][/types]</li>
 	<li><strong><i class="fab fa-tripadvisor"></i> <a href="[types field='tripadvisor-url' format='FIELD_VALUE'][/types]" target="_blank" rel="nofollow noopener noreferrer">TripAdvisor Reviews from [wpv-post-title]</a></strong>
<ul class="list-inline post-detail text-left">
 	<li class="post-detail"><i class="fas fa-tag"></i> TAGS:    [wpv-post-taxonomy type='information' separator='   |   ']</li>
</ul>
</li>
</ul>

The function I like to have a custom function in Toolse Settings, like this:

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.

function func_display_information() {
    /* here used the whole part above */
}
add_shortcode( 'show_tourist_info', 'func_display_information' );

The main thing is, for each

  • , for example:
    <li><strong><i class="fas fa-location-arrow"></i> Address</strong> [types field="address-text" target="_blank"][/types]</li>

    if there is no address-text in, then it should not "echo" out. I know that I use to need

    [wpv-conditional if="( $(wpcf-address-text) ne '' )"]<li><strong><i class="fas fa-location-arrow"></i> Address</strong> [types field="address-text" target="_blank"][/types]</li>[/wpv-conditional]

    But this is somehow not working in the Theme we are using. This is why I like to create a function doing it. But my php for it is not so strong, and as You did recently some small work, You might be able to help, either if only for 3 lines and showing how to do.

    Regards

  • #1344825
    #1344827

    Minesh
    Supporter

    Languages: English (English )

    Timezone: Asia/Kolkata (GMT+05:30)

    So, I've adjusted the shortcode as given under:

    function func_display_information($atts) {
      global $post;
      $str = '';
      
      $field_value = get_post_meta($post->ID,'wpcf-'.$atts['field'],true);
       
      if($atts['field']=='address-text' and !empty($field_value)){
          $str .= '<li><strong><i class="fas fa-location-arrow"></i> Address</strong>'.$field_value.'</li>';
        
      }else if($atts['field']=='telephone' and !empty($field_value)){
        $str .= '<li><strong><i class="fas fa-phone"></i> Telephone</strong>'.$field_value.'</li>';
      }
      
      return $str;
       
    }
    add_shortcode( 'show_tourist_info', 'func_display_information' );
    

    Where:
    - You should add as many if else conditiontions as given above for your every field and display each field and call it as given under:

    [show_tourist_info field="address-text"]
    [show_tourist_info field="telephone"]
    

    You should just keep the UL tag and all li tags will be come from the shortcode. I hope its clear 🙂

    #1344939

    Dear Minesh

    Great thank You - I will try later, but looking understandable. Only 2 things:

    1. This line needs somehow ab it special coding:

    <strong>Food </strong>[wpv-conditional if="( $(wpcf-bismarck-rating-food) eq 'Excellent' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/five-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-food) eq 'Good' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/four-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-food) eq 'Average' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/three-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-food) eq 'Ok' )"]<img class="bis_star" src="/wp-content/uploads/2019/09/two_star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-food) eq 'Not Good' )"]<img class="bis_star" src="/wp-content/uploads/2019/09/one_star1.png" alt="Star Rating" />[/wpv-conditional] <strong>Service </strong>[wpv-conditional if="( $(wpcf-bismarck-rating-service) eq 'Excellent' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/five-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-service) eq 'Good' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/four-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-service) eq 'Average' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/three-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-service) eq 'Ok' )"]<img class="bis_star" src="/wp-content/uploads/2019/09/two_star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-service) eq 'Not Good' )"]<img class="bis_star" src="/wp-content/uploads/2019/09/one_star1.png" alt="Star Rating" />[/wpv-conditional] <strong>Ambiance </strong>[wpv-conditional if="( $(wpcf-bismarck-rating-ambiance) eq 'Excellent' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/five-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-ambiance) eq 'Good' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/four-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-ambiance) eq 'Average' )" output="raw"]<img class="bis_star" src="/wp-content/uploads/2019/09/three-star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-ambiance) eq 'Ok' )"]<img class="bis_star" src="/wp-content/uploads/2019/09/two_star1.png" alt="Star Rating" />[/wpv-conditional][wpv-conditional if="( $(wpcf-bismarck-rating-ambiance) eq 'Not Good' )"]<img class="bis_star" src="/wp-content/uploads/2019/09/one_star1.png" alt="Star Rating" />[/wpv-conditional]</li>

    2. If I like to show the post title somewhere in a function like this, where I used to have

    [wpv-post-title]

    - how I do this?

    Regards

    #1344999

    Minesh
    Supporter

    Languages: English (English )

    Timezone: Asia/Kolkata (GMT+05:30)

    1. This line needs somehow ab it special coding:
    => What you mean - I see there is already conditional statements added - do you mean that those conditionals also converted to PHP code?

    2. If I like to show the post title somewhere in a function like this, where I used to have
    => As this is PHP code and we do added the global $post; - So you can display the post title using $post->post_title.

    #1345019

    For point 1, yes... I like to have it also coded basically as last

  • or inbetween.

    For point 2, I assume like this will be okay?

    $str .= '<li><strong><i class="fab fa-tripadvisor"></i> <a href="'.$field_value.'" target="_blank" rel="nofollow noopener noreferrer">TripAdvisor Reviews from '.$post->post_title.'</a></strong></li>';

    And additional question, for those shortcodes - I will add them then like this

    <ul>
    [show_tourist_info field="facebook-url"]
    </ul>

    this will be then:

    <ul>
    <li>Whatever it needs to show</li>
    </ul>

    correct?

  • #1345239

    Dear Minesh

    Please check the actual status how It would be: hidden link

    This looking great. Now I only need to add those star-ratings... can also get done with a separate new function, if this is better.

    Regards

    #1345243

    My issue is resolved now. Thank you!

    I used now this code:

    else if($atts['field']=='tripadvisor-url' and !empty($field_value)){
        $str .= '<li><strong><i class="fab fa-tripadvisor"></i> <a href="'.$field_value.'" target="_blank" rel="nofollow noopener noreferrer">TripAdvisor Reviews from '.$post->post_title.'</a></strong></li>';
      }
      else if($atts['field']=='bismarck-rating-food' and !empty($field_value)){
        /*$str .= '<li><strong><i class="fas fa-pencil-alt"></i> Food:</strong>'.$field_value.'</li>';*/
        if($field_value == "Excellent"){ 
        $str .= '<strong>Food: </strong><img class="bis_star" src="/wp-content/uploads/2019/09/five-star1.png" alt="5 Star Rating" />';
        } elseif($field_value == "Good"){
          $str .= '<strong>Food: </strong><img class="bis_star" src="/wp-content/uploads/2019/09/four-star1.png" alt="4 Star Rating" />';
        } elseif($field_value == "Average"){
          $str .= '<strong>Food: </strong><img class="bis_star" src="/wp-content/uploads/2019/09/three-star1.png" alt="3 Star Rating" />';
        } elseif($field_value == "Ok"){
          $str .= '<strong>Food: </strong><img class="bis_star" src="/wp-content/uploads/2019/09/two_star1.png" alt="2 Star Rating" />';
        } elseif($field_value == "Not Good"){
          $str .= '<strong>Food: </strong><img class="bis_star" src="/wp-content/uploads/2019/09/one_star1.png" alt="1 Star Rating" />';
        }
      }

    There might be an easier way - but it is working as expected.

    #1345297

    Dear Minesh

    I found some more things... as in the original code are some variables needs to get set:

    [wpv-conditional if="( $(wpcf-distance-from-sakoo) ne '' )"]
    <li><strong><i class="fa fa-taxi"></i> Distance (Bismarcks Paradise)</strong>[types field="distance-from-sakoo" format="FIELD_VALUE"][/types] km [wpv-conditional if="( $(wpcf-time-from-sakoo) ne '' )"]([types field="time-from-sakoo" format="FIELD_VALUE"][/types] minutes)[/wpv-conditional]</li>[/wpv-conditional]
    [wpv-conditional if="( $(wpcf-distance-from-phuket-town) ne '' )"]<li><strong><i class="fa fa-taxi"></i> Distance (Central Phuket)</strong>[types field="distance-from-phuket-town" format="FIELD_VALUE"][/types] km [wpv-conditional if="( $(wpcf-time-from-phuket-town) ne '' )"]([types field="time-from-phuket-town" format="FIELD_VALUE"][/types] minutes)[/wpv-conditional]</li>[/wpv-conditional]
    [wpv-conditional if="( $(wpcf-price-from) ne '' ) AND ( $(wpcf-price-to) ne '' )"]<li><strong><i class="fa fa-money"></i> Price Range</strong> [types field="price-from" format="FIELD_VALUE"][/types] - [types field="price-to" format="FIELD_VALUE"][/types] THB</li>[/wpv-conditional]

    There are additional variables, where need to change into php code. I tried doing somehting like this, but not sure if this is working and what variable I need to use:

    if($atts['field']=='distance-from-sakoo' and !empty($field_value)){
          $str .= '<li><strong><i class="fas fa-taxi"></i> Distance From Bismarcks Paradise:</strong>'.$field_value.' km 'if (!empty $(wpcf-time-from-sakoo)) </li>';
      }

    If You can help me again.

    Regards

    #1345753

    Minesh
    Supporter

    Languages: English (English )

    Timezone: Asia/Kolkata (GMT+05:30)

    In such case you need to manage the field as given under:

    Please check the following example for distance-from-sakoo and time-from-sakoo fields:

    
    if($atts['field']=='distance-from-sakoo' and !empty($field_value)){
    
        $str .= '<li><strong><i class="fas fa-taxi"></i> Distance From Bismarcks Paradise:</strong>'.$field_value.' km ';
         
         $value_sakoo = get_post_meta($post->ID,'wpcf-time-from-sakoo',true);
          if(!empty($value_sakoo )){
             $str .=  ' (.'$value_sakoo '.) minutes';
            }
          $str .= '</li>';
      }
    
    #1345991

    Dear Minesh

    It gives me an error:

    syntax error, unexpected '$value_sakoo' (T_VARIABLE) in /mnt/disk1/home/luxuryvillasphuk/public_html/new/wp-content/toolset-customizations/show-tourist-information.php on line 19

    What could this be?

    #1346007

    Minesh
    Supporter

    Languages: English (English )

    Timezone: Asia/Kolkata (GMT+05:30)

    Upps. I added dots to join the string at wrong place. Could you please check now. It should work. You should use the following code:

    if($atts['field']=='distance-from-sakoo' and !empty($field_value)){
     
        $str .= '<li><strong><i class="fas fa-taxi"></i> Distance From Bismarcks Paradise:</strong>'.$field_value.' km ';
          
         $value_sakoo = get_post_meta($post->ID,'wpcf-time-from-sakoo',true);
          if(!empty($value_sakoo )){
             $str .=  ' ('.$value_sakoo.') minutes';
            }
          $str .= '</li>';
      }
    
    #1346027

    Dear Minesh

    This worked now. I assume, I can do the same or similar way for the second "distance/minutes" and the simialr way for the price from / to.

    Please check this out: hidden link

    On the last point, just after the Tripadvisor - when there is no food rating, it will show me the "|", even if I have no food rating.

    I used the short codes like this:

    [show_tourist_info field="bismarck-rating-food"]  |  [show_tourist_info field="bismarck-rating-service"]  |  [show_tourist_info field="bismarck-rating-ambiance"]

    Any hint or idea how I can do better? Maybe add a new short code, where it check if it needs those "|" - only when there is something filled in those 3 fields...

    Regards,
    Simon

    #1346033

    Minesh
    Supporter

    Languages: English (English )

    Timezone: Asia/Kolkata (GMT+05:30)

    This worked now. I assume, I can do the same or similar way for the second "distance/minutes" and the simialr way for the price from / to.
    => Yes, thats correct.

    You should add those "|" with $str value from your shortcode where required.
    For example;

       $str .= " | ";
    

    And remove the "|" from where you are calling the shortcode.

    #1347191

    Minesh
    Supporter

    Languages: English (English )

    Timezone: Asia/Kolkata (GMT+05:30)

    I hope everything is sorted now. If yes - Please mark resolve this ticket 🙂