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];


1 Answer 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']; 

  • Doesn't seem to work. When I test it on my staging site I now get a 500 error
    – John Cook
    Feb 3, 2019 at 11:06
  • I just figured it out. There's a barcket ) missing at the end of the if line, but when I fixed that I'm still seeing the css. Would it be because I'm using a text field and have previously entered something there? I deleted the content and clicked save and it's empty, so I'd assume it shouldn't show anything
    – John Cook
    Feb 3, 2019 at 11:17
  • @JohnCook Try my updated answer, you should also check if the value is not empty. Since get_option will return false when the key is not exists, the isset conditionals will always true.
    – hamdirizal
    Feb 3, 2019 at 11:36
  • You're missing quotes around the array indexes... Feb 3, 2019 at 12:14
  • @JacobPeattie I picked that up too. I'm going to mark this as the correct answer as it's now working perfectly. I knew it has to be an if statement, but wasn't exactly sure how to do it. I'm glad I asked. I'm a bit excited doing my first plugin and I hope it all works in the end
    – John Cook
    Feb 3, 2019 at 13:36

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.