WordPress.org

Forums

[resolved] Can't update settings with multiple settings sections (8 posts)

  1. Christiaan Conover
    Member
    Posted 1 year ago #

    I've got my plugin options set up to be in multiple sections, for ease of use and more efficient database calls. However, since reorganizing my Options page I can only seem so save changes made in the last section. No changes I make to earlier sections take effect.

    The code for the admin options page is here: https://github.com/cconover/wp-author-customization/blob/master/author-customization/admin/author-customization-admin.php

    Here's a screenshot of the Options page: https://github.com/cconover/wp-author-customization/blob/master/author-customization/assets/screenshot-2.png

    I'm sure the answer is staring me in the face, but I need some help looking in the right place.

  2. UaMV
    Member
    Posted 1 year ago #

    i'm pretty certain each call to add_settings_field() requires registering a new setting. I would suggest calling the function once and echoing both fields within the same callback. It might more properly be understood as adding a setting fields array.

  3. Christiaan Conover
    Member
    Posted 1 year ago #

    I've had multiple settings fields inside a single settings section without issue in other plugins. There shouldn't be a need to register a new setting for each field since I'm storing all the fields in a single setting inside an array.

  4. UaMV
    Member
    Posted 1 year ago #

    I agree, there is not a need to register a new setting for each field. However, I think you do need to register a new field each time the add_settings_field() function is called.

    In order to add multiple fields, I would suggest the following ...

    function cc_author_postpage_init() {
    	register_setting( 'cc_author_options_postpage', 'cc_author_postpage', 'cc_author_postpage_validate' ); // Register the settings group and specify validation and database locations
    
    	add_settings_section(
    		'postpage',  // section id
    		__( 'Post/Page Settings' ),  // section title
    		'cc_author_postpage_section_callback',  // callback
    		'cc-author'  // include on this page
    	);
    	add_settings_field(
    		'cc_author_postpage',  // array field id
    		__( 'Use author data from post' ),  // array field title
    		'cc_author_postpage_callback',  // callback
    		'cc-author',  // include on this page
    		'postpage'  // include in this section
    		);
    }
    
    function cc_author_postpage_section_callback() {
    	echo _e( '<p>These options are specific to posts and pages.</p>' );
    }
    
    /* Displaying fields that belong to 'cc_author_postpage' field */
    function cc_author_postpage_callback() {
    	$postpage = get_option( 'cc_author_postpage' );
    
    	echo '<label for="cc_author_postpage[perpost]">';
    		echo '<input id="perpost" name="cc_author_postpage[perpost]" type="checkbox" value="Post" ' . checked( $postpage['perpost'], 'Post' ) . '>';
    	echo ' Use author data from post</label>';
    	echo '<p class="description">If checked, the plugin will retrieve author information from the post metadata instead of the user database. Useful for keeping author information specific to the time a post was published.</p>';
    	echo '<p class="description"><strong>Note:</strong> You can toggle this at any time, as this plugin always saves author information to post metadata regardless of this setting.</p>';
    
    	echo '<label for="cc_author_postpage[relnofollow]">';
    		echo '<input id="relnofollow" name="cc_author_postpage[relnofollow]" type="checkbox" value="Nofollow" ' . checked( $postpage['Nofollow'] ) . '>';
    	echo ' Add rel="nofollow" to bio links</label>';
    	echo '<p class="description">Add a <a href="https://support.google.com/webmasters/answer/96569?hl=en">rel="nofollow"</a> attribute to any links in an author\'s biographical info when displayed. This prevents search engines from counting those links as part of your rank score.</p>';
    }

    You'll notice that both fields are displayed via the same callback, that which is connected with the registered cc_author_postpage field

  5. Christiaan Conover
    Member
    Posted 1 year ago #

    Thanks, I just tried that but it doesn't work. With the Settings API registering a settings field requires its own callback because it creates its own display entry. When I tried your suggestion I ended up with this: http://i.imgur.com/os1JTHm.png

    I'm still plugging away at it :-)

  6. UaMV
    Member
    Posted 1 year ago #

    Interesting. This is the method I have used in all my plugins and have not had such an issue. From the image, it appears that you may have called add_settings_field twice. It should only be called once. I realize in my code above, I probably should have given the array field title a more generic name that encompasses both of the fields you are including in the callback. Feel free to check out the admin pages and code from any of my plugins.

  7. Christiaan Conover
    Member
    Posted 1 year ago #

    Oh, I think I see what you're saying. I'll go back and try it again and let you know what I find.

  8. Christiaan Conover
    Member
    Posted 1 year ago #

    Ok, I've fixed it. I had different value for $option_group in each of my register_settings() calls, when I should have only been using one. I also only needed one settings_fields() call, again using the single $option_group value. Maybe this is what you were telling me and I just wasn't getting it :-)

    Either way, it's working now. Here's the working code:

    <?php
    /*
    Functions for managing the plugin inside wp-admin
    
    Included/Required by:
    author-customization.php
    */
    
    /**
     * Create entry in Settings menu
     * A submenu entry titled 'Custom Authors' is shown under Settings
     */
    function cc_author_create_menu() {
    	add_options_page(
    		'Author Customization',				// Page title. This is displayed in the browser title bar.
    		'Author Custom',					// Menu title. This is displayed in the Settings submenu.
    		'manage_options',					// Capability required to access the options page for this plugin
    		'cc-author',						// Menu slug
    		'cc_author_options_page'			// Function to render the options page
    	);
    } // cc_author_create_menu()
    add_action( 'admin_menu', 'cc_author_create_menu' ); // Hook menu entry into API
    /**
     * End Create entry in Settings menu
     */
    
    /**
     * Post/Page options configuration
     * Settings specific to posts and pages
     */
    add_action( 'admin_init', 'cc_author_postpage_init' ); // Hook admin initialization for plugin postpage
    
    function cc_author_postpage_init() {
    	register_setting( 'cc_author_options', 'cc_author_postpage', 'cc_author_postpage_validate' ); // Register the settings group and specify validation and database locations
    
    	add_settings_section(
    		'postpage',							// Name of the section
    		'Post/Page Settings',				// Title of the section, displayed on the options page
    		'cc_author_postpage_callback',		// Callback function for displaying information
    		'cc-author'							// Page ID for the options page
    	);
    
    	add_settings_field(						// Set whether author info is pulled from post meta or global user data
    		'perpost',							// Field ID
    		'Use author data from post',		// Field title, displayed to the left of the field on the options page
    		'cc_author_perpost_callback',		// Callback function to display the field
    		'cc-author',						// Page ID for the options page
    		'postpage'							// Settings section in which to display the field
    	);
    	add_settings_field(						// Set whether author info is pulled from post meta or global user data
    		'relnofollow',						// Field ID
    		'Add rel="nofollow" to bio links',	// Field title, displayed to the left of the field on the options page
    		'cc_author_relnofollow_callback',	// Callback function to display the field
    		'cc-author',						// Page ID for the options page
    		'postpage'							// Settings section in which to display the field
    	);
    } // cc_author_postpage_list()
    
    /* Settings section callback */
    function cc_author_postpage_callback() {
    	echo '<p>These options are specific to posts and pages.</p>';
    } // cc_author_postpage_callback()
    
    /* Callback for 'perpost' option */
    function cc_author_perpost_callback() {
    	$postpage = get_option( 'cc_author_postpage' ); // Retrieve plugin options from the database
    
    	/* Determine whether the box should be checked based on setting in database */
    	if ( $postpage['perpost'] ) {
    		$checked = 'checked';
    	}
    	else {
    		$checked = '';
    	}
    
    	echo '<input id="perpost" name="cc_author_postpage[perpost]" type="checkbox" value="Post" ' . $checked . '>'; // Print the input field to the screen
    	echo '<p class="description">If checked, the plugin will retrieve author information from the post metadata instead of the user database. Useful for keeping author information specific to the time a post was published.</p><p class="description"><strong>Note:</strong> You can toggle this at any time, as this plugin always saves author information to post metadata regardless of this setting.</p>'; // Description of option
    } // cc_author_perpost_callback()
    
    /* Callback for 'relnofollow' option */
    function cc_author_relnofollow_callback() {
    	$postpage = get_option( 'cc_author_postpage' ); // Retrieve plugin options from the database
    
    	/* Determine whether the box should be checked based on setting in database */
    	if ( $postpage['relnofollow'] ) {
    		$checked = 'checked';
    	}
    	else {
    		$checked = '';
    	}
    
    	echo '<input id="relnofollow" name="cc_author_postpage[relnofollow]" type="checkbox" value="Nofollow" ' . $checked . '>'; // Print the input field to the screen
    	echo '<p class="description">Add a <a href="https://support.google.com/webmasters/answer/96569?hl=en">rel="nofollow"</a> attribute to any links in an author\'s biographical info when displayed. This prevents search engines from counting those links as part of your rank score.</p>'; // Description of option
    } // cc_author_relnofollow_callback()
    
    /* Validate submitted options */
    function cc_author_postpage_validate( $input ) {
    	$postpage = get_option( 'cc_author_postpage' ); // Retrieve existing options values from the database
    
    	/* Directly set values that don't require validation */
    	$postpage['perpost']		=	$input['perpost'];
    	$postpage['relnofollow']	=	$input['relnofollow'];
    
    	return $postpage; // Send values to database
    } // cc_author_postpage_validate()
    /**
     * End Post/Page options configuration
     */
    
    /**
     * Admin Options
     * Options for things that happen inside WordPress admin
     */
    add_action( 'admin_init', 'cc_author_admin_options_init' ); // Hook admin initialization for plugin admin
    
    function cc_author_admin_options_init() {
    	register_setting( 'cc_author_options', 'cc_author_admin_options', 'cc_author_admin_options_validate' ); // Register the settings group and specify validation and database locations
    
    	add_settings_section(
    		'admin_options',					// Name of the section
    		'Admin Settings',					// Title of the section, displayed on the options page
    		'cc_author_admin_options_callback',	// Callback function for displaying information
    		'cc-author'							// Page ID for the options page
    	);
    
    	add_settings_field(						// Set whether author info is pulled from post meta or global user data
    		'wysiwyg',							// Field ID
    		'WYSIWYG editor for author bio',	// Field title, displayed to the left of the field on the options page
    		'cc_author_wysiwyg_callback',		// Callback function to display the field
    		'cc-author',						// Page ID for the options page
    		'admin_options'						// Settings section in which to display the field
    	);
    } // cc_author_admin_options_list()
    
    /* Settings section callback */
    function cc_author_admin_options_callback() {
    	echo '<p>These options are for things that happen inside WordPress admin.</p>';
    } // cc_author_admin_options_callback()
    
    /* Call back for 'wysiwyg' option */
    function cc_author_wysiwyg_callback() {
    	$admin_options = get_option( 'cc_author_admin_options' ); // Retrieve plugin options from the database
    
    	/* Determine whether the box should be checked based on setting in database */
    	if ( $admin_options['wysiwyg'] ) {
    		$checked = 'checked';
    	}
    	else {
    		$checked = '';
    	}
    
    	echo '<input id="wysiwyg" name="cc_author_admin_options[wysiwyg]" type="checkbox" value="WYSIWYG" ' . $checked . '>'; // Print the input field to the screen
    	echo '<p class="description">Enable a WYSIWYG editor for the author bio field, both in the user profile area and in the post/page meta box.</p>'; // Description of option
    } // cc_author_wysiwyg_callback()/* Call back fo
    
    /* Validate submitted options */
    function cc_author_admin_options_validate( $input ) {
    	$admin_options = get_option( 'cc_author_admin_options' ); // Retrieve existing options values from the database
    
    	/* Directly set values that don't require validation */
    	$admin_options['wysiwyg']		=	$input['wysiwyg'];
    
    	return $admin_options; // Send values to database
    } // cc_author_admin_options_validate()
    /**
     * End Admin Options
     */
    
    /**
     * Options Page
     */
    function cc_author_options_page() {
    	/* Prevent users with insufficient permissions from accessing settings */
    	if ( ! current_user_can( 'manage_options' ) ) {
    		wp_die( '<p>You do not have sufficient permissions to access this page.</p>' );
    	}
    	?>
    
    	<div class="wrap">
    		<?php screen_icon(); ?>
    		<h2>Author Customization</h2>
    
    		<form action="options.php" method="post">
    			<?php
    			settings_fields( 'cc_author_options' ); 			// Retrieve the fields created for plugin options
    			do_settings_sections( 'cc-author' ); 				// Display the section(s) for the options page
    			submit_button();									// Form submit button generated by WordPress
    			?>
    		</form>
    	</div>
    
    	<?php
    } // cc_author_options_page()
    /**
     * End Options Page
     */
    
    /* Include the functions for use while editing a post */
    require_once( dirname( __FILE__ ) . '/includes/edit-post.php' ); // Retrieve file containing edit post functions
    
    /* If editing user profile, include the functions for use while editing a user */
    if ( strstr( $_SERVER['REQUEST_URI'], 'wp-admin/profile.php' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/user-edit.php' ) ) {
    	require_once( dirname( __FILE__ ) . '/includes/edit-user.php' ); // Retrieve file containing edit user functions
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic