String length issue

Hi there,

I am using a form to save some data to the database like this:


function update_settings() {
	$updated = false;
	
	if($_REQUEST['address']) {
		update_option('address', $_REQUEST['address']);
		$updated = true;
	}
	
	if($_REQUEST['number']) {
		update_option('number', $_REQUEST['number']);
		$updated = true;
	}
	
	if($_REQUEST['site_email']) {
		update_option('site_email', $_REQUEST['site_email']);
		$updated = true;
	}
	
	if($_REQUEST['gmap_html']) {
		update_option('gmap_html', $_REQUEST['gmap_html']);
		$updated = true;
	}
	
	if($updated) { ?>
    	<div id="message">
        	<p>Details Updated</p>
      	</div>
    <?php }
	
	else { ?>
    	<div id="message">
        	<p>Failed to update details</p>
       	</div>
    <?php }
	
}

function options_form(){
	$saved_address = get_option('address');
	$saved_number = get_option('number');
	$saved_email = get_option('site_email');
	$saved_gmap = get_option('gmap_html'); ?>
    <table id="theme_settings_table">
	<form method="post">
    	<tr>
        <td><label for="address">Address: </label></td>
        <td><input size="80" type="text" name="address" value="<?=$saved_address?>"></td>
        </tr>
        <tr>
        <td><label for="number">Number: </label></td>
        <td><input size="80" type="text" name="number" value="<?=$saved_number?>"></td>
        </tr>
        <tr>
        <td><label for="site_email">Email: </label></td>
        <td><input size="80" type="text" name="site_email" value="<?=$saved_email?>"></td>
        </tr>
        <tr>
        <td><label for="gmap_html">HTML for map: </label></td>
        <td><textarea name="gmap_html" cols="51" rows="8" value="<?=$saved_gmap?>"></textarea></td>
        </tr>
        <tr>
        <td><input type="submit" name="submit" class="button-primary" value="Save"></td>
        </tr>
 	</form>
    </table>
<?php
}
?>

It all works great, except the textarea string “gmap_html” which is designed to hold the embed html for a google map. When saved, it cuts about half the information off the start of the string.

I’m wondering if there’s a maximum length string that WP will allow.

If this is the case, then presumably i need to preform some string functions to split it up and then re-connect it.

Any ideas would be greatly appreciated as always.

Mike

check your fieldtype in the db for the gmap_html

Ah ha, wonderful!

I imagine that the default setting for a string is CHAR, which has a maximum length of 255 characters.

Cool, i’m on the right tracks, but how do I directly edit the database? I’m guessing its MySQL right?

Cheers,
Mike

No, it’s longtext which is long enough.

What is happening, probably, is that your code has quotes in it which breaks the html. Try this instead:

$saved_gmap = htmlspecialchars(get_option('gmap_html'));

Ok, Saul, I’ll give it a whirl. Many thanks for your help :slight_smile:

Hey Saul, I know it’s been a while but I have tried your suggestion and it worked. I also found that wordpress has its own function for this as well wp_specialchars($string[, $quotes]); which does essentially the same job.

One other thing has reared its head now though. In my settings plug-in, the saved data is displayed so you can see what values are currently held in the db. For some reason, the gmap_html textarea is empty, even though on the source code it’s value contains the saved string.

<textarea name="gmap_html" cols="51" rows="15" value="<iframe width=\\&quot;425\\&quot; height=\\&quot;350\\&quot; frameborder=\\&quot;0\\&quot; scrolling=\\&quot;no\\&quot; marginheight=\\&quot;0\\&quot; marginwidth=\\&quot;0\\&quot; src=\\&quot;http://maps.google.co.uk/maps/ms?ie=UTF8&amp;hl=en&amp;source=embed&amp;msa=0&amp;msid=101615075176202398228.000494dd81c530b88a64b&amp;ll=51.476679,-0.146942&amp;spn=0.037421,0.072956&amp;z=13&amp;output=embed\\&quot;></iframe><br /><small>View <a href=\\&quot;http://maps.google.co.uk/maps/ms?ie=UTF8&amp;hl=en&amp;source=embed&amp;msa=0&amp;msid=101615075176202398228.000494dd81c530b88a64b&amp;ll=51.476679,-0.146942&amp;spn=0.037421,0.072956&amp;z=13\\&quot; style=\\&quot;color:#0000FF;text-align:left\\&quot;>Masons Arms</a> in Google Maps</small>" style="margin-left: 1px; margin-right: 1px; width: 431px; margin-top: 1px; margin-bottom: 1px; height: 278px; "></textarea>

That’s taken directly from Chrome.

I have tried changing the size of the textarea by no joy. Any ideas what’s happening here?

Many thanks,
Mike

Ok so I worked it out… obviously <textarea> works a little differently than <input type /> So I change the source code at little:


<textarea name="gmap_html" cols="51" rows="15" value="<?=$saved_gmap?>"><?=$saved_gmap?></textarea>

which does the job.

However, the string is now saved with escape slashes in front of every quote mark. I have disabled magic_quotes_gpc in my php.ini file, and I have tried htmlspecialchars, and wp_specialchars($string), ‘1’); to convert " in " but not joy.

What am I missing?

You can use stripslashes()

Yeah, thanks Saul, I got there. All working now. Thanks for your help.