Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom widget stripslashes issue WordPress

  • SOLVED

Hello,
I have a custom widget below. It's a twitter widget that collects a title, username, and tweet count. Its saving the data properly, but not displaying it back properly in the widget itself. See the attached image. I typed "I'm on Twitter" and it just returned "I\" in the widget.



//Twitter Widget
wp_register_sidebar_widget('twitter','CoPilot Twitter Widget','show_twitter');
register_widget_control('twitter','twitter_control');
function twitter_control(){
echo "Title: <input type='text' name='twitter-title' value='" .get_option('twitter_title'). "'/><br/></br>";
if(isset($_POST['twitter-title'])) update_option('twitter_title',$_POST['twitter-title']);

echo "Username: <input type='text' name='twitter-name' value='" .get_option('twitter_name'). "'/><br/></br>";
if(isset($_POST['twitter-name'])) update_option('twitter_name',$_POST['twitter-name']);

echo "# of Tweets: <input type='text' name='twitter-count' value='" .get_option('twitter_count'). "'/>";
if(isset($_POST['twitter-count'])) update_option('twitter_count',$_POST['twitter-count']);
}
function show_twitter(){ ?>
<div class="widget">
<h2><?php echo stripslashes(get_option('twitter_title')) ?></h2>
<?php $twittername = get_option('twitter_name');?>
<?php $twittercount = get_option('twitter_count');?>
<?php wp_echoTwitter($twittername); ?>
</div><!--widget-->

Answers (3)

2010-12-13

Maor Barazany answers:

Try instead of this line -

<h2><?php echo stripslashes(get_option('twitter_title')) ?></h2>

have this one:

<h2><?php echo esc_attr(get_option('twitter_title')) ?></h2>


Mike McAlister comments:

The slashes aren't a problem when it displays them on the frontend. The problem is it's adding slashes and cutting off the text in the widget window. The image attached is associated with the twitter_control function.


Maor Barazany comments:

How does the string appear in the db ?


Maor Barazany comments:

Or try this one :


<h2><?php echo htmlspecialchars(stripslashes(get_option('twitter_title'))); ?></h2>

2010-12-13

idt answers:

try adding this filter to your functions.php:

add_filter('option_twitter_title', 'stripslashes');

2010-12-13

Pippin Williamson answers:

Use a filter function to get rid of the slashes.

Add this function to your functions.php:


function no_magic_quotes($query) {
$data = explode("\\",$query);
$cleaned = implode("",$data);
return $cleaned;
}


Store the Twitter status received from your function into a variable, such as "$twitter_query", then run it through the function above like this:


<?php echo no_magic_quotes($twitter_query);?>