0

I am trying to create a simple option page. The options consist of a text field per user role. But I don't want to edit the code every time I add a new user role.

So I am trying to "for each"- user roles and build text fields from it. But the value does not get saved in the options. I think I need to save the values in an array before save.

Like this:

add_settings_field( 
'weborder_leveranstext',
__( 'Header', 'weborder' ), 
'weborder_leveranstext_render', 
'pluginPage', 
'weborder_pluginPage_leveransalternativ'

);

function weborder_leveranstext_render(  ) {

   $options = get_option( 'weborder_settings' );
    
   global $wp_roles;
   $all_roles = $wp_roles->get_names();

   foreach ($all_roles as $role => $value) {
      echo '<input type="text" name="??" value="' . $options['weborder_leveranstext'] . '">';
   }
}

I have tried a lot of thing but I think that this must be saved in array. I have tried similar thing to this without success:

echo '<input type="text" name="weborder_leveranstext_'.$role.'" value="' . $options['weborder_leveranstext_'.$role.''] . '">';
0

Okey I got it. I was close the whole time :)

function weborder_leveranstext_render(  ) {

$options = get_option( 'weborder_settings' );
    
global $wp_roles;
$all_roles = $wp_roles->get_names();

foreach ($all_roles as $role => $value) {
    echo '<br><span style="width: 180px; display: inline-block;">' . $value . '</span>';
    echo '<input type="text" style="width: 300px;" name="weborder_settings[weborder_leveranstext]['.$role.']" value="'.$options['weborder_leveranstext'][$role].'">';
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.