0

I am trying to make a plugin, so far everything is working, the problem is when its time to "save changes" the text are saved but the selected 2 checkbox didnt save so both remains unchecked.

Heres the screenshot, the inputed text in input type is saved so its working, but the selected 2 checkbox remained unchecked, why is this?

enter image description here

Here's my code

add_action( 'admin_init', function() {
    register_setting( 'my-awesome-plugin-settings', 'map_option_1' );
    register_setting( 'my-awesome-plugin-settings', 'map_option_2' );
    register_setting( 'my-awesome-plugin-settings', 'map_option_3' );
    register_setting( 'my-awesome-plugin-settings', 'map_option_4' );
    register_setting( 'my-awesome-plugin-settings', 'map_option_5' );
    register_setting( 'my-awesome-plugin-settings', 'map_option_6' );

});

    function sort_post_plugin() {
      ?>
        <div class="wrap">
          <form action="options.php" method="post">
            <?php
              settings_fields( 'my-awesome-plugin-settings' );
              do_settings_sections( 'my-awesome-plugin-settings' );
            ?>
           <div class="title-set"><h1>SORT POST SETTINGS</h1></div>
            <table class="mytable">
                <tr>
                    <th>Posty Type</th>
                    <td><input type="text" placeholder="Post Type" name="map_option_1" value="<?php echo esc_attr( get_option('map_option_1') ); ?>" size="50" /></td>

                </tr>
                <tr>
                    <th>Meta Key</th>
                     <td><input type="text" placeholder="Meta Key" name="map_option_2" value="<?php echo esc_attr( get_option('map_option_2') ); ?>" size="50" /></td>
                </tr>
                <tr>
                    <th>Sorts</th>
                     <td><input type="checkbox" placeholder="Meta Key" name="map_option_3" />Name<br>
                      <input type="checkbox" id="cb_name" name="map_option_4">Price<br><input type="text" name="metakey" id="metakey" placeholder="Meta Key"></td>

                </tr>

                <tr>
                    <td><?php submit_button(); ?></td>
                </tr>

            </table>
          </form>
        </div>
      <?php
    }
     ?>

I hope you can help me, this is my first time creating a wp plugin. Thank you!

0

Your code just outputs empty checkboxes. You have to check each box's value and then set checked="checked" if it's true.

<td><input type="checkbox" placeholder="Meta Key" name="map_option_3"
    <?php // check whether map_option_3 was saved
    if(isset(get_option('map_option_3'))) { ?>checked="checked"<?php } ?>
 />Name<br>
<input type="checkbox" id="cb_name" name="map_option_4"
    <?php // check whether map_option_4 was saved
    if(isset(get_option('map_option_4'))) { ?>checked="checked"<?php } ?>
/>Price<br><input type="text" name="metakey" id="metakey" placeholder="Meta Key"></td>

You should also make name and price into actual form labels, not just plain text beside the boxes. :)

  • Thanks, but this makes the page "This page is not working, HTTP ERROR 500", But it finally works when I removed the 'isset'. Thank you! – Yuki Nekomimi Jun 23 '18 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.