• Resolved Floris

    (@florismk)


    So I have this very simple plugin, which saves a single option using the standard options management functions WordPress offers.
    It worked. And while maintaining it, I broke it, only to discover I can’t figure out how it was supposed to work.

    The good news: Settings page loads and is populated as expected. I can select an option from the dropdown list and press Save.

    The bad news: When the Settings page reloads after save, the old value is restored.
    So apparently, I broke the bit that saves the option.

    But as I understand it, that’s the bit that happens behind the scenes, based on my correct code.
    So apparently, my code isn’t entirely correct, but I can’t figure out where.

    I think it all boils down to two things I don’t know/understand:

    1. How to the function signatures interact, i.e. what is the $page and the $section I pass to the standard functions?
    2. How do I name my form input correctly?

    But there may be more I’m not getting, or getting wrong.

    Here’s my code:

    add_action   ('admin_menu',          'fd_create_settings');
    add_action   ('admin_init',          'fd_register_settings' );
    
    function fd_create_settings() {
    	add_options_page( 'Vlag van de Dag'
    					, 'Vlag van de Dag'
    					, 'manage_options'
    					, 'fd_settings'
    					, 'fd_get_settings_page' );
    }
    function fd_get_settings_page() {
    	echo '<div class="wrap">';
    	echo '<h1>Vlag van de Dag-instellingen</h1>';
    	echo '<form method="post" action="options.php">';
    
    	settings_fields      ('fd_options');
    // Is this correct?                ^
    
    	do_settings_sections ('fd_options');
    // Is this correct?                ^
    
    	submit_button();
    	echo '</form>';
    	echo '</div>';
    }
    function fd_register_settings() {
    
    		register_setting    	('fd_options', 'fd_current_flag');
    // Is this correct?                           ^
    
    		add_settings_section	( 'fd_plugin_main_section'
    					, 'Instellingen'
    					, 'fd_get_text_section_main'
    					, 'fd_options');
    // Is this correct?                            ^
    
    		add_settings_field	( 'fd_current_flag'
    	 				, 'Huidige vlag'
    					, 'fd_get_flags_dropdown'
    					, 'fd_options'
    					, 'fd_plugin_main_section');
    // Is fd_options correct here?                 ^
    		}
    
    	function fd_get_text_section_main() {
    		echo '<p>Algemene instellingen Vlag van de Dag.</p>';
    		}
    
    	function fd_get_flags_dropdown() {
    
    		$flags   = fd_get_flags_array();
    		// $flags array is correctly filled with key/value pairs, I've verified
    		// So I won't bore you with that code
    
    		$options = get_option('fd_options');
    		// $ditto for options array
    
    		$current = $options['fd_current_flag'];
    		// $current variable fills correctly with the last saved value
    
    		echo "<select id='fd_current_flag' name='fd_current_flag'>";
    		// What I'm nut sure of is if this is correct  ^
    		// I thought the name should refer to the option name?
    
    		// Filling the dropdown; the logic to determine the selected option
    		// works perfectly, $current gets selected
    		$option = '<option value="geen"';
    		if($current=='geen') {
    			$option .= ' selected';			
    			}
    		$option .= '>Geen</option>';
    		echo $option;
    		foreach($flags as $key => $flag):
    			$option  = "<option value='$key'";
    			if($key==$current) {
    				$option .= ' selected';			
    				}
    			$option .= ">$flag</option>";
    			echo $option;
    		endforeach;
    		echo '</select>';
    		}

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    The options page you are adding is “fd_settings”, so anywhere a WP settings function wants a page name, it should be “fd_settings”, not “fd_options” as you have it now. “fd_options” might be a valid setting or section name as long as it’s used consistently. I don’t think it’s a good name for a setting since it’s plural. It could be a reasonable section name?

    Review the doc page for the Settings API and try to coordinate your code likewise. I don’t have time right now to look at your code in more detail, but hopefully this sets you off in the right direction. I know this is rather confusing, it took me a while to get it right when I first tried using the API. If you still have trouble let us know, I may have time later to help further if need be. Good luck!

    Thread Starter Floris

    (@florismk)

    @bcworkz, that was it! You made sense of page/section/option, and following that logic, I was able to replace the applicable names in the applicable places. I was put on the wrong foot by the docs on add_options_page, which call the fourth argument ‘menu slug’.

    This had me wondering what on Earth the page should be that the docs refer to. Your comment clarified that the menu slug in add_options_page is also the name for the page, to be used in the other function calls.

    It’s now also clear to me what the function is of the option_group. I was retrieving the entire option group with get_option(), which yielded an array of option name-value pairs that I had to extract my option from. I take it that’s the purpose of the option group. For my purpose, I can access the specific option directly with get_option(). Making the option group specified in register_setting irrelevant (though required).

    Thanks!

    Moderator bcworkz

    (@bcworkz)

    I believe the add_options_page() feature was developed independent from the Settings API, thus the inconsistency in terminology. While better consistency could have been established, WP core devs probably have more pressing priorities. Not a good excuse for sure. There could be more legitimate reasons I’m unaware of.

    I’ve always thought of the options groups as just a way to organize settings on a page. It never occurred to me that we could get all options in a group at once by name. I’ve always retrieved options one at a time. One at a time would be the most common need, but I can imagine a scenario where getting an entire group in one go could at times be useful. I might never have the need, but I’m glad you’ve inadvertently discovered that it’s possible ??

    Thread Starter Floris

    (@florismk)

    A success story all around! ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘I broke my plugin Options page, please help me fix it’ is closed to new replies.