- This topic has 2 replies, 3 voices, and was last updated 4 years, 12 months ago by
hozefasmile.
-
AuthorPosts
-
-
January 17, 2017 at 8:12 am #7724
morganrtMemberHello,
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_APIIs the use of echo a Piklist-only thing or something to be avoided?
-
January 17, 2017 at 5:43 pm #7733
-
February 10, 2017 at 11:11 am #7785
hozefasmileMember@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>
-
-
AuthorPosts
- You must be logged in to reply to this topic.