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

Wordpress theme settings not saving

programmeradmin0浏览0评论

I am trying to develop a theme. I am at the part of using the settings API. The code I use for the settings are as follows:

In the example snippets I'm using variables to show the structure of my code. Ofcourse in my actual code the variables are strings (the callback variables are no strings)

register_setting($option_group, $options_name);
add_settings_section($id, $title, $callback_section, $page);
add_settings_field($id, $title, $callback_field, $page, $section);

function $callback_section()
{
}

function $callback_field()
{
  echo '<input type="checkbox" name="$option_name" />';
}

for the form in my theme options page:

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

    <?php
          settings_fields($option_group);
          do_settings_sections($page);
          submit_button();
          settings_errors();
    ?>

</form>

My settings are saved but the input returns to the unchecked state. How can I make it work? Did I miss anything? Also.. I will need alot of settings. What is the best approach for handling alot of settings with the settings API without getting messy code?

I am trying to develop a theme. I am at the part of using the settings API. The code I use for the settings are as follows:

In the example snippets I'm using variables to show the structure of my code. Ofcourse in my actual code the variables are strings (the callback variables are no strings)

register_setting($option_group, $options_name);
add_settings_section($id, $title, $callback_section, $page);
add_settings_field($id, $title, $callback_field, $page, $section);

function $callback_section()
{
}

function $callback_field()
{
  echo '<input type="checkbox" name="$option_name" />';
}

for the form in my theme options page:

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

    <?php
          settings_fields($option_group);
          do_settings_sections($page);
          submit_button();
          settings_errors();
    ?>

</form>

My settings are saved but the input returns to the unchecked state. How can I make it work? Did I miss anything? Also.. I will need alot of settings. What is the best approach for handling alot of settings with the settings API without getting messy code?

Share Improve this question edited Jun 13, 2020 at 11:58 Leo asked Jun 13, 2020 at 7:09 LeoLeo 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0
$options = get_option( 'textdomain_settings' );
    ?>    
<input type='checkbox' name='textdomain_settings[textdomain_checkbox_field_0]' <?php checked( $options['textdomain_checkbox_field_0'], 1 ); ?> value='1'>
<?php

Use this settings under Callback Field Function or you can generate code form here for settings api directly.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论