The MU forums have moved to WordPress.org

Plugin settings not saved (14 posts)

  1. nicktank
    Member
    Posted 14 years ago #

    I am working on editing the Geotag Plugin to make it compatible with WPMU. I have registered the settings and adding the settings_fields function but everytime I click "Save Settings" I get a message that says "Settings Saved" and all of the fields have gone back to default. Basically, it is not actually saving the settings. This is a large php file so posting the whole thing is a little bit of an overkill. Let me know what it is you need to see to help out.

    Thanks.

  2. nicktank
    Member
    Posted 14 years ago #

    To add a little more information it looks like this plugin isn't creating any entries in the DB

  3. SteveAtty
    Member
    Posted 14 years ago #

    So there is no update_option('Option Name',option_array); call?

  4. nicktank
    Member
    Posted 14 years ago #

    There is not. I'm going to paste a little of the code in. It's all handled in one file so I'm leaving out a lot of the actual plugin functionality code

    <?php
    /*
    Plugin Name: Geotag
    Plugin URI: http://www.bobsp.de/weblog/geotag/
    Description: Provides geocoding features for Wordpress.
    Version: 1.0
    Author: Boris Pulyer
    Author URI: http://www.bobsp.de
    Minimum WordPress Version Required: 2.7.0
    Tested up to: 2.7.1
    */
    
    /* ==================================================================== */
    /* = Hooks, Filters, Globals etc.                                     = */
    /* ==================================================================== */
    
    global $geotag_maps, $geotag_options;
    $geotag_options = get_option("geotag_options");
    
    		function geotag_admin_init(){
    		register_setting( 'my-options-group', 'gmap_api_key', '' );
    		register_setting( 'my-options-group', 'gmap_dissplay_page', 'true' );
    		register_setting( 'my-options-group', 'auto_map', 'BOTTOM' );
    		register_setting( 'my-options-group', 'gmap_type', 'G_HYBRID_MAP' );
    		register_setting( 'my-options-group', 'gmap_controls_zoompan', '' );
    		register_setting( 'my-options-group', 'gmap_controls_maptype', '' );
    		register_setting( 'my-options-group', 'gmap_zoom', '' );
    		register_setting( 'my-options-group', 'gmap_width', '' );
    		register_setting( 'my-options-group', 'gmap_height', '' );
    		register_setting( 'my-options-group', 'geotaged_photos', '' );
    		register_setting( 'my-options-group', 'misc', '' );
    		register_setting( 'my-options-group', 'misc_wpgeocompatibility', '' );
    	}
    add_action( 'admin_init', 'geotag_admin_init' );
    
    register_activation_hook(__FILE__, array("Geotag", "registerPlugin"));
    add_action("admin_menu", array("Geotag", "hookAdminMenu"));
    add_action("admin_footer", array("Geotag", "hookAdminFooter"));
    add_action("save_post", array("Geotag", "hookSavePost"));
    add_action("wp_head", array("Geotag", "hookWPHeader"));
    add_action("wp_footer", array("Geotag", "hookWPFooter"));
    add_filter("the_content", array("Geotag", "filterTheContent"));
    add_shortcode("gmap", array("Geotag", "parseShortcode"));
    if ($geotag_options["misc_wpgeocompatibility"]["SHORTCODE"] == "true") {add_shortcode("wp_geo_map", array("Geotag", "parseShortcode"));};
    if ($geotag_options["misc"]["GEOTAG_FEEDS"] == "true") {
    	add_action("rss2_ns", array("Geotag", "hookFeedNamespace"));
    	add_action("atom_ns", array("Geotag", "hookFeedNamespace"));
    	add_action("rdf_ns", array("Geotag", "hookFeedNamespace"));
    	add_action("rss_item", array("Geotag", "hookFeedItem"));
    	add_action("rss2_item", array("Geotag", "hookFeedItem"));
    	add_action("atom_entry", array("Geotag", "hookFeedItem"));
    	add_action("rdf_item", array("Geotag", "hookFeedItem"));
    }
    
    /* ==================================================================== */
    /* = Class Geotag                                                     = */
    /* ==================================================================== */
    
    class Geotag {
    
    	/* ==================================================================== */
    	/* = Register the plugin                                              = */
    	/* ==================================================================== */
    
    	function registerPlugin() {
    		$options = array(
    			"gmap_api_key" => "",
    			"gmap_display_page" => array("SINGLE" => "true", "PAGE" => "true"),
    			"auto_map" => array("SHOW" => null, "POSITION" => "BOTTOM"),
    			"gmap_type" => "G_HYBRID_MAP",
    			"gmap_controls_zoompan" => "GLargeMapControl3D",
    			"gmap_controls_maptype" => array("G_NORMAL_MAP" => "true", "G_SATELLITE_MAP" => "true", "G_HYBRID_MAP" => "true", "G_PHYSICAL_MAP" => "true"),
    			"gmap_controls_other" => null,
    			"gmap_zoom" => "5",
    			"gmap_width" => "100%",
    			"gmap_height" => "300px",
    			"geotaged_photos" => array("READ_GEOTAGED_PHOTOS" => null, "ICON" => "CAMERA"),
    			"misc" => array("GEOTAG_FEEDS" => "true", "GEOTAG_HTML" => "true", "QUICKGUIDE" => "true"),
    			"misc_wpgeocompatibility" => array("DB" => "READ", "SHORTCODE" => "true")
    		);
    		add_option("geotag_options", $options);
    	}

    and here is the form in the options screen

    function displayOptions() {
    		global $geotag_options;
    		echo "
    			<div class='wrap'>
    				<h2>Geotag Configuration</h2>
    				<h3>Documentation</h3>
    				<p>If you want to learn more about how to use this plugin, take a look at the <a href='?page=geotag/documentation.php'>documentation</a>. You may also check out the <a href='http://www.bobsp.de/weblog/geotag'><em>Geotag</em> website</a>.</p>
    				<h3 style='margin-top: 3em;'>General Options</h3>
    				<form method='post' action='options.php'>";
    				settings_fields('my-options-group');
    		echo "
    				<table class='form-table'>
    					<tr valign='top'>
    						<th scope='row'>Google API Key</th>
    						<td><input name='geotag_options[gmap_api_key]' type='text' value='".$geotag_options["gmap_api_key"]."' size='50' ";
    		if (empty($geotag_options["gmap_api_key"])) {echo "style='border: 1px solid #d54e21; background-color: #ffebe8;'";}
    		echo " /><br />
    							You need a unique Google API Key for your website to display Google Maps. If you don't have one yet, <a href='http://code.google.com/apis/maps/signup.html' target='_blank'>sign up</a> to get one!</td>
    					</tr>
    					<tr valign='top'>
    						<th scope='row'>Show Map</th>
    						<td>Show maps only on this kind of pages:<br />".Geotag::displayOptions_Checkbox("geotag_options[gmap_display_page]", array("HOME" => "Home", "SINGLE" => "Single posts", "PAGE" => "Pages", "DATE" => "Date archives", "CATEGORY" => "Category Archives"), $geotag_options["gmap_display_page"])."</td>
    					</tr>
    					<tr valign='top'>
    						<th scope='row'>Auto Map</th>
    						<td>".Geotag::displayOptions_Checkbox("geotag_options[auto_map]", array("SHOW" => "Automatically show a map..."), $geotag_options["auto_map"])."
    							".Geotag::displayOptions_Select("geotag_options[auto_map][POSITION]", array("TOP" => "at the top of every post", "BOTTOM" => "at the bottom of every post"), $geotag_options["auto_map"]["POSITION"])."</td>
    					</tr>
    					<tr valign='top'>
    						<th scope='row'>Add Geotags</th>
    						<td>".Geotag::displayOptions_Checkbox("geotag_options[misc]", array("GEOTAG_FEEDS" => "Add geographical information to feeds"), $geotag_options["misc"])."
    							".Geotag::displayOptions_Checkbox("geotag_options[misc]", array("GEOTAG_HTML" => "Add geographical information to HTML Header"), $geotag_options["misc"])."</td>
    					</tr>
    				</table>
    				<h3 style='margin-top: 3em;'>Default Map Appearance</h3>
    				<p>Most of these settings can be overwritten in every post. See the <a href='?page=geotag/documentation.php'>documentation</a> for details.</p>
    				<table class='form-table'>
    					<tr valign='top'>
    						<th scope='row'>Map Type</th>
    						<td>".Geotag::displayOptions_Select("geotag_options[gmap_type]", array("G_NORMAL_MAP" => "Normal", "G_SATELLITE_MAP" => "Satellite", "G_HYBRID_MAP" => "Hybrid", "G_PHYSICAL_MAP" => "Physical (terrain information)", "G_STATIC_MAP" => "Static map (no gadgets but fast)"), $geotag_options["gmap_type"])."</td>
    					</tr>
    					<tr valign='top'>
    						<th scope='row'>Map Controls</th>
    						<td><strong>Zoom/Pan Controls</strong><br />".Geotag::displayOptions_Select("geotag_options[gmap_controls_zoompan]", array("GLargeMapControl3D" => "Large zoom/pan controls (new style)", "GLargeMapControl" => "Large zoom/pan controls", "GSmallMapControl" => "Small zoom/pan controls", "GSmallZoomControl3D " => "Small zoom control (new style)", "GSmallZoomControl" => "Small zoom control", "" => "No controls"), $geotag_options["gmap_controls_zoompan"])."<br />&nbsp;<br />
    							<strong>Map Type Controls</strong><br />".Geotag::displayOptions_Checkbox("geotag_options[gmap_controls_maptype]", array("G_NORMAL_MAP" => "Normal", "G_SATELLITE_MAP" => "Satellite", "G_HYBRID_MAP" => "Hybrid", "G_PHYSICAL_MAP" => "Physical (terrain information)"), $geotag_options["gmap_controls_maptype"])."<br />
    							<strong>Other Map Controls</strong><br />".Geotag::displayOptions_Checkbox("geotag_options[gmap_controls_other]", array("GScaleControl" => "Show map scale", "GOverviewMapControl" => "Show small overview map"), $geotag_options["gmap_controls_other"])."</td>
    					</tr>
    					<tr valign='top'>
    						<th scope='row'>Map Zoom</th>
    						<td>".Geotag::displayOptions_Select("geotag_options[gmap_zoom]", array("0" => "0 - Zoomed out", "1" => "1", "2" => "2", "3" => "3", "4" => "4", "5" => "5", "6" => "6", "7" => "7", "8" => "8", "9" => "9", "10" => "10", "11" => "11", "12" => "12", "13" => "13", "14" => "14", "15" => "15", "16" => "16", "17" => "17", "18" => "18", "19" => "19 - Zoomed In"), $geotag_options["gmap_zoom"])."</td>
    					</tr>
    					<tr valign='top'>
    						<th scope='row'>Map Width</th>
    						<td><input name='geotag_options[gmap_width]' type='text' value='".$geotag_options["gmap_width"]."' size='10' /> Please add <em>%</em> or <em>px</em></td>
    					</tr>
    					<tr valign='top'>
    						<th scope='row'>Map Height</th>
    						<td><input name='geotag_options[gmap_height]' type='text' value='".$geotag_options["gmap_height"]."' size='10' /> Please add <em>%</em> or <em>px</em></td>
    					</tr>
    					<tr valign='top'>
    						<th scope='row'>Geotaged Photos</th>
    						<td>".Geotag::displayOptions_Checkbox("geotag_options[geotaged_photos]", array("READ_GEOTAGED_PHOTOS" => "Try to read the geotags from every photo of the post and display an icon on the maps"), $geotag_options["geotaged_photos"])."
    							Icon style: ".Geotag::displayOptions_Select("geotag_options[geotaged_photos][ICON]", array("DEFAULT" => "Standard icon", "CAMERA" => "Camera icon", "THUMBNAIL" => "Thumbnail of the photos"), $geotag_options["geotaged_photos"]["ICON"])."</td>
    					</tr>
    				</table>
    				<h3 style='margin-top: 3em;'>Miscellaneous</h3>
    				<table class='form-table'>
    					<tr valign='top'>
    						<th scope='row'><em>WP Geo</em> Compatibility</th>
    						<td><strong>Database</strong><br />".Geotag::displayOptions_Radio("geotag_options[misc_wpgeocompatibility][DB]", array("NULL" => "No compatibility - <em>WP Geo</em> coordinates will be ignored", "READ" => "Read compatibility - read the <em>WP Geo</em> coordinates only if no <em>Geotag</em> coordinates were saved", "WRITE" => "Read and write compatibility - read the <em>WP Geo</em> coordinates and save new coordinates in the <em>WP Geo</em> database field"), $geotag_options["misc_wpgeocompatibility"]["DB"])."<br />
    							<strong>Shortcode</strong><br />".Geotag::displayOptions_Checkbox("geotag_options[misc_wpgeocompatibility]", array("SHORTCODE" => "Process the <em>WP Geo</em> Shortcode <code>[wp_geo_map]</code> in posts"), $geotag_options["misc_wpgeocompatibility"])."</td>
    					</tr>
    					<tr valign='top'>
    						<th scope='row'>Quick Guide</th>
    						<td>".Geotag::displayOptions_Checkbox("geotag_options[misc]", array("QUICKGUIDE" => "Add a short documentation to the post writing page."), $geotag_options["misc"])."</td>
    					</tr>
    				</table>
    				<p class='submit'>
    					<input type='submit' name='submit' value='Save Changes' class='button-primary' />
    					<input type='hidden' name='action' value='update' />
    					<input type='hidden' name='page_options' value='geotag_options' />
    				</p>
    				</form>
    			</div>";
    			//echo "<pre>"; print_r($geotag_options); echo "</pre>";
    	}
  5. SteveAtty
    Member
    Posted 14 years ago #

    The form posts to options.php and uses that to save its settings.

    This is how I did it in my plugin:

    Set up defaults:

    // Get the current settings or setup some defaults if needed
    if (!$current_settings = get_option('lastfm_options')){
    	$lastfm_options['lastfm_user'] = 'YourNameHere';
    	$lastfm_options['lastfm_colour'] = 'grey';
    	$lastfm_options['lastfm_size'] = '405';
    	$lastfm_options['lastfm_artist'] = 'Artist Name Here';
    	$lastfm_options['lastfm_genre'] = 'Genre Here';
    	$lastfm_options['lastfm_friends'] = 'No';
    	update_option('lastfm_options', $lastfm_options);
    }

    Form definition starts :

    <form method="post" action="options-general.php?page=lastfm.php">
    			<input type="hidden" name="action" value="save_options" />
    			<fieldset class="options">

    Then right at the bottom I have:

    add_action('widgets_init', 'widget_lastfm_init');
    add_action('admin_menu', 'lastfm_add_options'); 		// Insert the Admin panel.
    
    if ($_POST['action'] == 'save_options'){
    	lastfm_save_options();
    }
    ?>

    with lastm_save_options() defined as:

    function lastfm_save_options() {
    	$lastfm_options['lastfm_user'] = $_POST['lastfm_user'];
    	$lastfm_options['lastfm_colour'] = $_POST['lastfm_colour'];
    	$lastfm_options['lastfm_size'] = $_POST['lastfm_size'];
    	$lastfm_options['lastfm_artist'] = $_POST['lastfm_artist'];
    	$lastfm_options['lastfm_genre'] = $_POST['lastfm_genre'];
    	$lastfm_options['lastfm_friends'] = $_POST['lastfm_friends'];
    	update_option('lastfm_options', $lastfm_options);
    }

    I basically based that on someone else's code and it works for WP and WPMU. Its probably "wrong" or inefficient but it works.

    BTW - if you go to http://bobsp.de/weblog/geotag/

    there is a comment:

    "Hi,
    as reported by holyolli, running the Geotag plugin with Wordpress MU will produce some trouble. I tried to find out what went wrong and noticed, that only the procedure which saves the options is affected. Although I stick to the Wordpress Codex for creating options pages, you will always receive a failure notice if you try to save the options. To be honest, right now I don’t now how I can improve the plugin to avoid this error, but I compared the Worpdress MU with the standard version and found the difference which causes the trouble.
    Take a look at the /wp-admin/options.php file and search for the following code:

    case ‘update’:
    if ( isset($_POST[ 'option_page' ]) ) {
    $option_page = $_POST[ 'option_page' ];
    check_admin_referer( $option_page . ‘-options’ );
    }

    If you change this to

    case ‘update’:
    if ( isset($_POST[ 'option_page' ]) ) {
    $option_page = $_POST[ 'option_page' ];
    check_admin_referer( $option_page . ‘-options’ );
    } else {
    // This is for back compat and will eventually be removed.
    $option_page = ‘options’;
    check_admin_referer( ‘update-options’ );
    }!

  6. nicktank
    Member
    Posted 14 years ago #

    I'll give it a go and see what happens. Thanks

  7. nicktank
    Member
    Posted 14 years ago #

    I tried to run that hack with no results so I'm working on getting this thing to operate natively with MU. I think there is enough of a demand that this would be a welcome patch

  8. SteveAtty
    Member
    Posted 14 years ago #

    Well it looks quite interesting

  9. nicktank
    Member
    Posted 14 years ago #

    I think there is some conflicting logic going on. I implemented the things you had and am now getting a blank page when update. Back to the drawing board.

  10. SteveAtty
    Member
    Posted 14 years ago #

    blank page... hmm whats in the error logs ;-)

  11. nicktank
    Member
    Posted 14 years ago #

    Don't know yet. Just sent down to the network team to check. I've gotten it to write the settings tables to options.php but am unable to save to those tables right now. Basically, I have all of the settings in the table but they remain unupdated.

  12. nicktank
    Member
    Posted 14 years ago #

    ok, here are the php logs

    "script '/www/blog/current/wp-admin/general-options.php' not found or unable to stat, referer: http://blog.newsok.com/dustyglobe/wp-admin/options-general.php?page=geotag%2Fgeotag.php&updated=true

    That's all that was going on when the blank page showed.

  13. nicktank
    Member
    Posted 14 years ago #

    More news. It looks like the the options entries exist for the blog the plugin is activated on. But I still can't update the fields via the options page for the plugin.

  14. bobs1982
    Member
    Posted 14 years ago #

    Hey nicktank,

    basically it is possible to save the options "manually" like SteveAtty described. However, I tried to go the way described in the Wordpress Codex. It seemed to me more elegant as all the handling is done by Wordpress. Unfortunately this solution does not work with Wordpress MU and I am not sure why. You already discovered the workaround I posted on my weblog which worked on my testing system.

    Perhaps it is possible to bring this question to one of the developers. Otherwise I already considered to do the saving of the options manually. This is less elegant but at least it should work with all Worpress versions. Anyway, if you found any other solution I would be glad to hear about it so that I can integrate it in the next version.

    Thanks for your efforts so far!

    Cheers

    Boris

About this Topic

  • Started 14 years ago by nicktank
  • Latest reply from bobs1982