Skip to content
Advertisement

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.

Advertisement

Answer

The registered settings are stored in wordpress wp_options table by uid, you can access them using the get_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).

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement