WordPress.org

Ready to get started?Download WordPress

Forums

How to display the 'settings updated' message (4 posts)

  1. David Gard
    Member
    Posted 3 years ago #

    Hey all,

    I'm trying to get the message that tells a user that some settings have been saved to appear at the top of the screen in a plugin, but I'm having a bit of trouble.

    I have the correct code and have placed it in a function -

    function write_custom_admin_message($message = null, $class = 'updated'){
    	if($message === null)
    		return;
    	echo '<div id="message0" class="'.$class.' fade">';
    	echo '<p><strong>'.$message.'</strong></p>';
    	echo '</div>';
    }

    but the problem is that I am having trouble getting my message to the function.

    I am using tha action add_action('admin_post_save_services_options', array(&$this, 'on_save_changes')); to call the function on_save_changes() when the user clicks the 'Save changes' button, and I can do all my validation etc in there, but then I don't know of a way of passing the values for the message box to the main plugin function.

    function on_save_changes(){
    	if(!current_user_can('manage_options'))
    		wp_die( __('Cheatin’ uh?'));
    	check_admin_referer('my-admin-referrer');
    
    	if(isset($_POST['action']) === true){
    		$options_name['title'] = $_POST['title'];
    		$options_name['text'] = $_POST['text'];
    		$options_name['image'] = $_POST['image'];
    
    		update_option('options_name', $options_name);
    
    		$message = __('Settings saved.');
    		$class = 'updated';
    	}
    	wp_redirect($_POST['_wp_http_referer']);
    }

    I hope all that makes sense, and I'd be very greatful if someone could help.

    Thanks.

  2. jonradio
    Member
    Posted 3 years ago #

    Maybe wait a little longer to see if you get a reply, but I would recommend re-posting this question in:
    http://wordpress.org/support/forum/hacks

  3. David Gard
    Member
    Posted 3 years ago #

    Ok, sorted it in a pretty simple way I think.

    The on_save_changes() function becomes this -

    function on_save_changes(){
    	if(!current_user_can('manage_options'))
    		wp_die( __('Cheatin’ uh?'));
    	check_admin_referer('my-admin-referrer');
    
    	if(isset($_POST['action']) === true){
    		$options_name['title'] = $_POST['title'];
    		$options_name['text'] = $_POST['text'];
    		$options_name['image'] = $_POST['image'];
    
    		update_option('options_name', $options_name);
    
    		$message = 0
    }
    $_POST['_wp_http_referer'] = add_query_arg('message', $message_num, $_POST['_wp_http_referer']);
    	wp_redirect($_POST['_wp_http_referer']);
    }

    I add this to functions.php -

    function custom_plugin_messages(){
    	$messages['example-plugin'] = array(
    		0 => __('Settings saved.')
    	);
    	return $messages;
    }

    and finally pop this in just below the Page title in the on_show_page() function -

    <?php
    $messages = custom_plugin_messages();
    write_custom_admin_message($messages[$_GET['page']][$_GET['message']], $class);
    ?>
  4. amereservant
    Member
    Posted 3 years ago #

    In my settings page HTML for my plugin or theme, I just add the following line of code where I want the message to appear:

    <?php if( isset($_GET['settings-updated']) ) { ?>
        <div id="message" class="updated">
            <p><strong><?php _e('Settings saved.') ?></strong></p>
        </div>
    <?php } ?>

    If you want to display an error message, check the value of $_GET['settings-updated'] to see if it's true or false and if it's false, set the class to 'error' instead of 'updated' on the div.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.