只有主题选项中的最后一个选项将保存到数据库

时间:2020-03-29 作者:Ahmad Ali

我有这个管理页面的功能,我试图提示管理员输入一些文本数据。

只有最后一个字段的问题正在保存并存在于数据库中,顶级字段在数据库中根本不存在。

向管理页面添加选项:

 register_setting( \'top-data-group\', \'yourLogo\');
 register_setting( \'card1-group\', \'card1_text\');
 register_setting( \'card4-group\', \'card4_text\');


  add_settings_section(\'top-options\', \'Top Information\', \'top_options\', \'top-data\' );
  add_settings_section(\'card1\', \'Card 1\', \'card1_options\', \'top-data\' );
  add_settings_section(\'card4\', \'Card 4\', \'top_options\', \'top-data\' );


 add_settings_field(\'yourLogo\', \'Logo Image URL:\', \'yourLogoImage_callback\', \'top-data\', \'top-options\'  );
 add_settings_field(\'card1_text\', \'card 1 text:\', \'card1_text_callback\', \'top-data\', \'card1\'  );
 add_settings_field(\'card4_text\', \'card 4 text:\', \'card4_text_callback\', \'top-data\', \'card4\'  );
回调函数:


function card1_text_callback (){
    $preText =  esc_attr( get_option(\'card1_text\'));
     echo \' <input type="text" name="card1_text" placeholder="card text" size="50" value="\'.$preText. \'" > \';
 }


function card4_text_callback (){
    $preText =  esc_attr( get_option(\'card4_text\'));
     echo \' <input type="text" name="card4_text" placeholder="card text" size="50" value="\'.$preText. \'" > \';
 }

function yourLogoImage_callback (){
    $preText =  esc_attr( get_option(\'yourLogoImage\'));
     echo \' <input type="text" name="yourLogoImage" placeholder="your Logo Image URL" size="50" value="\'.$preText. \'" > <p> use external or internal image url , preferred (300 * 50 px) </p>\'   ;
 }
表单页面:

<h1> Top Section </h1>
<?php settings_errors(); ?>


<form action="options.php" method="post" >  
    <?php 


    settings_fields(\'top-data-group\');
    do_settings_sections(\'top-data\');

    settings_fields(\'card1-group\');
    do_settings_sections(\'card1\');

    settings_fields(\'card4-group\');
    do_settings_sections(\'card4\');

    submit_button(\'save\', \'primary sub-button\', \'submit\', true);

  ?>
</form>
只有最后一个选项保存到数据库中,最上面的选项都是空的。

1 个回复
最合适的回答,由SO网友:Ahmad Ali 整理而成

我的解决方法如下:

1-regester_setting() id不应与任何其他id匹配,这是您必须在回调函数中使用的id。

2-您不需要使用add_setting_field() 任何地方的id。

3-将所有部分放在一个选项组中。

4-内页add_settings_section()add_setting_field() 应与呈现的页段塞相匹配add_menu_page()add_submenu_page().

现在代码如下:

向管理页面添加选项:

更改:删除每个组选项,仅保存一个并添加到每个regester_settings(). 已更改页面slug以匹配上的页面add_settings_field().


    add_submenu_page(\'top\', \'top\', \'top\', \'manage_options\', \'top-data\', \'top_init\' );


 register_setting( \'top-data-group\', \'yourLogo\');
 register_setting( \'top-data-group\', \'card1_text\');
 register_setting( \'top-data-group\', \'card4_text\');


  add_settings_section(\'top-options\', \'Top Information\', \'top_options\', \'top-data\' );
  add_settings_section(\'card1\', \'Card 1\', \'card1_options\', \'top-data\' );
  add_settings_section(\'card4\', \'Card 4\', \'top_options\', \'top-data\' );


 add_settings_field(\'random3\', \'Logo Image URL:\', \'yourLogoImage_callback\', \'top-data\', \'top-options\'  );
 add_settings_field(\'crandom1\', \'card 1 text:\', \'card1_text_callback\', \'top-data\', \'card1\'  );
 add_settings_field(\'random2\', \'card 4 text:\', \'card4_text_callback\', \'top-data\', \'card4\'  );
回调函数:

更改:已更改get_options()name= 与上的ID匹配的属性regester_settings()


function card1_text_callback (){
    $preText =  esc_attr( get_option(\'card1_text\'));
     echo \' <input type="text" name="card1_text" placeholder="card text" size="50" value="\'.$preText. \'" > \';
 }


function card4_text_callback (){
    $preText =  esc_attr( get_option(\'card4_text\'));
     echo \' <input type="text" name="card4_text" placeholder="card text" size="50" value="\'.$preText. \'" > \';
 }

function yourLogoImage_callback (){
    $preText =  esc_attr( get_option(\'yourLogo\'));
     echo \' <input type="text" name="yourLogoImage" placeholder="your Logo Image URL" size="50" value="\'.$preText. \'" > <p> use external or internal image url , preferred (300 * 50 px) </p>\'   ;
 }
表单页面:

更改:删除每个组选项,仅保存一个。

<h1> Top Section </h1>
<?php settings_errors(); ?>


<form action="options.php" method="post" >  
    <?php 


    settings_fields(\'top-data-group\');
    do_settings_sections(\'top-data\');



    submit_button(\'save\', \'primary sub-button\', \'submit\', true);

  ?>
</form>