Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Better code for my shortcodes WordPress

  • SOLVED

Below, my code problem

add_shortcode('shortcode_one', 'myoptions_one',100);
function myoptions_one() { ?>
<p class="credit"><?php the_field(options_one, 'options'); ?></p>
<?php }


I do not know php. I create shortcodes using the above method, and the shortcode content always shows up elsewhere on the site. If in a text widget, it will show before the title. What is the correct way to display code like this in my functions file?
This function I found and tested and it worked fine, so I know it's the way I am excuting

function show_current_year(){
return date('Y');
}
add_shortcode('show_current_year', 'show_current_year');

And it behaved correctly, but I do not know how to wh

Answers (5)

2011-08-30

Hai Bui answers:

try this:

add_shortcode('shortcode_one', 'myoptions_one',100);

function myoptions_one() {

return '<p class="credit">'.get_field(options_one, 'options').'</p>';

}


Rick Bible comments:

this worked great, I'll learn something, thanks

2011-08-30

Kailey Lampert answers:

Do you know where 'the_field()' function is coming from?

If it was defined using WordPress logic, this may work (but if get_the_field doesn't exist, it will break):

add_shortcode('shortcode_one', 'myoptions_one',100);
function myoptions_one() {
return '<p class="credit">'. get_the_field(options_one, 'options'). '</p>';
}


Otherwise, you'll have to do as AdamGold suggests and find the where the function is defined to change 'echo' to 'return' in order to use 'the_field'

2011-08-30

Jurre Hanema answers:

You should not (as you do now) echo the content you want to display in the function, but instead you should return it from the function:


add_shortcode('shortcode_one', 'myoptions_one',100);

function myoptions_one()
{
return 'the content you want do display';
}


Unfortunately, I don't know where the function "the_field()" you are using comes from, otherwise I could've given you a complete example.


Rick Bible comments:

"the field is actually what is supposed to be placed here... see [[LINK href="http://plugins.elliotcondon.com/advanced-custom-fields/documentation/retrieving-values-from-the-options-page/"]][[/LINK]]
<?php the_field(options_one, 'options'); ?> works just like that on any template

2011-08-30

AdamGold answers:

add_shortcode('shortcode_one', 'myoptions_one',100);
function myoptions_one() { ?>
$return_html = '
<p class="credit">' . the_field('options_one', 'options') . '</p>';

return $return_html;
}


Is the_field a custom function you created? if so you will have to return the output there and not echo it.

let's say this is your function:
function the_field($var1, $var2) {
echo 'Change a bit here';
}


You should turn it to:

function the_field($var1, $var2) {
return 'Change a bit here';
}

2011-08-30

Ivaylo Draganov answers:

That's right - you ought to return the output, rather than echoing it (or typing in HTML out of the PHP tags). Something along the lines of:


add_shortcode('shortcode_one', 'myoptions_one');

function myoptions_one() {

$r = '<p class="credit">';
$r .= get_the_field(options_one, 'options');
$r .= '</p>';

return $r;

}


But you need to find a way go <em>get</em> the results from <em>the_field</em> function. It's not native to WP but I guess there's a function <em>get_the_field</em> that does that or some parameter that has to be passed to the function. Where is that function coming from?