最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Refactor create_function

programmeradmin1浏览0评论

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 

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 
Share Improve this question asked Jun 13, 2019 at 0:40 Jobbie DaddyJobbie Daddy 771 silver badge8 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 5

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) as it seems $a is not used anyway.

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

You should be able to change create_function( '$a', "return null;" ) to Anonymous Function (aka Closure) :

From :

    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;" )
    );

To :

        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', 
                function($a){ return null; }
        );

full code changes :

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'],
          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, 
                    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', 
                    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, 
                    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', 
                    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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论