0

I'm trying to figure out how to display the output of a shortcode in a text field using wordpress settings api. My goal is to create a few shortcodes so that I can use one or the other along with some other text. In the example I'm trying to display the product title then some text in the text field such as sorry, [oosd-title] has sold out to display sorry, red short has sold out.

The shortcode was easy enough to do

function oosdfw_title_display() {

return get_the_title();
}
add_shortcode( 'oosd-title', 'oosdfw_title_display' );

And this generates the text field in the admin area. It's all working except the shortcode displays [oosd-title] instead of the product title

function oosdw_text_field_6_render(  ) { 

$options = get_option( 'oosdw_settings' );
?>
<input type="text" name="oosdw_settings[oosdw_text_field_6]" value="<? 
php echo $options["oosdw_text_field_6"]; ?>">
    <?php

}

I'm not quite sure what I have to do to get the shortcode to generate in the on the frontend. I did try esc_html but wasn't entirely sure how to use it or where to place it

1 Answer 1

0

Use a shortcode with parameters

function oosdfw_title_display($atts) {
    $a = shortcode_atts( array(
        'before' => 'Sorry, ',
        'after' => ' has sold out.',
    ), $atts );

    return $a['before'].get_the_title().$a['after'];
}

add_shortcode( 'oosd-title', 'oosdfw_title_display' );

Put shortcode in text field and modify before and after parameters to suit your requirements.

[oosd-title before="Sorry " after = " has sold out."]

There is no need to display the output of shortcode within text field

Front-end

$options = get_option( 'oosdw_settings' );
echo do_shortcode( $options["oosdw_text_field_6"]);

I hope this helps

7
  • I can't quite get it to work. I followed your updated answer and the product title doesn't display but it displays with text[oosd-title] The shortcode does work if I use it in the text editor though
    – John Cook
    Mar 1, 2019 at 9:35
  • Remove all other text from text field and try! Mar 1, 2019 at 10:00
  • Shortcodes works only if placed on separate line. Mar 1, 2019 at 10:08
  • Yep, tried that already. Was the first thing I tried
    – John Cook
    Mar 1, 2019 at 10:51
  • I read your question again and this is what I understood: You want sorry, [oosd-title] has sold out. in your text field to display sorry, red short has sold out on front-end. Am I right? Mar 1, 2019 at 11:20

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.