$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - I want to display the content of a text field only if it has been entered|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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

} 
Post a comment

comment list (0)

  1. No comments so far