I would like to use $id to echo the id entered in other functions. Here is an example function using id:
function nl_register_ga_code() {
// Validation callback
register_setting( 'nl_theme_options', 'nl_theme_options', 'nl_validate_settings' );
// Add setting to section
add_settings_section( 'nl_footer_section', 'Footer', 'nl_display_footer_section', 'nl_theme_options.php' );
// Create textarea field
$field_args = array(
'type' => 'textarea',
'id' => 'nl_ga_code',
'name' => 'nl_ga_code',
'desc' => 'Paste Google Analytics code.',
'std' => '',
'label_for' => 'nl_ga_code'
);
// Label
add_settings_field( 'label_ga_code', 'Google Analytics Code', 'nl_display_setting', 'nl_theme_options.php', 'nl_footer_section', $field_args );
}
// Registers the setting
add_action( 'admin_init', 'nl_register_ga_code' );
The function I will be using the variable is this one:
function nl_display_setting ( $args ) {
extract( $args );
$option_name = 'nl_theme_options';
$options = get_option( $option_name );
switch ( $type ) {
case 'text':
$options[$id] = stripslashes( $options[$id] );
$options[$id] = esc_attr( $options[$id] );
echo "<input class='regular-text$class' type='text' id='$id' name='" . $option_name . "[$id]' value='$options[$id]'>";
echo ( $desc != '' ) ? "<br><span class='description'>$desc</span>" : "";
break;
I need to initialize $id but do not know how.
From this question and your prior question, it looks like you are passing
$field_args
fromnl_register_ga_code()
tonl_display_setting()
, like thisnl_display_setting($field_args)
. (If not, then you should be — based on your comments, that’s what you’re trying to do.)So, in
nl_display_setting()
, don’t refer to$id
,$type
, or$desc
; refer to$args['id']
,$args['type']
, and$args['desc']
, respectively.You cannot refer to an array key as though it were a standalone variable. So, if you have this:
You can’t use
$bar
to get1234
, but you can use$foo['bar']
to get it.