简体   繁体   中英

How to change the value of the input field of custom WordPress admin page?

I made a custom WordPress Admin page with input fields . I would like to change the values of them in the constructor based on the database . I have no idea how to access them...

The function which is called in the constructor and which will handle the database init . The database column names and input field ids are the same:

public function update_inputs(){
    global $wpdb;
    $columns = $wpdb->get_col("DESC `dbtable-admin`", 0);
    foreach($columns as $column){
        /*if($column == field name){
            field name.value = $wpdb -> get_var("SELECT $column FROM `dbtable-admin`");
        }*/
    }
}

Field setup :

public function setup_fields()
{
    $fields = array(
        array(
            'uid' => 'page_name',
            'label' => 'Oldal neve',
            'section' => 'custom_page_settings',
            'type' => 'text',
        ) ,
        //Others are the same.
     );
     foreach ($fields as $field)
    {

        add_settings_field($field['uid'], $field['label'], array(
            $this,
            'field_callback'
        ) , 'custom-main-menu', $field['section'], $field);
        register_setting('custom-main-menu', $field['uid']);
    }
}

So I need to somehow access the fields outside the function.

The registered settings are stored in wordpress wp_options table by uid, you can access them using theget_option function. As your column name is same as your uid u can get the value of the field by get_option($column). You can update the values using update_option ($column, $new_value).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM