Wordpress插件添加\设置\字段精确代码在一个实例中有效,但在第二个实例中无效?

Wordpress插件添加\设置\字段精确代码在一个实例中有效,但在第二个实例中无效?,wordpress,Wordpress,我正在编写一个插件,使用组合“注册设置”、“添加设置”部分、“添加设置”字段创建管理选项页面。我有一个父菜单和两个子菜单。在Wordpress中创建管理页面非常简单。。。当您创建管理页面时,它将启动。这是我的参数。(这是在OOP中完成的) 所以请记住我的菜单上贴的“开球报告”是下一步需要的 class ClubHouseReports { function register() { add_action( 'admin_init', array( $thi

我正在编写一个插件,使用组合“注册设置”、“添加设置”部分、“添加设置”字段创建管理选项页面。我有一个父菜单和两个子菜单。在Wordpress中创建管理页面非常简单。。。当您创建管理页面时,它将启动。这是我的参数。(这是在OOP中完成的)

所以请记住我的菜单上贴的“开球报告”是下一步需要的

    class ClubHouseReports
{
    function register()
    {
        add_action( 'admin_init', array( $this, 'teeoff_report_sellector') );
        
    }
    
    function teeoff_report_sellector() {
        register_setting( 'my_options_group', 'timer_month' );
        register_setting( 'my_options_group', 'timer_day' );
        register_setting( 'my_options_group', 'timer_year' );
        
        add_settings_section( 'teeoff-club-options', 'Get Monthy Report', array( $this,     'teeoff_month_section'), 'tee_offs_reports');
        
        add_settings_field( 'timer_month', 'Month', array( $this, 'teeoff_month'), 'tee_offs_reports', 'teeoff-club-options');
        add_settings_field( 'timer_day', 'Day', array( $this, 'teeoff_day'), 'tee_offs_reports', 'teeoff-club-options');
        add_settings_field( 'timer_year', 'Year', array( $this, 'teeoff_year'), 'tee_offs_reports', 'teeoff-club-options');
        
    }
    function teeoff_month_section() {
        echo '<p>Reports</p>';
                
    }

    function teeoff_month() {
        $timer_month = esc_attr( get_option( 'timer_month' ) );
        echo '<input type="text" class="regular-text" name="timer_month" value="'.$timer_month.'" placeholder="Month" />';      
    }
    function teeoff_day() {
        $timer_day = esc_attr( get_option( 'timer_day' ) );
        echo '<input type="text" class="regular-text" name="timer_day" value="'.$timer_day.'" placeholder="Day" />';        
    }
    function teeoff_year() {
        $timer_year = esc_attr( get_option( 'timer_year' ) );
        echo '<input type="text" class="regular-text" name="timer_month" value="'.$timer_year.'" placeholder="Year" />';        
    }
    }
class Clubhouse报告
{
函数寄存器()
{
添加操作('admin_init',数组($this,'teeoff_report_sellector'));
}
函数teeoff_report_sellector(){
注册设置(“我的选项组”、“计时器月”);
注册设置(“我的选项组”、“计时器日”);
注册设置(“我的选项组”、“计时器年”);
添加“设置”部分(“teeoff club options”、“Get Monthy Report”、数组($this、“teeoff\u month\u section”)、“teeoff\u reports”);
添加设置字段('timer\u month','month',array($this,'teeoff\u month'),'teeoff\u reports','teeoff club options');
添加_设置_字段('timer_day','day',array('this,'teeoff_day'),'teeoff_offs_reports','teeoff club options');
添加_设置_字段('timer_year','year',array($this,'teeoff_year'),'teeoff_offs_reports','teeoff club options');
}
职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门{
回显“报告”

; } 功能启动月(){ $timer\u month=esc\u attr(获取选项('timer\u month'); 回声'; } 功能启动日(){ $timer_day=esc_attr(获取选项('timer_day')); 回声'; } 功能测试年(){ $timer_year=esc_attr(获取选项('timer_year'); 回声'; } }
上面的这个类是从根插件文件调用的,就像一个可以工作的类一样。创建一个实例,然后调用方法“register()”

在我的模板文件中,我这样做。。。。就像那个能工作的一样

    <?php settings_errors(); ?>

<form method="post" action="options.php">
    <?php settings_fields( 'my_options_group' ); ?>
    <?php do_settings_sections('teeoff-club-options'); ?>
    <?php submit_button(); ?>
</form>

所以我的问题是。。。除了在重复相同代码时需要更改值之外,在这种情况下,您需要为“register_setting”分配一个新的菜单段塞,一个新的名称,并确保它指向正确的回调,我向您保证一切都设置正确。。。由于某种原因,这不会发生

我没有收到任何错误,我看到的只是提交按钮,如果我使用它,我会收到一条“成功”消息,所以所有内容都在范围内。。。但是除了提交按钮外,“tee_offs_reports”页面上没有呈现任何内容。。。这是有道理的,因为它是硬编码的


所有这些都是为了说。。。我花了几十次检查我的代码,寻找最小/最愚蠢的语法错误,或者为什么这个管理区域不能工作,而另一个使用完全相同的结构和逻辑,可以100%工作。你看到这个代码有什么问题吗?不要介意上面代码之间的大间距,这只发生在我粘贴到这里的时候。。。。wordpress中有我不知道的限制吗?为什么这不起作用?虽然它的蓝图很好用。

好吧,现在我真的很困惑。。。我成功了,但我很怀疑我是否遵守了法典的规定。。。还是我??

这是100%有效的代码

function teeoff_report_sellector() {
        register_setting( 'my_options_group', 'timer_month' );
        register_setting( 'my_options_group', 'timer_day' );
        register_setting( 'my_options_group', 'timer_year' );
        
        add_settings_section('tee-off-reports', 'Get Monthy Report', array( $this, 'teeoff_month_section'), 'tee-off-reports');
        
        add_settings_field('timer_month', 'Month', array( $this, 'teeoff_month'), 'tee-off-reports', 'tee-off-reports');
        add_settings_field('timer_day', 'Day', array( $this, 'teeoff_day'), 'tee-off-reports', 'tee-off-reports');
        add_settings_field('timer_year', 'Year', array( $this, 'teeoff_year'), 'tee-off-reports', 'tee-off-reports');
        
    }
    function teeoff_month_section() {
        echo '<p>Reports</p>';
                
    }

    function teeoff_month() {
        $timer_month = esc_attr( get_option( 'timer_month' ) );
        echo '<input type="date" class="regular-text" name="timer_month" value="'.$timer_month.'" placeholder="Month" />';      
    }
    function teeoff_day() {
        $timer_day = esc_attr( get_option( 'timer_day' ) );
        echo '<input type="text" class="regular-text" name="timer_day" value="'.$timer_day.'" placeholder="Day" />';        
    }
    function teeoff_year() {
        $timer_year = esc_attr( get_option( 'timer_year' ) );
        echo '<input type="text" class="regular-text" name="timer_year" value="'.$timer_year.'" placeholder="Year" />';     
    }
函数teeoff\u report\u sellector(){
注册设置(“我的选项组”、“计时器月”);
注册设置(“我的选项组”、“计时器日”);
注册设置(“我的选项组”、“计时器年”);
添加_设置_部分('tee-off-reports','Get Monthy Report',数组($this,'teeoff\u month_部分'),'tee-off reports');
添加_设置_字段('timer_month','month',array($this,'teeoff_month'),'teeoff reports','teeoff reports');
添加_设置_字段('timer_day','day',array('this,'teeoff_day'),'teeoff reports','teeoff reports');
添加_设置_字段('timer_year','year',array($this,'teeoff_year'),'teeoff reports','teeoff reports');
}
职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门职能部门{
回显“报告”

; } 功能启动月(){ $timer\u month=esc\u attr(获取选项('timer\u month'); 回声'; } 功能启动日(){ $timer_day=esc_attr(获取选项('timer_day')); 回声'; } 功能测试年(){ $timer_year=esc_attr(获取选项('timer_year'); 回声'; }
我所做的是。。。我没有像codex告诉你的那样使用菜单slug,…(我创建的第一个管理页面就是这样工作的),而是用“add\u settings\u section”ID替换了它。。。因此,如果您查看每个“add_settings_field”(添加设置字段)的前一个参数,它们现在被分配了节的ID,codex应该在查找菜单段塞。。。我一直认为无论如何。。。我做的第一个也是这样,效果很棒。。。因此,“添加设置”字段的最后两个参数是相同的。奇怪的上面的代码中没有提到页面slug,但是这可以100%工作,并且不会破坏任何其他代码

现在我想知道。。。我在设置API中发现错误了吗?这将在以后修复,我的插件将停止工作?或者是我不顾一切地尝试了一大堆东西,才偶然发现了解决方案

我不明白。。。如果有一位真正的WordPress大师可以向我解释这一点,我将非常感激

function teeoff_report_sellector() {
        register_setting( 'my_options_group', 'timer_month' );
        register_setting( 'my_options_group', 'timer_day' );
        register_setting( 'my_options_group', 'timer_year' );
        
        add_settings_section('tee-off-reports', 'Get Monthy Report', array( $this, 'teeoff_month_section'), 'tee-off-reports');
        
        add_settings_field('timer_month', 'Month', array( $this, 'teeoff_month'), 'tee-off-reports', 'tee-off-reports');
        add_settings_field('timer_day', 'Day', array( $this, 'teeoff_day'), 'tee-off-reports', 'tee-off-reports');
        add_settings_field('timer_year', 'Year', array( $this, 'teeoff_year'), 'tee-off-reports', 'tee-off-reports');
        
    }
    function teeoff_month_section() {
        echo '<p>Reports</p>';
                
    }

    function teeoff_month() {
        $timer_month = esc_attr( get_option( 'timer_month' ) );
        echo '<input type="date" class="regular-text" name="timer_month" value="'.$timer_month.'" placeholder="Month" />';      
    }
    function teeoff_day() {
        $timer_day = esc_attr( get_option( 'timer_day' ) );
        echo '<input type="text" class="regular-text" name="timer_day" value="'.$timer_day.'" placeholder="Day" />';        
    }
    function teeoff_year() {
        $timer_year = esc_attr( get_option( 'timer_year' ) );
        echo '<input type="text" class="regular-text" name="timer_year" value="'.$timer_year.'" placeholder="Year" />';     
    }