在wordpress中创建自定义选项页面

时间:2014-06-09 12:17:33

标签: php wordpress

在我的wordpress主题的选项页面中创建自定义字段时,它在每个输入类型文本上显示一些错误call_user_func()期望参数1是有效的回调,类'MySettingsPage'没有方法logo_callback()或social_callback( )请提出一些解决方案

        private $options;

         public function __construct()
        {
            add_action( 'admin_menu', array( $this, 'bguru_register_options_page' ) );
            add_action( 'admin_init', array( $this, 'bguru_register_settings' ) );


        }

             public function bguru_register_options_page()
        {
            // This page will be under "Settings"
         add_theme_page('Business Guru Options',
                         'Theme Customizer', 
                         'edit_theme_options', 
                         'bguru-options', 
                         array( $this, 'bguru_options_page')
            );
        }

    public function bguru_options_page()
        {
            // Set class property
            $this->options = get_option( 'bguru_logo' );
             $this->options = get_option( 'bguru_vimeo' );


            ?>
            <div class="wrap">
                <?php screen_icon(); ?>
              <h1>Business Guru Options</h1>      
                <form method="post" action="options.php">
                <?php
                    // This prints out all hidden setting fields
                    settings_fields( 'defaultbg' );   
                    do_settings_sections( 'defaultbg' );
                    submit_button(); 
                ?>
                </form>
            </div>
            <?php
        }

        public function bguru_register_settings()
        {        
    register_setting('defaultbg','bguru_logo',  array( $this, 'sanitize' ) );
    register_setting('defaultbg', 'bguru_vimeo',  array( $this, 'sanitize' ));
           add_settings_section(
                'setting_section_id', // ID
                'General', 
                array( $this, 'print_section_info' ), // Callback
                'defaultbg' // Page

            );  

            add_settings_field(
                'bguru_logo', // ID
                'Logo', // Title 
                array($this,'logo_callback()' ), // Callback
                 'defaultbg', // Page
               'setting_section_id' // Section           
            );      


            add_settings_field(
                'bguru_vimeo', // ID
                'Vimeo', // Vimeo 
                array( $this, 'socialv_callback()' ), // Callback
                 'defaultbg', // Page
                'setting_section_id' // Section           
            );   

        }


     public function sanitize( $input )
        {
            $new_input = array();


                if( isset( $input['bguru_logo'] ) )
                $new_input['bguru_logo'] = sanitize_text_field( $input['bguru_logo'] );

                 if( isset( $input['bguru_vimeo'] ) )
                $new_input['bguru_vimeo'] = sanitize_text_field( $input['bguru_vimeo'] );

            return $new_input;
        }
    public function print_section_info()
        {
            print 'Enter your settings below:';
        }

    public function logo_callback()
        {
            printf(`enter code here`
                '<input type="text" id="bguru_logo"  size="50" name="bguru_logo" value="%s" />',
                isset( $this->options['bguru_logo'] ) ? esc_attr( $this->options['bguru_logo']) : ''
            );
        }

    public function socialv_callback()
        {
            printf(
                '<input type="text" id="bguru_vimeo"  size="50" name="bguru_vimeo" value="%s" />',
                isset( $this->options['bguru_vimeo'] ) ? esc_attr( $this->options['bguru_vimeo']) : ''
            );
        }

    }
    if( is_admin() )
        $my_settings_page = new MySettingsPage();

1 个答案:

答案 0 :(得分:0)

查看您的代码后,我认为您需要从()删除array($this,'logo_callback()' )

见下面的代码,我有变化&amp;修正: -

class MySettingsPage
{
    private $options;
    public function __construct()
    {
        add_action( 'admin_menu', array( $this, 'bguru_register_options_page' ) );
        add_action( 'admin_init', array( $this, 'bguru_register_settings' ) );
    }

    public function bguru_register_options_page()
    {
        // This page will be under "Settings"
        add_theme_page('Business Guru Options',
                'Theme Customizer',
                'edit_theme_options',
                'bguru-options',
                array( $this, 'bguru_options_page')
        );
    }

    public function bguru_options_page()
    {
        // Set class property
        $this->options['bguru_logo'] = get_option( 'bguru_logo' );
        $this->options['bguru_vimeo'] = get_option( 'bguru_vimeo' );
        ?>
        <div class="wrap">
            <?php screen_icon(); ?>
            <h1>Business Guru Options</h1>
            <form method="post" action="options.php">
                <?php
                // This prints out all hidden setting fields
                settings_fields( 'defaultbg' );
                do_settings_sections( 'defaultbg' );
                submit_button();
                ?>
            </form>
        </div>
        <?php
    }

    public function bguru_register_settings()
    {
        register_setting('defaultbg','bguru_logo',  array( $this, 'sanitize' ) );
        register_setting('defaultbg', 'bguru_vimeo',  array( $this, 'sanitize' ));
        add_settings_section(
                'setting_section_id', // ID
                'General',
                array( $this, 'print_section_info' ), // Callback
                'defaultbg' // Page

        );

        add_settings_field(
                'bguru_logo', // ID
                'Logo', // Title
                array($this,'logo_callback' ), // Callback
                'defaultbg', // Page
                'setting_section_id' // Section
        );

        add_settings_field(
                'bguru_vimeo', // ID
                'Vimeo', // Vimeo
                array( $this, 'socialv_callback' ), // Callback
                'defaultbg', // Page
                'setting_section_id' // Section
        );
    }

    public function sanitize( $input )
    {
        $new_input = array();

        if( isset( $input['bguru_logo'] ) )
            $new_input['bguru_logo'] = sanitize_text_field( $input['bguru_logo'] );

        if( isset( $input['bguru_vimeo'] ) )
            $new_input['bguru_vimeo'] = sanitize_text_field( $input['bguru_vimeo'] );

        return $new_input;
    }
    public function print_section_info()
    {
        print 'Enter your settings below:';
    }

    public function logo_callback()
    {
        printf('<input type="text" id="bguru_logo"  size="50" name="bguru_logo" value="%s" />',
                isset( $this->options['bguru_logo'] ) ? esc_attr( $this->options['bguru_logo']) : ''
        );
    }

    public function socialv_callback()
    {
        printf('<input type="text" id="bguru_vimeo"  size="50" name="bguru_vimeo" value="%s" />',
                isset( $this->options['bguru_vimeo'] ) ? esc_attr( $this->options['bguru_vimeo']) : ''
        );
    }
}
if( is_admin() )
    $my_settings_page = new MySettingsPage();

希望这对你有用。