Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've built a front-end post submission form that allows a user to include a shortcode provided by SoundCloud. SoundCloud shortcodes generally look like this:

[soundcloud url="http://api.soundcloud.com/playlists/92355602" params="" width=" 100%" height="450" iframe="true" /]

The user pastes the shortcode into my form and my form receives the short code just fine. Before I save it as post meta on the post, I'm trying to perform a few str_replace functions on it.

if( isset( $_POST['scembed'] ) ) {
    $scembed = $_POST['scembed'];
    $scembed = str_replace( " 100%", "100%", $scembed );
    $scembed = str_replace( "params=\"\"", "params=\"show_artwork=false\"", $scembed );
    $scembed = str_replace( "height=\"450\"", "height=\"405\"", $scembed );
}

The first one works because it's just removing the erroneous space in the string. But then the next two, I actually need to include the attribute and its double quotes to accurately target the string to change.

It won't work and I'm not sure why. I even tried this:

$pfind = 'params=""';
$preplace = 'params="show_artwork=false"';
$scembed = str_replace( $pfind, $preplace, $scembed );

What am I missing? Any time you include the double quotes for the attributes, the function is ignored :-/

-- For Ravs -------------------

<form name="create_post" method="post" action="" enctype="multipart/form-data">
    <input type="text" name="scembed" maxlength="160" />
    <input type="submit" value="Submit" name="submit" />
</form>

if( !empty( $_POST ) ) {
  if( !empty( $_POST['scembed'] ) ) {
      $scsource = $_POST['scembed'];
    $searchReplaceArray = array(
      ' 100%' => '100%', 
      'params=""' => 'params="show_artwork=false"',
      'height="450"' => 'height="405"',
    );
    $scembed = str_replace( array_keys( $searchReplaceArray ), array_values( $searchReplaceArray ), $scsource );
  }
}

if( $scembed )
  add_post_meta( $post_id, 'scembed', $scembed, TRUE );
share|improve this question
    
what output you are getting ? function seems to work fine. –  Ravs Aug 23 '13 at 2:27
    
The output which gets stored in my post_meta is this: [soundcloud url="http://api.soundcloud.com/playlists/92355602" params="" width="100%" height="450" iframe="true" /] The only thing that changes is the space before 100% which is the first str_replace I execute. The other two never seem to work. –  Suzanne Aug 23 '13 at 2:35
    
can you paste you whole function code in question that do this functionality? –  Ravs Aug 23 '13 at 2:37
    
There are well over 350 lines of code in this form/processing function but I pasted the parts where this data is at play. I have now tried a short code without the [ and ] still didn't work which may rule out WordPress thinking it's a short code and preventing something. The new code I pasted for Ravs doesn't work either. It will remove the leading 0 from the 100% but will NOT change the other two. –  Suzanne Aug 23 '13 at 3:35
2  
Have you tried echoing or logging the value of $_POST['scembed'] before you get started with replacing, to see what is actually in it? My guess is that the value has some escaping or encoding of its own already in the string before you get it. For example, the string might actually have params=\"\" in it or maybe even params=&quot;&quot;. –  Ben Miller Aug 23 '13 at 3:40

1 Answer 1

up vote 2 down vote accepted

try to put this code before replace text in string

inside if( !empty( $_POST['scembed'] ) ) {

$_POST['scembed'] = stripslashes( $_POST['scembed'] );

Note

problem occuring because your quote are escaped in string, so " becomes \"

share|improve this answer
    
Ravs you're right... in a weird scenario where I was testing something else, my form data was returned to the screen and I could then see the quotes had already been escaped! Thanks for your help and solution. –  Suzanne Aug 23 '13 at 4:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.