admin管理员组文章数量:1289515
working on my first plugin where a user can select an option from a dropdown and this selection gets saved in a code that gets written into the footer. That works, but the problem is, my default selection (as in the HTML) gets ignored.
The selection
<select id="location_select" name="location_select">
<option selected value="ch" <?php selected(get_option('location_select'), 'ch'); ?>><?php _e('CH', 'pluginName') ; ?></option>
<option value="de" <?php selected(get_option('location_select'), 'de'); ?>><?php _e('DE', 'pluginName') ; ?></option>
</select>
The code
public function print_code(){
$location = get_option('location_select');
$url = 'https://' . esc_html($location) . '.domain/js/script.js';
wp_enqueue_script('myID', $url, array('jquery'), null, true);
}
public function additional_attrs($tag, $handle, $src){
if ($handle === 'myID'){
$tag = '<script data-host="; data-dnt="false" src="' . esc_url( $src ) . '" id="myID" async defer></script>';
}
return $tag;
}
How is it possible to have the first selection <option selected ...
as a default selection in the code? Because right now, if a user does not save his selection, nothing gets written in the output code.
Thanks for any help!
working on my first plugin where a user can select an option from a dropdown and this selection gets saved in a code that gets written into the footer. That works, but the problem is, my default selection (as in the HTML) gets ignored.
The selection
<select id="location_select" name="location_select">
<option selected value="ch" <?php selected(get_option('location_select'), 'ch'); ?>><?php _e('CH', 'pluginName') ; ?></option>
<option value="de" <?php selected(get_option('location_select'), 'de'); ?>><?php _e('DE', 'pluginName') ; ?></option>
</select>
The code
public function print_code(){
$location = get_option('location_select');
$url = 'https://' . esc_html($location) . '.domain/js/script.js';
wp_enqueue_script('myID', $url, array('jquery'), null, true);
}
public function additional_attrs($tag, $handle, $src){
if ($handle === 'myID'){
$tag = '<script data-host="https://domain" data-dnt="false" src="' . esc_url( $src ) . '" id="myID" async defer></script>';
}
return $tag;
}
How is it possible to have the first selection <option selected ...
as a default selection in the code? Because right now, if a user does not save his selection, nothing gets written in the output code.
Thanks for any help!
Share Improve this question edited Jul 22, 2021 at 9:59 Buttered_Toast 2,8191 gold badge8 silver badges21 bronze badges asked Jul 22, 2021 at 9:33 ParanoiaParanoia 252 silver badges11 bronze badges1 Answer
Reset to default 0Solved by updating the second value in get_option
like
$location = get_option('location_select', 'ch');
Codex Reference: https://developer.wordpress/reference/functions/get_option
本文标签: plugin developmentAdd default value from selection ltselectgt
版权声明:本文标题:plugin development - Add default value from selection <select> 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741427467a2378159.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论