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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

Solved 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