【问题标题】:Call to undefined function settings_fields() in boiler plate plugin structure在样板插件结构中调用未定义的函数 settings_fields()
【发布时间】:2017-05-12 05:29:32
【问题描述】:

我正在基于样板结构创建插件,并且正在尝试创建管理菜单页面,但出现错误。 下面是我的代码。

下面的代码在 admin hook 内的 class-plugin-name.php 中,

$this->loader->add_action( 'admin_menu', $plugin_admin, 'define_admin_page' );
$this->loader->add_action( 'admin_init', $plugin_admin, 'register_setting' );

在 class-plugin-name-admin.php 中,

public function define_admin_page(){

        add_menu_page(
          __('SEO Boost', 'seo-boost'), 
          __('SEO Boost', 'seo-boost'), 
          'manage_options', 
          'seo-boost', 
          array(&$this, 'seo_boost_page_callback')
        );        
    }

    public function seo_boost_page_callback(){
        include_once 'partials/plugin-name-admin-display.php';
    }

    public function register_setting(){
        add_settings_section(
               $this->option_name.'_general-section',
               __( 'General', 'seo-boost' ), 
               array( $this, $this->option_name . '_general_line' ), 
               $this->plugin_name
               );

    add_settings_field(
           $this->option_name . '_text', 
           __("Text box label:", 'seo-boost'), 
           array( $this, $this->option_name . '_text_api_element' ),    
          $this->plugin_name, 
          $this->option_name.'general-section',
          array( 'label_for' => $this->option_name . '_text' )
          );

    register_setting($this->option_name.'general-section', $this->option_name . '_text');           

    }

    public function seo_boost_general_line(){
        echo '<p>' . __( 'Please change the settings accordingly.', 'outdated-notice' ) . '</p>';
    }

    public function seo_boost_text_api_element(){
        $text = get_option( $this->option_name . '_text' );
        echo '<input type="text" name="' . $this->option_name . '_text' . '" id="' . $this->option_name . '_text' . '" value="' . $text . '"> ' . __( 'text', 'seo-boost' );
    }

在 plugin-name-admin-display.php 中有显示管理页面表单字段的代码,

<div class="wrap">
            <h1><?php _e('Seo Settings', 'seo-boost'); ?></h1>
             <form action="options.php" method="post">
        <?php       
        settings_fields($this->option_name.'_general-section');
        do_settings_sections($this->plugin_name);
        submit_button(); ?>             
            </form>
        </div>

但我收到致命错误的错误:

在第 20 行的 plugin-name-admin-display.php 中调用未定义函数 settings_fields()

我正在使用此链接中的 ref 来创建插件。 REf. plugin

【问题讨论】:

  • 您注册的函数名称是 define_setting_fields 但您正在编写 setting_fields 并调用它。那么脚本如何获取函数呢?
  • 看看这个 - 在这个页面的底部codex.wordpress.org/Function_Reference/do_settings_fields,你将有类似的功能来处理。
  • do_settings_sections($this->plugin_name,$this->option_name.'_general-section');试试这个@AshPatel
  • @Exprator:不工作。 :(
  • 现在显示什么?

标签: php wordpress boilerplate


【解决方案1】:

令人难以置信的是,您必须在括号之间添加一个空格才能使调用生效。所以而不是:

settings_fields($this->option_name.'_general-section');

您应该将其编码为:

settings_fields( $this->option_name.'_general-section' );

【讨论】:

  • 括号和参数之间的空格不会以任何方式影响代码功能。
猜你喜欢
  • 2020-04-24
  • 2014-08-03
  • 2011-09-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-13
  • 1970-01-01
  • 2014-02-13
  • 1970-01-01
相关资源
最近更新 更多