plugin development - I want to display the content of a text field only if it has been entered

admin2025-06-03  4

I need a little help with my very first plugin. It's not that big a plugin and consists of a small settings page with 3 text fields that display on the woocommerce checkout page with some css.

I'm stuck at one bit though. I want to display the fields only if they have something to display. At the moment when they're empty, it will display the background css styling and still behave on click. I just want to have it not display if the field is empty.

In the public folder of the plugin I've got this. I hope someone can shed some light on it for me

// first button below billing form
add_action('woocommerce_after_checkout_billing_form', 'show_my_message');
    function show_my_message() {

    $wc_checkout_message_settings = get_option('wc_checkout_message_settings');

        echo $wc_checkout_message_settings[wc_checkout_message_text_field_0];

        echo '<br>';

        echo $wc_checkout_message_settings[wc_checkout_message_text_field_1];

} 
// second set of buttons linking to payment only section
add_action('woocommerce_review_order_before_payment', 'show_new_message');
    function show_new_message(){

    $wc_checkout_message_settings = get_option('wc_checkout_message_settings');

    echo $wc_checkout_message_settings[wc_checkout_message_text_field_2];

}

I need a little help with my very first plugin. It's not that big a plugin and consists of a small settings page with 3 text fields that display on the woocommerce checkout page with some css.

I'm stuck at one bit though. I want to display the fields only if they have something to display. At the moment when they're empty, it will display the background css styling and still behave on click. I just want to have it not display if the field is empty.

In the public folder of the plugin I've got this. I hope someone can shed some light on it for me

// first button below billing form
add_action('woocommerce_after_checkout_billing_form', 'show_my_message');
    function show_my_message() {

    $wc_checkout_message_settings = get_option('wc_checkout_message_settings');

        echo $wc_checkout_message_settings[wc_checkout_message_text_field_0];

        echo '<br>';

        echo $wc_checkout_message_settings[wc_checkout_message_text_field_1];

} 
// second set of buttons linking to payment only section
add_action('woocommerce_review_order_before_payment', 'show_new_message');
    function show_new_message(){

    $wc_checkout_message_settings = get_option('wc_checkout_message_settings');

    echo $wc_checkout_message_settings[wc_checkout_message_text_field_2];

}
Share Improve this question asked Feb 3, 2019 at 9:38 John CookJohn Cook 673 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -1

Do echo inside an if statement.

function show_my_message() {
  $settings = get_option('wc_checkout_message_settings');

  //Check if the array is set
  if( isset($settings['wc_checkout_message_text_field_0']) && !empty($settings['wc_checkout_message_text_field_0']) ){
    echo $settings['wc_checkout_message_text_field_0'];   
  } 

  //Check the other value and show it
  if(isset($settings['wc_checkout_message_text_field_1']) && !empty($settings['wc_checkout_message_text_field_1']) ){
    echo $settings['wc_checkout_message_text_field_1']; 
  }

} 
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748948478a315073.html

最新回复(0)