0

Can you explain the hooks or filters WordPress applies when saving a post which remove an absolute URL and makes it a relative URL?

Background

My site consumes an RSS feed, that I pay for, which includes a tracking pixel at the end of each post. The tracking pixel is in an IMG tag with a bunch of parameters after it - something like this:

<img src='http://example.com/trackingPixel.gif?param1=123abc456' />

The tracking pixel changes for each item in the RSS feed (after 'param1' in the example), so I can't just add the pixel myself - I have to use the one that's in the feed's content. The problem is WordPress replaces the tracking pixel with a relative URL like this:

/uploads/2012/02/trackingPixel.gif

The modified URL makes the tracking pixel useless.

I have been looking for a workaround for hours now - I checked default filters, removed default filters, modified $allowedtags global variable, commented out the wp_strip_all_tags function, went through the theme, RSS reader, etc, etc.

4
  • How are you importing the data from the feed to your site? (Code preferably)
    – mor7ifer
    Commented Feb 4, 2012 at 14:03
  • I am using FeedWordPress and I am also trying with WP-Robot; the behaviour is the same in both cases. This happens in the editor too - just add an image, that's hosted elsewhere, to a post in HTML mode and save. Go to your posts list and edit the post again in HTML mode and you'll see that WP modified the image's URL. Commented Feb 4, 2012 at 19:28
  • I found media_sideload_image - which seems to upload a given image and return the modified URL - I coded it to just return whatever it gets and am still seeing the same behaviour. What a pain. Commented Feb 4, 2012 at 20:48
  • I modified default-filters.php to remove filters for that call wp_kses_data from just about everything - still no change, in the editor itself and through RSS. Obviously something is happening when a post gets saved, regardless of through the UI or using code. Commented Feb 4, 2012 at 21:12

1 Answer 1

1

Regarding the HTML editor, if you want to use a different path, without all the functions/ hooks that I will list below, you should use the UPLOAD FROM URL tab, and not the UPLOAD FROM COMPUTER .. that will keep the original URL.

I am not sure about the tracking pixel, how you get it in the RSS (format) or other variables in your system, but the function that sends the images to the editor is a function called image_send_to_editor();

So using the next function, will alter the URL that is SENT TO THE EDITOR..

add_filter('image_send_to_editor', 'change_image_url', 20, 8);
$original_URL = 'your_url/not_sure_how/'; // this part you have to pass the original URL..
function change_image_url($html, $id, $caption, $title, $align, $url, $size, $alt)
{
$url = $original_URL ;
return $url;
}

For using outside the editor, the upload path can be intercepted in a LOT of ways, depending on what you really need to do (like I said, I do not know HOW you get the RSS, but start with checking and hooking these function :

update_attached_file( $attachment_id, $file ) 

What you would hook here is the $file part, which originally is:

$file = _wp_relative_upload_path($file);

As you can see, it calls yet another function with $path parameter that you can hook which is:

 _wp_relative_upload_path( $path )

This function will change the $path of the image to be relative to the upload dir.

The function update_attached_file(); will also update the post_meta hidden field _wp_attached_file with:

update_post_meta( $attachment_id, '_wp_attached_file', $file );

This is yet another way to control the path. You can manipulate that custom_field to your needs. It is actually the easiest way IMHO...

2
  • Thanks for your help - you cleared up a lot and I eventually fixed it by writing a plugin that hooks 'the_content' and 'the_editor_content' - the plugin saves and restores what I need before everything else gets to a post, and after other plugins, etc are finished with it. Commented Feb 6, 2012 at 21:14
  • I am glad it helped you, and I would love to see your solution somehow - itt would be nice if you will post the code somehow so it could help others in similar distress but with less coding abilities (maybe pastebin or snippler)
    – krembo99
    Commented Feb 8, 2012 at 2:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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