plugin development - What is the use of get_option method

I am a new learner of Wordpress plugin. recently I came across a method get_option(). When I pass the page parameter of

I am a new learner of Wordpress plugin. recently I came across a method get_option(). When I pass the page parameter of add_settings_field() function then it outputs all the input fields (checkboxes) of that page as true if checkbox is checked and false if checkbox is unchecked.

I didn't understand how does the function know that whether a checkbox is checked or not. May be I'm not able to focus the part of code where this is being decided.

I will be grateful if anyone explains to me the use of the function and how it works?

I am a new learner of Wordpress plugin. recently I came across a method get_option(). When I pass the page parameter of add_settings_field() function then it outputs all the input fields (checkboxes) of that page as true if checkbox is checked and false if checkbox is unchecked.

I didn't understand how does the function know that whether a checkbox is checked or not. May be I'm not able to focus the part of code where this is being decided.

I will be grateful if anyone explains to me the use of the function and how it works?

Share Improve this question edited Nov 26, 2019 at 18:01 fahad shaikh asked Nov 25, 2019 at 17:03 fahad shaikhfahad shaikh 1455 bronze badges 2
  • Hi! Welcome to the community. I'm sorry but your question seems about a very specific code, maybe in one theme or plugin, would you mind to share in your question where did you find the argument $page passed to the add_settings_field function? – Jesús Franco Commented Nov 26, 2019 at 1:38
  • Just to put you on the right track, I've shared a simple reference to the WP documentation and explained it a bit as an answer, However if you walk the extra mile and share with us what code do you have in front on you and what you are trying to achieve, boht could be significatively enhanced; the question (yours) and answer (mine, or even yours and earning extra points to show your achieved kwowledge!). – Jesús Franco Commented Nov 26, 2019 at 1:56
Add a comment  | 

1 Answer 1

Reset to default 2

Well, as you can see in the contributed example to the WordPress Reference, it is not that add_settings_field magically knows about a setting being true or false, and it is not limited to checkboxes (boolean flags):

Instead, the function signature states clearly that the $callback parameter it is required. And the introduction says:

The $callback argument should be the name of a function that echoes out the html input tags for this setting field. Use get_option() to retrieve existing values to show.

Maybe it should say that get_option it's the recommended (as it is the easier) way to show stored setting in the database, but you decide how do you use it in your callback function, and how that control renders the option.

In this example provided, yes, it is a checkbox that is checked (or not), depending on the value stored:

function callback_input_myid() {
    echo "<input type='checkbox' id='mynewcheckboxID' value='1'"
    if ( get_option('mynewcheckboxID') == '1' ) {
        echo ' checked';
    }
    echo '/>';
}

But as I've said before, it is not limited to boolean settings and checkboxes therefore. You could store a text, a color and your function render a ColorPicker, to store a date therefore render a DatePicker, even store a file in base64, however I'm not sure that should be advisable, just saying what is rendered and what option is retrieved it is entirely your responsibility.

Put it simple, get_option is just a convenient and pluggable way to do a SELECT FROM options table.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744965519a4603656.html

相关推荐

  • plugin development - What is the use of get_option method

    I am a new learner of Wordpress plugin. recently I came across a method get_option(). When I pass the page parameter of

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信