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 am using this function to get imnages urls from google

   function getGoogleImg($k)
{
    $url = "http://images.google.com/images?as_q=##query##&hl=it&imgtbs=z&btnG=Cerca+con+Google&as_epq=&as_oq=&as_eq=&imgtype=&imgsz=m&imgw=&imgh=&imgar=&as_filetype=&imgc=&as_sitesearch=&as_rights=&safe=images&as_st=y";
    $web_page = file_get_contents( str_replace("##query##",urlencode($k), $url ));
    $tieni = stristr($web_page,"dyn.setResults(");
    $tieni = str_replace( "dyn.setResults(","", str_replace(stristr($tieni,");"),"",$tieni) );
    $tieni = str_replace("[]","",$tieni);
    $m = preg_split("/[\[\]]/",$tieni);
    $x = array();
    for($i=0;$i<count($m);$i++)
    {
        $m[$i] = str_replace("/imgres?imgurl\\x3d","",$m[$i]);
        $m[$i] = str_replace(stristr($m[$i],"\\x26imgrefurl"),"",$m[$i]);
        $m[$i] = preg_replace("/^\"/i","",$m[$i]);
        $m[$i] = preg_replace("/^,/i","",$m[$i]);
        if ($m[$i]!="")
        array_push($x,$m[$i]);
   }
   return $x;
}

After that i want add image url to post meta i am using this function to add post meta

  function add_image_to_save_post($post_ID)
    {
            if ( !wp_is_post_revision($post_id) )
        {
        $title = $_POST['post_title'];
        $image = getGoogleImg ($title);
        add_post_meta($post_ID, 'thumb', $image[3], true);

        }
    }
    add_action('wp_insert_post', 'add_image_to_save_post');

But when i click on on link add new post its add autmatically blank thumb key in database

I just want this for users so just add articles they no need to add image to article image will fetch automatically,

Can anyone give me suggestion how this possible

Thanks

MOhammad Umer

share|improve this question
    
I don't understand the need for global $post; $pos_id = $post->ID;, besides the $pos_id being an unused variable afterwards I don't think global $post contains anything at that point, you already have the $post_ID from the action, what do you need global $post; for? –  soulseekah Oct 18 '11 at 7:55
    
ok i will remove them –  Mohammad Umer Oct 18 '11 at 8:45
    
But still getting empty thumb after click on add new post –  Mohammad Umer Oct 18 '11 at 8:47
    
is there any action when user click on "post submit for review" this button then fetch thumb key –  Mohammad Umer Oct 18 '11 at 9:07
    
Hold on, are you changing the post status to "Pending Review"? Can you add die('Trying to get image...'); at the very top of your function to see if it's even called? Also modify your question to reflect the current code you're working with, please. –  soulseekah Oct 18 '11 at 9:12
show 4 more comments

1 Answer

up vote 0 down vote accepted

If you look at your current setup, you can see that your add_image_to_save_post function is fired off when wp_insert_post is performed.

wp_insert_post is fired off when a post is inserted into the database, and as you may know, as soon as you click "Add New" in WordPress a database entry is created, thus pulling on your hook.

What happens next is $title = $_POST['post_title'];, and yet there is no post_title, so getGoogleImg() is called with NULL, thus, I assume that the $web_page is not populated with what you would expect.

WordPress offers a set of actions that are useful when a post is saved, edited and even has its status changed. In your case, posts are premoderated for users, meaning that they cannot publish and can only set the status to pending. And there are actions for status changes: draft_to_pending, draft_to_publish, pending_to_publish, publish_to_private, etc. its always in the form of status_to_status.

So hopefully, changing the hook to fire off on draft_to_pending will solve your problem.

And on a completely different note, it's not good to use non-validated user-input from $_POST['post_title'], who knows what kind of things the user will write there, and will result in directory-traversal and XSS in your case. So try to grab it from the post instead, you have the $post_ID, get it using get_post($post_ID) and then grab its post_title from there.

Hope this helped.

share|improve this answer
    
Thank you very much, that is help me a lot. –  Mohammad Umer Oct 19 '11 at 10:06
add comment

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.