2

I have created a Facebook App to upload photos to facebook from image url with help of some tutorials. It needs an image url and description. I want to put a button "Upload to Facebook" below every "Image" type posts in wordpress.

App Main Part to upload images -

<?php
if(isset($_POST["source"]))
{
try {
    $access_token=$facebook->getAccessToken();
    $graph_url= "https://graph.facebook.com/me/photos?"
  . "url=" . urlencode($_POST["source"])
  . "&message=" . urlencode($_POST['message'])
  . "&method=POST"
  . "&access_token=" .$access_token;
    $response=file_get_contents($graph_url);
    $json=json_decode($response);
  }
  catch (FacebookApiException $e) {
    error_log('Could not post image to Facebook.');
  }
}
?>

    <form enctype="multipart/form-data" action=" " method="POST">
        Paste an image URL here:
        <input name="source" type="text"><br/><br/>
        Say something about this photo:
        <input name="message"
               type="text" value=""><br/><br/>
        <input type="submit" value="Upload" class="btn btn-primary"/><br/>
    </form>

How can i extract image src dynamically from a custom post type(Image), and set src as source in form automatically. (There is only one image in every image type post)

2
  • Where is the image src located? post_content?
    – s_ha_dum
    Jun 2, 2013 at 17:05
  • image src is in post content. I don't know how i'll extract that. May be with get_post_meta. I don't know, that's why i posted my query here.
    – Prateek
    Jun 2, 2013 at 17:09

2 Answers 2

4

There is no built-in way to extract an image/image-src from the post body. If the images are attachments you can do it with get_children or WP_Query, and wp_get_attachment_image_src.

function get_image_src_from_content_101578($content) {
  global $post;
  $args = array( 
    'post_parent' => $post->ID,
  );
  $images = get_children($args);
  foreach ($images as $img) {
    var_dump(wp_get_attachment_image_src($img->ID));
  }
}
add_action('the_content','get_image_src_from_content_101578');

You could also use regex.

function replace_image_link_101578($content) {
  $pattern = '|<img.*?src="([^"]*)".*?/?>|';
  $content = preg_match($pattern,$content,$matches);
  var_dump($matches);
  return $content;
}
add_filter('the_content','replace_image_link_101578');

The latter might be less work for the server, but may also be less reliable. If you have images embedded that aren't attachments, it will be your only choice though.

A non-hook example that returns only the image src attribute, if found.

function replace_image_link_($content) {
  $pattern = '|<img.*?src="([^"]*)".*?/?>|';
  $content = preg_match($pattern,$content,$matches);
  return (!empty($matches[1])) ? $matches[1] : '';
}
8
  • Images are wordpress media files, but i didn't attached them, i embedded them. So i need image src as source for that app form.
    – Prateek
    Jun 3, 2013 at 3:24
  • Then the second option-- the one with the preg_match-- should extract the image source.
    – s_ha_dum
    Jun 3, 2013 at 3:27
  • ok i'll try it, but how i gonna send it to that app page?
    – Prateek
    Jun 3, 2013 at 3:41
  • Just feed the post content into that function. It doesn't have to be a filter. The function will process whatever you give it. Run the function, look at the var_dump and you should see the image src that you need. Take that and do with it whatever you want.
    – s_ha_dum
    Jun 3, 2013 at 3:45
  • 4
    I can't write this whole thing for you! You asked how to extract the image source. I told you. If you are asking how the Facebook API works you are off-topic. I understand that you say you "don't know PHP" but is so you shouldn't be writing PHP until you take some time to learn at least the basics, and "how do I call a function?" is pretty basic. No offense intended.
    – s_ha_dum
    Jun 3, 2013 at 19:56
0

get_children() is not a reliable way of getting all images in a post, because it uses the post_parent column in the wp_posts table to decide which images belong to a post. The problem is that post_parent of an image only points to the post that first used this image.

If other posts use the same image, get_children won't find it if run on that post, so your list of images will be incomplete and only contain images that have been attached for the first time.

1
  • While I think this is good info, maybe it's better as a comment on the other answer, since it doesn't answer the original question itself. :)
    – Rarst
    Jul 24, 2017 at 15:57

Your Answer

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

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