2012-11-06 6 views
4

내 게시물마다 추천 이미지로 구성된 Photo/Wordpress 사이트가 있습니다. 내가 만들고자하는 것은 게시물을 게시 한 후 자동으로 업로드 된 추천 이미지를 Twitter에 게시하는 것입니다. 게시물을 게시 할 때 실행되는 Functions.php에 함수를 추가 할 수있었습니다.Wordpress 게시 기능 트위터에 이미지 게시

add_action('publish_post','postToTwitter'); 

postToTwitter 함수는 Matt Harris OAuth 1.0A 라이브러리가있는 트윗을 만듭니다. postToTwitter 함수의 파일과 관련된 이미지를 첨부하면 정상적으로 작동합니다.

// this is the jpeg file to upload. It should be in the same directory as this file. 
$image = dirname(__FILE__) . '/image.jpg'; 

그래서 저는 $ image var을 Wordpress 게시물에 업로드 한 내 추천 이미지를 보유하고 싶습니다.

그러나 업로드 된 이미지의 URL을 추가하는 것만으로는 작동하지 않습니다 (Wordpress 업로드 폴더는 postToTwitter 함수의 파일과 관련이 없기 때문에) : 미디어 엔드 포인트 (Twitter)를 사용한 업데이트는 직접 업로드 한 이미지 만 지원합니다 POST에서는 원격 URL을 인수로 사용하지 않습니다.

제 질문은 내가 POST에 업로드 된 추천 이미지를 어떻게 참조 할 수 있습니까?

// This is how it should work with an image upload form 
$image = "@{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}" 
+0

더 나은 대답은 전체 코드를 보는 데 달려 있습니다. ['WP_CONTENT_DIR'] (http://codex.wordpress.org/Determining_Plugin_and_Content_Directories#Constants)를 사용하면 어떨까요? – brasofilo

답변

0

URL 대신 이미지 파일 경로를 얻는 방법을 묻고 나머지 $ 이미지 문자열을 채우는 것처럼 들리는 것 같습니다. Wordpress 함수 get_attached_file()으로 파일 경로를 가져온 다음 몇 가지 PHP 함수로 전달하여 나머지 이미지 메타 데이터를 가져올 수 있습니다. 그런데

// Get featured image. 
$img_id = get_post_thumbnail_id($post->ID); 
// Get image absolute filepath ($_FILES['image']['tmp_name']) 
$filepath = get_attached_file($img_id); 
// Get image mime type ($_FILES['image']['type']) 
// Cleaner, but deprecated: mime_content_type($filepath) 
$mime = image_type_to_mime_type(exif_imagetype($filepath)); 
// Get image file name ($_FILES['image']['name']) 
$filename = basename($filepath); 

, publish_postaccording to the Codex 때문에, 또한 매번 게시 된 게시물을 편집라고하며,이 경우에 사용하는 가장 후크하지 않을 수 있습니다. 모든 업데이트가 트위터되고 싶지 않다면 ${old_status}_to_${new_status} 후크 (게시 객체를 전달)를 살펴볼 수 있습니다. 이 후크를 사용하는 것이 더 좋을 수도 게시물 '이전의 상태에 따라 트윗을 변경하려는 경우,

add_action('new_to_publish', 'postToTwitter'); 
add_action('draft_to_publish', 'postToTwitter'); 
add_action('pending_to_publish', 'postToTwitter'); 
add_action('auto-draft_to_publish', 'postToTwitter'); 
add_action('future_to_publish', 'postToTwitter'); 

을 또는 : transition_post_status 그것이 전달하기 때문에 그래서 그 대신 add_action('publish_post','postToTwitter')의이 같은 아마 뭔가 더 잘 작동 것 이전 상태와 새로운 상태를 인수로 사용합니다.