A Bit Stumbled on Something PHP/Wordpress Related

Hi,

I am a bit stumbled on how to get the Wordpress Custom fields feature to work through PHP. I have gotten for Wordpress to apply the custom fields to all the posts, however, am now a bit stumped on how to add a variable within the Custom fields code to be able to pull the real image URL. The end result I am trying to get is for the code to pull the image URL.

//curl function
function get_data($url){
	$ch = curl_init();	
	$timeout = 25;	
	curl_setopt($ch,CURLOPT_URL,$url);	
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);	
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);	
	$data = curl_exec($ch);	curl_close($ch);	
	return $data;
}

$queryul="SELECT * FROM ".$wpdb->prefix."db";
$film = $wpdb->get_row($queryul);
$from = $_GET['from'];
if($film!=NULL){

	$link=$film->url;
	$link=trim($link);
	$data=$film->data;
	$queryul2="DELETE FROM ".$wpdb->prefix."db WHERE url='$link'";
	$wpdb->query($queryul2);
	$data=trim($data);
	$category=$film->category;
	$category=trim($category);
	$imdb_content = get_data($link);

	// get Poster Url;
	$pos=strpos($imdb_content,'Poster"');
	$img_search = substr($imdb_content, 0, $pos);
	substr($imdb_content, 0, $pos);
	if($pos!=0)	{
		$pos1=strrpos($img_search,'src="');
		$pos2=strpos($img_search,'"',$pos1+5);
		$poster=substr($imdb_content,$pos1+5,$pos2-$pos1-5);
		srand((float) microtime() * 10000000);			
		$indice=rand(1000,9999);			
		$where="posters/".$data."-".$indice.".jpg";			
		copy($poster,$where);			
		$where=get_option('home')."/admin/".$where;
add_action('publish_page', 'add_custom_field_automatically');
add_action('publish_post', 'add_custom_field_automatically');

function add_custom_field_automatically($post_ID) {
	global $wpdb;
$thelink=$where
	if(!wp_is_post_revision($post_ID)) {
		add_post_meta($post_ID, 'image', $thelink, 'true');
	}
}

Basically, I don’t get why $thelink is not pulling the data from $where, which should be the URL of the image link. What I am trying to edit is the last snippet of code (second box) to be able to get the correct image URL to input into the custom field. If anyone could advise what I am doing wrong, I am will feel very appreciative. I have spent several hours messing with it, and still am stumped. Any help would be greatly appreciated.

Thanks!

$thelink=$where ;

Not sure if that is intentional, you are missing a semicolon on that line, should be showing you an error though …

Looks to me as though $where should be declared global too.

The ; was a mistake on my part on copy and pasting an older version of the code, but I don’t think it is the reason why it is not working.

Also, since $where pulls from both $data and $intice, do you believe setting these to be global would be the way to go?

Easy enough to test using trial and error:


//override your $where for a moment
$where = "a test";

// then

function add_custom_field_automatically($post_ID) { 
    global $wpdb; 

// uncomment out the line below and see what diff it makes
//    global $where; 
var_dump($where);

// commented out for the meantime
//$thelink=$where 
//    if(!wp_is_post_revision($post_ID)) { 
//        add_post_meta($post_ID, 'image', $thelink, 'true'); 
//    } 
}