-1

This question already has an answer here:

Ive created a custom shortcode via functions.php to display a phone number in the content when the shortcode is used in the WYSIWYG field.

function contact() {
  $contact = get_field('contact',options);
  echo $contact;
}
add_shortcode('contact', 'contact');

However in the content editor when we write "contact us on [contact]" it comes out on the front end with the phone number first and then

contact us on

on the next line.

I've also tried this:

function contact() {
  $contact = get_field('contact',options);
  printf ('%s',$contact);
}
add_shortcode('contact', 'contact');

How can I please get this string to be included in the paragraph and not be on its own line above any content?

Thanks, Adrian

marked as duplicate by Milo, Pieter Goosen, Howdy_McGee, kaiser Apr 4 '16 at 15:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

The 'contact' function should return a string instead of echoing.

So the above, should look like:

function contact() {
  $contact = get_field('contact',options);
  return $contact;
}
add_shortcode('contact', 'contact');

Shortcodes are written by providing a handler function. Shortcode handlers are broadly similar to WordPress filters: they accept parameters (attributes) and return a result (the shortcode output).

See the codex for Shortcodes

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