添加自定义字段中的帖子的第一个链接 - Wordpress
我尝试将帖子内容中的第一个链接添加到自定义字段。有些事情是错误的,我一直试图让它工作,但没有运气。我应该如何添加链接到“链接”自定义字段?添加自定义字段中的帖子的第一个链接 - 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);
}
编辑 我得到了它一半的工作。它会保存网址,但是当帖子发布时它不会保存它。它在更新时保存。我需要使用什么?我尝试使用publish_post,但也保存了更新网址。
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'));
}
}
}
在这里你去:
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));
}
不错。我的目标是检查$ first_link是否已经存在,如果它存在,将它添加到它的文章:'update_post_meta($ post_id,'users',$ user_info-> user_login);'并删除或不发布新文章。我在正确的道路上吗? – Ciprian
你可以通过'get_option'获得现有的值,然后进行比较,然后做任何你必须做的事情。但是,请注意,保存帖子后会触发save_post钩子。它也可能意味着该帖子已经发布,因此您可能需要取消发布该帖子。如果你决定这么做,确保在发射'wp_update_post'之前移除'my_save_post'动作,否则你会以无限循环结束。 – kovshenin
还有一件事。如果我通过xmlrpc发布,该怎么办? @kovshenin – Ciprian
你可以试试它的''save_post''钩呢? – Xhynk
如果我尝试使用'save_post',我得到致命错误'致命错误:无法重新声明get_first_link()(先前在/home/ddnets/public_html/site.com/wp-content/themes/twentyeleven/functions.php中声明: 618)在/home/ddnets/public_html/site.com/wp-content/themes/twentyeleven/functions.php on line 616' – Ciprian
声明get_first_link超出check_post,因为save_post运行两次(或更多)。在添加后期元素之前,您还应该检查修订版本。此外,您应该使用update_post_meta,因为如果密钥已存在,add_post_meta将会失败。以下是关于save_post钩子的更多信息:http://codex.wordpress.org/Plugin_API/Action_Reference/save_post – kovshenin