$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'); ?>theme development - How to safely return the HTML?|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)

theme development - How to safely return the HTML?

matteradmin6PV0评论

I have

return '<button type="button" class="btn btn-success">Open Now</button>';

If I echo this it will work fine. I am getting an warning like echo is used without escaping. I know that it will be unnecessary to escape this as it is not something that user enters. But just to get rid of that error what can I do?

I tried esc_html($thehtmlabove); it does not get rendered. It prints like a text -> <button type="button" class="btn btn-success">Open Now</button>

Is there any way?

Full code:

public function opening_hour_html()
        {
            $type = $this->shopstatus(); // this will be 'Open Now' or 'Closed'
            if ($type == 'Open Now') {
                $wppl_open = __('Open Now','arika');
                return '<button type="button" class="btn btn-success">.'$wppl_open'.</button>';
            } else {
                $wppl_closed = __('Closed','arika');
                return '<button type="button" class="btn btn-danger">.'$wppl_closed'.</button>';
            }
        }

I have

return '<button type="button" class="btn btn-success">Open Now</button>';

If I echo this it will work fine. I am getting an warning like echo is used without escaping. I know that it will be unnecessary to escape this as it is not something that user enters. But just to get rid of that error what can I do?

I tried esc_html($thehtmlabove); it does not get rendered. It prints like a text -> <button type="button" class="btn btn-success">Open Now</button>

Is there any way?

Full code:

public function opening_hour_html()
        {
            $type = $this->shopstatus(); // this will be 'Open Now' or 'Closed'
            if ($type == 'Open Now') {
                $wppl_open = __('Open Now','arika');
                return '<button type="button" class="btn btn-success">.'$wppl_open'.</button>';
            } else {
                $wppl_closed = __('Closed','arika');
                return '<button type="button" class="btn btn-danger">.'$wppl_closed'.</button>';
            }
        }
Share Improve this question edited Nov 24, 2018 at 15:56 butlerblog 5,1413 gold badges28 silver badges44 bronze badges asked Nov 24, 2018 at 14:04 user145078user145078 6
  • Where are you getting the error? Are you testing the code somehow? Seems like your issue is with the test, not WordPress. – Jacob Peattie Commented Nov 24, 2018 at 14:11
  • 1 You can use wp_kses to ensure only what you specified is returned. But it seems like you might be better including that Html in a different way. If you could post the whole function and where you are using it, it may be easier to see. – Alvaro Commented Nov 24, 2018 at 14:13
  • @Alvaro thanks i have updated by question, i am using it inside a class. testing wp_kses – user145078 Commented Nov 24, 2018 at 14:24
  • @JacobPeattie it's not an error but a warning from themecheck plugin. – user145078 Commented Nov 24, 2018 at 14:25
  • 2 @LatheeshVMVilla which theme check plugin? Note that escaping isn't something you just magically wrap an entire document in and it's secure, you escape the variables at the most fine grain level possible, and output immediatley without returning HTML as variables. It's good practice to return values from function, not complex HTML fragments – Tom J Nowell Commented Nov 24, 2018 at 14:53
 |  Show 1 more comment

1 Answer 1

Reset to default 1

Ok, so this is why it's important to post the actual code that's giving you problems.

There is nothing wrong with this, which is all you had originally:

return '<button type="button" class="btn btn-success">Open Now</button>';

If that was the error, you could ignore it.

This, on the other hand, does need to be escaped:

$wppl_open = __('Open Now','arika');
return '<button type="button" class="btn btn-success">.'$wppl_open'.</button>';

The value of $wppl_open can't be trusted, because it can be affected by something from the outside by the user. A poorly configured or written translation file could cause this HTML to break, or worse, a malicious translation file could make this code unsafe. Also, the quotes are in the wrong place.

So you need to escape $wppl_open (and fix the quotes):

$wppl_open = __('Open Now','arika');
return '<button type="button" class="btn btn-success">' . esc_html( $wppl_open ) . '</button>';

Alternatively, you can perform the escaping at the same time as the translation function by using esc_html__():

$wppl_open = esc_html__('Open Now','arika');
return '<button type="button" class="btn btn-success">' . $wppl_open . '</button>';

But I can't guarantee that the theme check plugin you're using will recognise that as safe, because it still looks like you're echoing an unescaped variable if you just look at the last line.

Post a comment

comment list (0)

  1. No comments so far