Viewing 2 reply threads
  • Author
    Posts
    • #7724
      morganrt
      Member

      Hello,
      I’m using Piklist to create a shortcode that will take values from custom fields and turn it into an HTML tag. I haven’t done much with shortcodes until now.

      The code examples I’ve seen from Piklist use echo, but the WP codex states:

      Remember to use return and not echo – anything that is echoed will be output to the browser, but it won’t appear in the correct place on the page.

      on

      https://codex.wordpress.org/Shortcode_API
      

      Is the use of echo a Piklist-only thing or something to be avoided?

    • #7733
      Steve
      Keymaster

      @morganrt– Using echo with Piklist is fine. The framework takes care of the placement for you.

    • #7785
      hozefasmile
      Member

      @morganrt, I have seen that keeping html separate from php code for output of shortcode causes sometime issue. So keeping the html also within the php code will be better.

      For example for displaying a bootstrap button I have created this form file

      <?php
      /*
      Name: Button Primary
      shortcode: button_primary
      icon: dashicons-tickets-alt
      editor: true
      */
      
      piklist('field', array(
          'type' => 'text',
          'field' => 'button_url',
          'label' => 'Link url for Button',
          'attributes' => array(
            'class' => 'regular-text' // WordPress css class 
          )
        ));
      piklist('field', array(
          'type' => 'text',
          'field' => 'button_text',
          'label' => 'Text to show in Button',
          'attributes' => array(
            'class' => 'regular-text' // WordPress css class 
          )
        ));
      piklist('field', array(
          'type' => 'select',
          'field' => 'button_type',
          'label' => 'Select Button Type',
          'choices' => array(
            'btn-primary' => 'Primary Color',
            'btn-info' => 'Light Blue',
            'btn-danger' => 'Red'
          )
        ));
      ?>

      and then created the shortcode file like this

      <?php
      /*
      shortcode: button_primary
      */
      
      echo '<a href="' . $button_url . '" class="btn ' . $button_type . '">' . $button_text . '</a>' ;
      
      ?>
      

      This way it not cause any issue in display to front end. But if you try to keep it separate in html something like this it will cause error and not display your content in frontend.

      <?php
      /*
      shortcode: button_primary
      */
      ?>
      <a href="<?php echo $button_url; ?>" class="btn <?php echo $button_type; ?>"><?php echo $button_text; ?></a>
Viewing 2 reply threads
  • You must be logged in to reply to this topic.