2012-10-10 3 views
0

게시 내용의 첫 번째 링크를 사용자 정의 필드에 추가하려고합니다. 뭔가 잘못된 것이고 나는 그것을 작동 시키려고 노력하고 있지만 운은 없다. "링크"사용자 정의 필드에 대한 링크를 추가하려면 어떻게해야합니까?사용자 정의 필드의 게시물에서 첫 번째 링크 추가 - Wordpress

add_action('publish_post', 'check_post'); 
function check_post($post_id) { 
$user_info = get_userdata(1); 

    function get_first_link() { 

     global $post, $posts; 
     preg_match_all('/href\s*=\s*[\"\']([^\"\']+)/', $post->post_content, $links); 
     return $links[1][0]; 

    } 
    $first_link = get_first_link(); 

    add_post_meta($post_id, 'link', $first_link, true); 
    add_post_meta($post_id, 'users', $user_info->user_login, true); 
} 

편집 중간에 작업했습니다. 그것은 URL을 저장하지만 게시물이 게시 될 때 저장하지 않습니다. 그것을 업데이트시 저장합니다. 무엇을 사용해야합니까? 나는 publish_post를 사용하여 해봤지만 URL은 업데이트시에도 저장되었습니다. 여기

add_action('save_post', 'my_save_post', 10, 2); 
function my_save_post($post_id, $post) { 
if (wp_is_post_revision($post_id)) 
    return; 

$matches = array(); 
preg_match_all('/href\s*=\s*[\"\']([^\"\']+)/', $post->post_content, $matches); 

$first_link = false; 
if (! empty($matches[1][0])) 
    $first_link = $matches[1][0]; 

$user_info = get_userdata(1); 

$meta_link = $_POST['link']; 

$args = array(
    'post__not_in'=> array($id), 
    'post_type' => 'post', 
    'post_status' => array('publish'), 
    'meta_query' => array(

     array(
    'key' => 'link', 
    'value' => $first_link, 
    'compare' => '=' 
    )  
) 
); 
$existingMeta = get_posts($args); 

if(empty($existingMeta)){ 
    //Go ahead and save meta data 
     update_post_meta($post_id, 'link', esc_url_raw($first_link)); 
    update_post_meta($post_id, 'users', $user_info->user_login); 
}else{ 
    //Revert post back to draft status 
    update_post_meta($post_id, 'link', $existingMeta[0]->ID); 
     update_post_meta($existingMeta[0]->ID, 'users', $user_info->user_login); 

    //Now perform checks to validate your data. 
     //Note custom fields (different from data in custom metaboxes!) 
     //will already have been saved. 
     $prevent_publish= true;//Set to true if data was invalid. 
     if ($prevent_publish) { 
     // unhook this function to prevent indefinite loop 
     remove_action('save_post', 'my_save_post'); 

     // update the post to change post status 
     wp_update_post(array('ID' => $post_id, 'post_status' => 'draft')); 

     } 

} 
} 
+0

는 대신'save_post'' 후크에 그것을 시도 할 수 있을까? – Xhynk

+0

'save_post'로 시도하면 치명적인 오류가 발생합니다. 치명적 오류 : get_first_link()를 다시 선언 할 수 없습니다 (이전에 /home/ddnets/public_html/site.com/wp-content/themes/twentyeleven/functions.php에 선언되었습니다. 618) in /home/ddnets/public_html/site.com/wp-content/themes/twentyeleven/functions.php on line 616' – Ciprian

+0

save_post가 두 번 (또는 그 이상) 실행되기 때문에 check_post 외부에서 get_first_link를 선언하십시오. 또한 게시물 메타를 추가하기 전에 수정 사항을 확인해야합니다. 또한 키가 이미있는 경우 add_post_meta가 실패하므로 update_post_meta를 사용해야합니다. save_post 훅에 대한 자세한 내용은 다음과 같습니다. http://codex.wordpress.org/Plugin_API/Action_Reference/save_post – kovshenin

답변

1

당신은 갈 :

add_action('save_post', 'my_save_post', 10, 2); 
function my_save_post($post_id, $post) { 
    if (wp_is_post_revision($post_id)) 
     return; 

    $matches = array(); 
    preg_match_all('/href\s*=\s*[\"\']([^\"\']+)/', $post->post_content, $matches); 

    $first_link = false; 
    if (! empty($matches[1][0])) 
     $first_link = $matches[1][0]; 

    update_post_meta($post_id, 'link', esc_url_raw($first_link)); 
} 
+0

좋은. 내 목표는 $ first_link가 이미 존재하는지 확인하고, 그렇다면 포스트에 update_post_meta ($ post_id, 'users', $ user_info-> user_login);를 추가하고 새 게시물을 삭제하거나 게시하지 않는다. . 나는 올바른 길을 가고 있는가? – Ciprian

+0

'get_option'을 사용하여 기존 값을 가져 와서 비교 한 다음 수행해야하는 작업을 수행 할 수 있습니다. 그러나 게시물 저장 후'save_post' 훅이 시작된다는 것에주의하십시오. 이미 게시 된 게시물이기 때문에 게시를 취소 할 수도 있습니다. 그렇게하기로 결정했다면,'wp_update_post'를 실행하기 전에'my_save_post' 액션을 제거해야합니다. 그렇지 않으면 무한 루프가됩니다. – kovshenin

+0

한 가지 더. xmlrpc를 통해 게시하는 경우 어떻게해야합니까? @ kovshenin – Ciprian

관련 문제