Refactor create_function

by Jobbie Daddy   Last Updated June 13, 2019 01:08 AM

I'm getting several create_function is depreciated errors from a plugin as the result of a recent WP upgrade to 5.2. The plugin is no longer supported which means I need to refactor the code myself. Would anyone be able to get me pointed in the right direction?

    function _options_page(){
    if($this->args['page_type'] == 'submenu'){
        if(!isset($this->args['page_parent']) || empty($this->args['page_parent'])){
            $this->args['page_parent'] = 'themes.php';
        }
        $this->page = add_theme_page(
                        $this->args['page_parent'],
                        $this->args['page_title'], 
                        $this->args['menu_title'], 
                        $this->args['page_cap'], 
                        $this->args['page_slug'], 
                        array(&$this, '_options_page_html')
                    );
    }else{
        $this->page = add_theme_page(
                        $this->args['page_title'], 
                        $this->args['menu_title'], 
                        $this->args['page_cap'], 
                        $this->args['page_slug'], 
                        array(&$this, '_options_page_html'),
                        $this->args['menu_icon'],
                        $this->args['page_position']
                    );

    if(true === $this->args['allow_sub_menu']){

        //this is needed to remove the top level menu item from showing in the submenu
        add_theme_page($this->args['page_slug'],$this->args['page_title'],'',$this->args['page_cap'],$this->args['page_slug'],create_function( '$a', "return null;" ));


        foreach($this->sections as $k => $section){

            add_theme_page(
                    $this->args['page_slug'],
                    $section['title'], 
                    $section['title'], 
                    $this->args['page_cap'], 
                    $this->args['page_slug'].'&tab='.$k, 
                    create_function( '$a', "return null;" )
            );

        }

        if(true === $this->args['show_import_export']){

            add_theme_page(
                    $this->args['page_slug'],
                    __('Import / Export', 'nhp-opts'), 
                    __('Import / Export', 'nhp-opts'), 
                    $this->args['page_cap'], 
                    $this->args['page_slug'].'&tab=import_export_default', 
                    create_function( '$a', "return null;" )
            );

        }//if

        foreach($this->extra_tabs as $k => $tab){

            add_theme_page(
                    $this->args['page_slug'],
                    $tab['title'], 
                    $tab['title'], 
                    $this->args['page_cap'], 
                    $this->args['page_slug'].'&tab='.$k, 
                    create_function( '$a', "return null;" )
            );

        }
        if(true === $this->args['dev_mode']){

            add_theme_page(
                    $this->args['page_slug'],
                    __('Dev Mode Info', 'nhp-opts'), 
                    __('Dev Mode Info', 'nhp-opts'), 
                    $this->args['page_cap'], 
                    $this->args['page_slug'].'&tab=dev_mode_default', 
                    create_function( '$a', "return null;" )
            );

        }//if
    }//if           


    }//else
    add_action('admin_print_styles-'.$this->page, array(&$this, '_enqueue'));
    add_action('load-'.$this->page, array(&$this, '_load_page'));
}//function 


Answers 1


There is an inbuilt function for returning null: __return_null.

So you can replace create_function('$a', 'return null;'); with just '__return_null' (note the quotes.)

Or you can use an anonymous function as the argument directly: function() {return null;} (no quotes). Either way since the argument is expecting a function calback.

majick
majick
June 13, 2019 01:07 AM

Related Questions


Updated April 26, 2015 21:03 PM

Updated August 20, 2017 18:08 PM

Updated March 23, 2016 08:03 AM

Updated October 15, 2017 11:08 AM

Updated October 25, 2017 20:08 PM