将变量传递给wordpress函数失败

时间:2013-09-09 10:13:20

标签: php

如果我在这个wordpress函数中使用变量它怎么可能不起作用?

////////
// Creating theme settings
////////
create_theme_setting('theme_social_options');

////////
// function to create theme settings easily
////////
function create_theme_setting($option_name) {

        ////////
        // Theme settings initialize social options
        ////////
        function theme_intialize_options() {
            // If the social options don't exist, create them.
            if (false == get_option($option_name)) {   
                add_option($option_name);
            }

            add_settings_section(
                'social_settings_section',          // ID used to identify this section and with which to register options
                'Social Options',                   // Title to be displayed on the administration page
                'social_options_callback',          // Callback used to render the description of the section
                $option_name                        // 
            );

            add_settings_field( 
                'twitter',                      
                'Twitter',                          
                'twitter_callback', 
                $option_name, 
                'social_settings_section'
            );

            register_setting(
                $option_name,
                $option_name,
                'theme_sanitize_options'
            );
        }
        add_action('admin_init', 'theme_intialize_options');
        //
        //
        function social_options_callback() {
            echo '<p>Provide the URL to the social networks you\'d like to display.</p>';
        }
        //
        //
        function twitter_callback() {
            //First, we read the options
            $options = get_option($option_name);
            //Next, we need to make sure the element is defined in the options. If not, we'll set an empty string.
            $url = '';
            if(isset($options['twitter'])) {
                $url = $options['twitter'];
            }
            //Render the output
            echo '<input type="text" id="twitter" name="' . $option_name . '[twitter]" value="' . $options['twitter'] . '" />';
        }
        //
        //
        function theme_sanitize_options($input) {
            $output = array();

            //Loop through each of the options sanitizing the data
            foreach($input as $key => $val) {
                if (isset($input[$key])) {
                    $output[$key] = esc_url_raw(strip_tags(stripslashes($input[$key])));
                }
            }

            //Return the new collection
            return apply_filters('theme_sanitize_options', $output, $input);
        }

} //End create_theme_setting

1 个答案:

答案 0 :(得分:0)

我认为你试图在这里创建闭包:定义$option_name一次,并在函数内声明的不同函数中使用它,并将$option_name作为参数传递。但是,我认为PHP不会那样工作:函数只是在全局命名空间中声明,没有变量绑定。

要获得所需的结果,可以将代码编写为类,使用不同的函数作为该类的方法。然后使用$object_name作为属性实例化该类的对象。现在,您可以从不同的方法访问$this->object_name。请记住pass the objectadd_action,而不是类,如下:

// There might be syntax errors here, it's been a while since I wrote PHP code
class SettingTheme {
    public function __construct($option_name)
    {
         $this->option_name = $option_name
         add_action('admin_init', array($this, 'theme_intialize_options'));
    }

    public function theme_intialize_options() {
    {
        if (false == get_option($this->option_name)) {   
            add_option($this->option_name);
        }

        // ...
    }
}

$st = new SettingTheme('theme_social_class');