gpt4 book ai didi

php - 保存所有发布数据和发布元数据后会触发哪个 WordPress Hook ?

转载 作者:可可西里 更新时间:2023-11-01 13:46:46 27 4
gpt4 key购买 nike

我有一个自定义的帖子类型 crm,我需要在每个 crm 保存或更新后发送一封邮件。我将 cmb2 用于一些自定义元,如主题、用户等。我知道 save_post 钩子(Hook)在保存后 (根据 WordPress codex) 在我调用 的情况下触发 save_post 带有两个参数 (id 和 post) 帖子不包含更新值。这是我的代码:

function send_mail_to_user($id, $post){
$crm = $post;
$user_email = array();
if($crm->vc_all_vc == 'on'){
$args = array('orderby' => 'display_name');
$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();
if (!empty($authors)) {
foreach ($authors as $author) {
array_push($user_email , $author->user_email );
}
}
}
else{
$to_users = $crm->vc_users;
$to_program = $crm->vc_program;
$to_group = $crm->vc_group;
$to_excode = $crm->vc_ex_code;
foreach ($to_users as $key => $value) {
$user_data = get_userdata($value);
array_push($user_email, $user_data->user_email);
}
foreach ($to_program as $key => $value) {
$users = get_users( array('meta_key' => 'programs' ) );
if($users){
foreach ($users as $index => $data) {
if(in_array($value , explode('#', $data->programs))){
if(! in_array($data->user_email, $user_email) )
{
array_push($user_email, $data->user_email);
}
}
}
}
}
foreach($to_group as $group) {
$term = get_term_by('slug', esc_attr($group), 'user-group');
$user_ids = get_objects_in_term($term->term_id, 'user-group');
foreach($user_ids as $user_id){
$fc_user = get_userdata($user_id);
if(! in_array($fc_user->user_email, $user_email) )
{
array_push($user_email, $fc_user->user_email);
}
}
}
foreach($to_excode as $codes) {
$value = explode('*',$codes)[1];
$users = get_users( array('meta_key' => 'programs' ) );
if($users){
foreach ($users as $index => $data) {
if(in_array($value , explode('#', $data->programs))){
if(! in_array($data->user_email, $user_email) )
{
array_push($user_email, $data->user_email);
}
}
}
}
}
}
foreach($user_email as $index => $email){
$to = $email;
$subject = $crm->vc_subject;
$body = $crm->post_content;
$headers = array(
'Content-Type: text/html; charset=UTF-8'
);
wp_mail($to, $subject, $body, $headers);
}
}

add_action( 'save_post', 'send_mail_to_user', 10, 2 );

而且我还尝试了 publish_post 钩子(Hook),它在创建新帖子时工作正常,但在更新时它工作相同。我也尝试过 edit_postpost_updated Hook ,但我永远无法检索我的更新数据。

那我该如何解决呢?哪个 Action Hook 会给我所有新数据?提前致谢。

最佳答案

你可以使用这样的东西,

function your_custom_function($meta_id, $post_id, $meta_key='', $meta_value='') {
if($meta_key=='_edit_lock') {
// if post meta is updated
}
}
add_action('updated_post_meta', 'your_custom_function', 10, 4);

关于php - 保存所有发布数据和发布元数据后会触发哪个 WordPress Hook ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44518752/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com