게시 내용의 첫 번째 링크를 사용자 정의 필드에 추가하려고합니다. 뭔가 잘못된 것이고 나는 그것을 작동 시키려고 노력하고 있지만 운은 없다. "링크"사용자 정의 필드에 대한 링크를 추가하려면 어떻게해야합니까?사용자 정의 필드의 게시물에서 첫 번째 링크 추가 - 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'));
}
}
}
는 대신'save_post'' 후크에 그것을 시도 할 수 있을까? – Xhynk
'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
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