0

I have a WordPress blog pulling an RSS feed from Flickr. A plugin is grabbing the <description> tag in the XML and creating a pending post. I have a PHP function which modifies the embedded image and I'm trying to, in the same function, remove some extra text. I'm using str_replace and an array of strings (as suggested here).

My problem is that the first string I am trying to replace ($att[0]) is not replaced by my function.

rss.xml

<description>
  <!-- This first string is always the same, I'd like to remove it -->
  <p><a href="https://someUrlHere.com">username</a> posted a photo:</p> 
  <p><a href="https://linkToimage.com"><img src="https://imgSrc.jpg" /></a></p>
</description>

functions.php

function edit_content($content) {
    // Set the array of strings
    // $att[0] is not replaced by the function. Why?
    // $att[1] is replaced
    $att = array('<p><a href=\"https://www.flickr.com/people/bennettscience/\">bennettscience</a> posted a photo: </p>', '_m.jpg');
    $replace = array(' ', '_b.jpg');

    // Modify the content and return to the post.
    $content = str_replace($att, $replace, $content);
    return $content;
}
add_filter( 'the_content', 'edit_content');

I guess I have two questions:

  1. Is there something obvious I'm missing in the function?
  2. How can I debug the script? There are no PHP errors given when I update the file in WordPress. I'm not too familiar with PHP at this point.
Community
  • 1
  • 1
Brian
  • 4,274
  • 2
  • 27
  • 55

1 Answers1

1

You have a difference in whitespace between the XML file and your PHP string. You need to be 100% sure the string is exactly the same. Whitespace differences matter.

The XML file you posted has no space in photo:</p>, but your PHP string has photo: </p>.

Compare:

// XML
<p><a href="https://someUrlHere.com">username</a> posted a photo:</p> 
// PHP
<p><a href=\"https://www.flickr.com/people/bennettscience/\">bennettscience</a> posted a photo: </p>

Also, obligatory note: You're better off using an actual parser, instead of str_replace() or preg_replace(). See this classic post for an explanation of why.

Community
  • 1
  • 1
elixenide
  • 44,308
  • 16
  • 74
  • 100
  • Figured it was something small like that. Still isn't working, but I just discovered the plugin I'm using is adding three leading tabs, which obviously isn't in the search string, either.. I might switch to `preg_replace()` to handle the leading spaces rather than try and code it exact for `str_replace()`. Thanks. – Brian Jan 02 '17 at 19:29
  • @BrianBennett Yeah, using `str_replace()` here is going to be tricky; if Flickr ever changes anything about their feeds, it could break your script. You're better off using an actual parser, instead of `str_replace()` or `preg_replace()`. Also, just FYI, I recommend using a good IDE (like PHPStorm) and debugger to check these kinds of things in the future. – elixenide Jan 02 '17 at 19:32