0

I set up an admin settings menu and I'd like to output the fields as a short code.

The way I learned to output the fields from a tutorial was with:

<?php $input_examples = get_option('sandbox_theme_input_examples'); ?>
<?php echo sanitize_text_field( $input_examples[ 'textarea_example' ] ); ?> 

But putting that into the shortcode doesn't work:

function shortcode()
{

return '<?php $input_examples = get_option('sandbox_theme_input_examples'); ?>';
return '<?php echo sanitize_text_field( $input_examples[ 'textarea_example' ] ); ?>';

}
add_shortcode('shortcode', 'shortcode');

I'm guessing there is a specific way to put php code into the shortcodes that I'm not understanding?

I've looked all over and can't find a solution to this.

1 Answer 1

1

Your issues aren't shortcode-related, just some PHP syntax problems. I suggest enabling debugging so you can see PHP errors being generated.

Opening <?php and closing ?> php tags are for switching between html and php output. See escaping from html in PHP documentation.

A function can only return once, as it immediately ends execution and exits the function. See return in PHP docs.

It also couldn't hurt to familiarize yourself with strings and the proper use of single and double quotes.

function shortcode()
{
    $input_examples = get_option('sandbox_theme_input_examples');
    return '<img src="' . sanitize_text_field( $input_examples[ 'textarea_example' ] ) . '">';
}
add_shortcode('shortcode', 'shortcode');
2
  • That worked! And I'm very new to PHP just trying to learn as I go so thank you for the extra materials I'll read up on them! One more question though - How would I insert an html string into this though? For instance if the text field is a URL that I want converted into an HTML img string.
    – Derek
    Commented Oct 17, 2013 at 3:57
  • see edit above.
    – Milo
    Commented Oct 17, 2013 at 4:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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