How to initialize $id from another function?

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

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.

回答1:

From this question and your prior question, it looks like you are passing $field_args from nl_register_ga_code() to nl_display_setting(), like this nl_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:

$foo = array('bar' => 1234); 

You can't use $bar to get 1234, but you can use $foo['bar'] to get it.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!