将变量传递给 wordpress 函数失败


Passing variable to wordpress function fails

如果我在这个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

我认为您正在尝试在此处创建闭包:定义一次$option_name,然后在函数内部声明的不同函数中使用它,该函数$option_name作为参数传递。但是,我认为 PHP 不是这样工作的:函数只是在全局命名空间中声明,没有变量绑定。

若要实现所需的结果,可以将代码编写为类,并将不同的函数作为该类的方法。然后实例化该类的对象,并将 $object_name 作为属性。现在,您可以从不同的方法访问$this->object_name。请记住将对象传递给add_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');