更改优先级后,SAVE_POST挂接不起作用

时间:2016-08-15 作者:milan patel

我正在使用日历事件pro插件。在该插件中save_post 使用挂钩,其优先级设置为20。我想在处理日历事件的数据后添加一些其他数据。所以我做到了

add_action(\'save_post\',\'save_tribe_func\',30,2);
function save_tribe_func($post_id,$post){

#some data
}
但不幸的是,我无法添加任何数据。

下面是中的代码save_tribe_func

 function save_tribe_func($post_id,$post){
if($post->post_status == "publish" && $post->post_type == "tribe_events"){
            //echo "<pre>";print_r($post);
            unset($post->comment_count);
            unset($post->filter);
            //unset($post->ID);

            //print_r($post);
            //echo "<pre>";print_r(get_post_meta($post_id));

            $all_tags=get_the_terms($post_id,\'tribe_event_tags\');
            $all_cat=get_the_terms($post_id,\'tribe_events_cat\');
            $all_tag_arr=[];$index=0;
            foreach ($all_tags as $key => $each_tag) {
                $all_tag_arr[$index++] = $each_tag->term_id;
            }
            foreach ($all_cat as $key => $each_tag) {
                $all_tag_arr[$index++] = $each_tag->term_id;
            }
            //echo "<pre>";print_r($all_tags);
            //echo "<pre>";print_r($all_tag_arr);
            //exit();

            $author_user=get_userdata($post->post_author);
            $dept_site_url=$author_user->data->user_url;            

            $inputJSON=json_encode($post);

            //echo $inputJSON;
            $dept_site_url="http://192.168.200.69/wordpress_test/";
            $ch = curl_init($dept_site_url.\'wp-json/myplugin/v1/posteventInsert/\');
              curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
              curl_setopt($ch, CURLOPT_VERBOSE, 0);                                                                       
              curl_setopt($ch, CURLOPT_POSTFIELDS, $inputJSON);                                                                  
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
              curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
              \'Content-Type: application/json\',                                                                               
                \'Content-Length: \' . strlen($inputJSON))                                                                       
              );
            $last_post_id = curl_exec($ch);

            echo $last_post_id;

            $meta_url=$dept_site_url.\'wp-json/myplugin/v1/postmetaInsert/\';

            $post_meta=get_post_meta($post_id);
            // echo \'<pre>\';
            // echo \'get_save_events_data\';
            // print_r($post_meta);
            // exit;
            if(isset($post_meta[\'_thumbnail_id\'])){
                set_thumbnail_id($post_meta[\'_thumbnail_id\'][0],$dept_site_url);
            }


          if($last_post_id != 0){

            //$post_meta=get_post_meta($post_id);

            $post_meta_arr=[];$i=0;
            foreach ($post_meta as $key => $metavalue) {
                $post_meta_arr[$i][\'post_id\']=$last_post_id;
                $post_meta_arr[$i][\'meta_key\']=$key;
                $post_meta_arr[$i][\'meta_value\']=$metavalue[0];         
                $i++;
            }
            $post_meta_arr[$i][\'post_id\']=$last_post_id;
            $post_meta_arr[$i][\'meta_key\']=\'event_main_site_id\';
            $post_meta_arr[$i][\'meta_value\']=$post_id;
            $meta_url=$dept_site_url.\'wp-json/myplugin/v1/posteventmetaInsert/\';

            $post_all[\'meta\']=$post_meta_arr;
            $post_all[\'tag\']=$all_tag_arr;


            //echo "<pre>";print_r($post_all);


            $meta_json=json_encode($post_all);
            //echo $meta_json;

            //exit;
            $ch = curl_init($meta_url);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_VERBOSE, 0);                                                                       
            curl_setopt($ch, CURLOPT_POSTFIELDS, $meta_json);                                                                  
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
                \'Content-Type: application/json\',                                                                                
                \'Content-Length: \' . strlen($meta_json))                                                                       
            );
            $meta_success = curl_exec($ch);
        }
        //echo "<pre>";print_r($post_meta_arr);
        //echo "id".$post_id;
        //echo "<pre>";print_r($post);


    }
    }

1 个回复
SO网友:Nathan Powell

按优先级移除挂钩,然后在同一位置添加自己的挂钩。含蓄地使用代码(正如您所看到的那样),一切都会顺利进行。在不知道钩子添加到哪里的情况下,您还需要检查价格(plugins\\u-loaded等)。