WordPress.org

Forums

Submit button not saving settings (17 posts)

  1. partmont
    Member
    Posted 2 years ago #

    Hello, I have coded a plugin to have its own menu page for my client. the point of the plugin is so they can go in and update the time and location of where their food truck is goin to be. when i put in the info and press submit it erases all the fields i entered. i cant figure out what i am doing wrong.. is it in the function call back?

    Below is my code:

    <?php
    /*
    Plugin Name: THE SHISH SCHEDULE
    Plugin URI: http://www.peterartmont.com/thebigshish
    Description: This widget will let you update where you will be for the week and where u will be in the afternoon and evening.
    Version: The Plugin's Version Number, e.g.: 1.0
    Author: Peter Artmont
    Author URI: http://wwww.peterartmont.com
    License: THE SHISH SCHEDULE
    */
    
    add_action( 'admin_menu', 'my_admin_menu' );
    function my_admin_menu() {
     add_menu_page( '7 Day Schedule', '7 Day Schedule', 'manage_options', 'shish7dayplugin', 'schedule_page', '/THEBIGSHISH/blog/wp-content/plugins/BigShish7DaySchedule/Calendar-icon.png', '3' );
    }
    
    add_action( 'admin_init', 'my_admin_init' );
    function my_admin_init() {
        register_setting( '7dayschedule-group', '7dayschedule-settings' );
        add_settings_section( 'monday', 'Monday', 'monday_callback', 'shish7dayplugin' );
        add_settings_field( 'field-one', 'Monday Afternoon Time', 'field_one_callback', 'shish7dayplugin', 'monday' );
    	add_settings_field( 'field-two', 'Monday Afternoon Location', 'field_two_callback', 'shish7dayplugin', 'monday' );
    	add_settings_field( 'field-three', 'Monday Evening Time', 'field_three_callback', 'shish7dayplugin', 'monday' );
    	add_settings_field( 'field-four', 'Monday Evening Location', 'field_four_callback', 'shish7dayplugin', 'monday' );
    	add_settings_section( 'tuesday', 'Tuesday', 'tuesday_callback', 'shish7dayplugin' );
    	add_settings_field( 'tuesday_field-one', 'Tuesday Afternoon Time', 'tuesday_field_one_callback', 'shish7dayplugin', 'tuesday' );
    	add_settings_field( 'tuesday_field-two', 'Tuesday Afternoon Location', 'tuesday_field_two_callback', 'shish7dayplugin', 'tuesday' );
    	add_settings_field( 'tuesday_field-three', 'Tuesday Evening Time', 'tuesday_field_three_callback', 'shish7dayplugin', 'tuesday' );
    	add_settings_field( 'tuesday_field-four', 'Tuesday Evening Location', 'tuesday_field_four_callback', 'shish7dayplugin', 'tuesday' );
    	add_settings_section( 'wednesday', 'Wednesday', 'wednesday_callback', 'shish7dayplugin' );
    	add_settings_field( 'wednesday_field-one', 'Wednesday Afternoon Time', 'wednesday_field_one_callback', 'shish7dayplugin', 'wednesday' );
    	add_settings_field( 'wednesday_field-two', 'Wednesday Afternoon Location', 'wednesday_field_two_callback', 'shish7dayplugin', 'wednesday' );
    	add_settings_field( 'wednesday_field-three', 'Wednesday Evening Time', 'wednesday_field_three_callback', 'shish7dayplugin', 'wednesday' );
    	add_settings_field( 'wednesday_field-four', 'Wednesday Evening Location', 'wednesday_field_four_callback', 'shish7dayplugin', 'wednesday' );
    		add_settings_section( 'thursday', 'Thursday', 'wednesday_callback', 'shish7dayplugin' );
    	add_settings_field( 'thursday_field-one', 'Thursday Afternoon Time', 'thursday_field_one_callback', 'shish7dayplugin', 'thursday' );
    	add_settings_field( 'thursday_field-two', 'Thursday Afternoon Location', 'thursday_field_two_callback', 'shish7dayplugin', 'thursday' );
    	add_settings_field( 'thursday_field-three', 'Thursday Evening Time', 'thursday_field_three_callback', 'shish7dayplugin', 'thursday' );
    	add_settings_field( 'thursday_field-four', 'Thursday Evening Location', 'thursday_field_four_callback', 'shish7dayplugin', 'thursday' );
    	add_settings_section( 'friday', 'Friday', 'friday_callback', 'shish7dayplugin' );
    	add_settings_field( 'friday_field-one', 'Friday Afternoon Time', 'friday_field_one_callback', 'shish7dayplugin', 'friday' );
    	add_settings_field( 'friday_field-two', 'Friday Afternoon Location', 'friday_field_two_callback', 'shish7dayplugin', 'friday' );
    	add_settings_field( 'friday_field-three', 'Friday Evening Time', 'friday_field_three_callback', 'shish7dayplugin', 'friday' );
    	add_settings_field( 'friday_field-four', 'Friday Evening Location', 'friday_field_four_callback', 'shish7dayplugin', 'friday' );
    	add_settings_section( 'saturday', 'Saturday', 'saturday_callback', 'shish7dayplugin' );
    	add_settings_field( 'saturday_field-one', 'Saturday Afternoon Time', 'saturday_field_one_callback', 'shish7dayplugin', 'saturday' );
    	add_settings_field( 'saturday_field-two', 'Saturday Afternoon Location', 'saturday_field_two_callback', 'shish7dayplugin', 'saturday' );
    	add_settings_field( 'saturday_field-three', 'Saturday Evening Time', 'saturday_field_three_callback', 'shish7dayplugin', 'saturday' );
    	add_settings_field( 'saturday_field-four', 'Saturday Evening Location', 'saturday_field_four_callback', 'shish7dayplugin', 'saturday' );
    	add_settings_section( 'sunday', 'Sunday', 'sunday_callback', 'shish7dayplugin' );
    	add_settings_field( 'sunday_field-one', 'Sunday Afternoon Time', 'sunday_field_one_callback', 'shish7dayplugin', 'sunday' );
    	add_settings_field( 'sunday_field-two', 'Sunday Afternoon Location', 'sunday_field_two_callback', 'shish7dayplugin', 'sunday' );
    	add_settings_field( 'sunday_field-three', 'Sunday Evening Time', 'sunday_field_three_callback', 'shish7dayplugin', 'sunday' );
    	add_settings_field( 'sunday_field-four', 'Sunday Evening Location', 'sunday_field_four_callback', 'shish7dayplugin', 'sunday' );
    
    }
    
    function monday_callback() {
        echo 'This is where Your times for Monday go.';
    }
    
    function tuesday_callback() {
        echo 'This is where Your times for Tuesday go.';
    }
    function wednesday_callback() {
        echo 'This is where Your times for Wednesday go.';
    }
    
    function thursday_callback() {
        echo 'This is where Your times for Thursday go.';
    }
    
    function friday_callback() {
        echo 'This is where Your times for Thursday go.';
    }
    
    function saturday_callback() {
        echo 'This is where Your times for Thursday go.';
    }
    
    function sunday_callback() {
        echo 'This is where Your times for Thursday go.';
    }
    
    function field_one_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function field_two_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function field_three_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function field_four_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function tuesday_field_one_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function tuesday_field_two_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function tuesday_field_three_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function tuesday_field_four_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function wednesday_field_one_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function wednesday_field_two_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function wednesday_field_three_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function wednesday_field_four_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function thursday_field_one_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function thursday_field_two_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function thursday_field_three_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function thursday_field_four_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function friday_field_one_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function friday_field_two_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function friday_field_three_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function friday_field_four_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function saturday_field_one_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function saturday_field_two_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function saturday_field_three_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function saturday_field_four_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function sunday_field_one_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function sunday_field_two_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function sunday_field_three_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    
    function sunday_field_four_callback() {
        $setting = esc_attr( get_option( '7dayschedule-settings' ) );
        echo "<input type='text' name='my-setting' value='$setting' />";
    }
    function schedule_page() {
        ?>
        <div class="wrap">
            <h2>7 Day Schedule</h2>
            <form action="options.php" method="POST">
                <?php settings_fields( '7dayschedule-group' ); ?>
                <?php do_settings_sections( 'shish7dayplugin' ); ?>
                <?php submit_button('Save 7 Day Schedule','primary'); ?>
            </form>
        </div>
        <?php


    [Please not that backticks and single quotes are not the same thing]

    finally i was hoping to be able to access the information they enter into the settings fields and i would be able to input that into a custom css calendar page i make them.
    any pointers on how to go about this. I am very beginner to wordpress codex.

  2. Jo Biesta
    Member
    Posted 2 years ago #

    Have a look how other plugin authors generate admin pages, personally i'm not too familiar with the way you have done things, therefore i can't say what could be causing the settings to not save properly.

    I myself just have another php file for the settings page which is just like a form, it updates the options using update_option and gets the options using get_option.

    This may be a simpler way of doing it if you're not too familiar with the wordpress codex.

    You can add the admin page as follows ( this is a sample from one of my plugins)

    function callback_settings() {
    	include('callback_settings.php');
    }
    function callback_admin_actions() {
    	add_menu_page("Callback Request", "Callback Request", 1, "Callback Request", "callback_settings");
    }
    add_action('admin_menu', 'callback_admin_actions');

    As you can see this just includes the callback_settings page as an administration page.

    In terms of outputting the options on a page itself you can just use what you've put above using esc_attr( get_option('optionname'));.

    Hopefully that will help a bit!

    Jo

  3. partmont
    Member
    Posted 2 years ago #

    thank you for your prompt response Jo,

    So if i understand what you are saying is in your plugin folder you have an index.php

    with this function below?

    function callback_settings() {
    	include('callback_settings.php');
    }
    function callback_admin_actions() {
    	add_menu_page("Callback Request", "Callback Request", 1, "Callback Request", "callback_settings");
    }
    add_action('admin_menu', 'callback_admin_actions');

    then a seperate settings.php with the form inside of that php?

    so the index php call the settings the the menu page?

    then my next question is am i putting the get_option into the template page home.php?

    thanks for your patience.
    Peter

  4. partmont
    Member
    Posted 2 years ago #

    the home.php is where i want to display the form that was entered.

  5. Jo Biesta
    Member
    Posted 2 years ago #

    It's not an index.php file it's the main plugin file with all the header information, like you have but rather than creating all the functions just create a form.

    Yes you could either put get option where you want to show the options on the front end (what you're calling home.php) or your other option would be to create a shortcode that outputs a list of the next 5 locations or something?

    All depends how you want the plugin to work really.

    I may get a chance to test your code tonight in which case I might find a bug that's actually causing it not to save properly, but I'd have to test it to see.

    Let me know what you think and if you require further help!

  6. partmont
    Member
    Posted 2 years ago #

    so i messed around with the simple add plug in function u gave me above. then i createda callback_settings.php and pasted the form inside of their. upon pressing submit i am redirected to my sites settings page.. anythoughts?

    this is the basic form that i am using.

    <html>
    <body>
    
    <form action="option.php" method="post">
    Name: <input type="text" name="fname">
    Age: <input type="text" name="age">
    <input type="submit">
    </form>
    
    </body>
    </html>

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum's parser.]

    thanks
    peter

    and if you do get a chance to check out my last code and test it tonight i would be greatful. you have been such great help. thank you jo.

  7. Jo Biesta
    Member
    Posted 2 years ago #

    Hi Peter,

    I've had a look at your code, the simple answer as to why it doesn't save the settings from what I can see is that you're not updating the options anywhere you're just expecting them to update without writing to code to update them.

    If you're wanting to use all the code you've written so far look here;
    http://codex.wordpress.org/Creating_Options_Pages
    specifically at example #2 as this seems to be like the way you're writing the plugin.

    Jo

  8. Jo Biesta
    Member
    Posted 2 years ago #

    What i would do - this would allow me to assist you better would be the following:

    Please note i do it this way as this is the way that i know things.

    structure your plugin like this:
    -plugin_folder
    --plugin_functions.php
    --plugin_settings.php

    Lets look at a simple example of how to save options etc,
    make your plugin_functions.php file as follows:

    <?php
    /*
    Plugin Name: Sample Code
    Plugin URI: http://joeybdesign.co.uk
    Description: Test plugin to save options and get them from the wordpress database.
    Version: 1.0
    Author: Jo Biesta
    Author URI: http://joeybdesign.co.uk
    */
    function plugin_settings() {
    	include('plugin_settings.php');
    }
    function plugin_admin_actions() {
    	add_menu_page("Test Plugin", "Test Plugin", 1, "Test Plugin", "plugin_settings");
    }
    add_action('admin_menu', 'plugin_admin_actions');
    ?>

    That is all that needs to be in your plugin_functions.php file for this little demo.

    Then in your plugin_settings.php file put the following:

    <?php
    	if($_POST['settings_hidden'] == 'Y') {
    
    	$setting1 = $_POST['setting1'];
    	update_option('setting1',$setting1);
    ?>
    <div class="updated"><p><strong>Settings Saved</strong></p></div>
    <?php
    }
    else {
    	$setting1 = get_option('setting1','Default');
    	}
    ?>
    <div class="wrap">
    <h2>Settings</h2>
    <hr />
    <form name="settings_form" method="post" action="<?php echo str_replace( '%7e', '~', $_SERVER['REQUEST_URI']); ?>">
    	<input type="hidden" name="settings_hidden" value="Y">
    
    <p><input type="text" name="setting1" value="<?php echo $setting1; ?>" size="25"><br />
    
    	<input type="submit" name="Submit" class="button-primary" Value="Save Settings" />
    	</p>
    </form>
    </div>
    <hr />

    This should give you a simple settings page which allows you to save a setting to the options database for wordpress.

    Please note this may not be the best way to do it and it certainly isn't the only way to do it, this is simply a way that works for me..#

    Hopefully that will help.

    Jo

  9. partmont
    Member
    Posted 2 years ago #

    still getting the same issue i copy pasted just your code to set it up and build off, tested it in my admin when i press save it deletes the input in the text field. i switched themes back to 2012 and tried the plugin and still deletes the info i type in is there somewhere it is storing when it deletes it i thought the info should just stay in the field... thanks for your patience jo your my life line right now.

  10. Jo Biesta
    Member
    Posted 2 years ago #

    Sorry thats my fault I made a mistake in the code i wrote!

    I've edited my post please try again

  11. partmont
    Member
    Posted 2 years ago #

    jo your the man thank you, it worked!!

    now i can expand this into my own great form thank you so much!!

    so excited right now thank you.

    so now that i have a plugin that works and saves i need to retrieve the info for the home page

    how would i get the info from setting one into my home.php file?

    this is my final step to have the whole site backend coded.!! so excited thanks for all your help man.

  12. Jo Biesta
    Member
    Posted 2 years ago #

    Just use get_option, it's in the settings file already and used to get the input value.

    Jo

  13. partmont
    Member
    Posted 2 years ago #

    thanks jo this was awesome help.

  14. Jo Biesta
    Member
    Posted 2 years ago #

    No problem, I would have offered to write the plugin for you but I'm not sure what forum rules are in regards to selling services!

    Jo

  15. partmont
    Member
    Posted 2 years ago #

    all good i enjoy hacking away at this stuff.

  16. bcworkz
    Member
    Posted 2 years ago #

    I'm not sure what forum rules are in regards to selling services!

    Jo - You correctly sensed this is the wrong place for that. The acceptable place for offering or hiring services is WP Jobs. Anytime I suggest someone hire help here, I make it clear I am not suggesting they hire me.

    In any case, the help you have freely offered here is greatly appreciated.

  17. Mark / t31os
    Moderator
    Posted 2 years ago #

    The issue with your original code(which was more inline with correctly using the settings api) is that your inputs all have the same name, they should be named inline with the registered setting 7dayschedule-settings, then differ only in their key.

    Here's a trimmed version of your code with the appropriate changes(kept it short and unsanitized to make it easy to follow).
    http://pastebin.mozilla.org/2387231

    If you want to refine the input printing to make it easier to manage and not require a seperate function for each field(again kept it short and unsanitized to make it easy to follow), simply pass along some args and use a single callback function, like so..
    http://pastebin.mozilla.org/2387282

    Follow the method i've used in either of the above pastes and your code should operate just fine.

    Though it should be noted that register_setting should always be used with your own custom callback that iterates over the options and sanitizes them appropriately.

    Hope that helps.

Topic Closed

This topic has been closed to new replies.

About this Topic