admin管理员组文章数量:1130349
I have this function to set the title of a wordpress post which works properly for saving / updating posts. The major issue is that the function stops posts being deleted (it removes the title but the post still exists).
Is there a way to identify if the action is a delete post rather than save / update post and obviously i don't return the $data if a delete action?
add_filter( 'wp_insert_post_data', array($this, 'updatetitle' ) , '99', 1 );
public function updatetitle($data) {
if($data['post_type'] != $property->post_name || !$_POST[${$this->active_class_var_name}->noncefield] ):
return $data;
endif;
if($_POST['address'] && $_POST[$property->noncefield]):
$data['post_title']= ucfirst (sanitize_text_field($_POST['address'])).' '.ucfirst (sanitize_text_field($_POST['town'])).' '.ucfirst (sanitize_text_field($_POST['county']) );
endif;
return $data;
}
I have this function to set the title of a wordpress post which works properly for saving / updating posts. The major issue is that the function stops posts being deleted (it removes the title but the post still exists).
Is there a way to identify if the action is a delete post rather than save / update post and obviously i don't return the $data if a delete action?
add_filter( 'wp_insert_post_data', array($this, 'updatetitle' ) , '99', 1 );
public function updatetitle($data) {
if($data['post_type'] != $property->post_name || !$_POST[${$this->active_class_var_name}->noncefield] ):
return $data;
endif;
if($_POST['address'] && $_POST[$property->noncefield]):
$data['post_title']= ucfirst (sanitize_text_field($_POST['address'])).' '.ucfirst (sanitize_text_field($_POST['town'])).' '.ucfirst (sanitize_text_field($_POST['county']) );
endif;
return $data;
}
Share
Improve this question
asked Oct 11, 2014 at 16:58
DavidDavid
1231 silver badge6 bronze badges
2 Answers
Reset to default 1Not sure, but seems maybe $data['post_status'] would be set to 'trash' at that point. If so, then you could just do something like...
if ($data['post_status'] == 'trash') { return $data; }
...before the other manipulations.
EDIT
That code should work then - but needs to be at top of the function, like...
public function updatetitle($data) {
if ($data['post_status'] == 'trash') { return $data; }
if($data['post_type'] != $property->post_name || !$_POST[${$this->active_class_var_name}->noncefield] ):
return $data;
endif;
if($_POST['address'] && $_POST[$property->noncefield]):
$data['post_title']= ucfirst(sanitize_text_field($_POST['address'])).' '.
ucfirst (sanitize_text_field($_POST['town'])).' '.
ucfirst (sanitize_text_field($_POST['county']) );
endif;
return $data;
}
Your function in that case would simply return back the $data without making any other modifications to it.
Try the save_post hook instead of wp_insert_post_data.
It should solve your problem
I have this function to set the title of a wordpress post which works properly for saving / updating posts. The major issue is that the function stops posts being deleted (it removes the title but the post still exists).
Is there a way to identify if the action is a delete post rather than save / update post and obviously i don't return the $data if a delete action?
add_filter( 'wp_insert_post_data', array($this, 'updatetitle' ) , '99', 1 );
public function updatetitle($data) {
if($data['post_type'] != $property->post_name || !$_POST[${$this->active_class_var_name}->noncefield] ):
return $data;
endif;
if($_POST['address'] && $_POST[$property->noncefield]):
$data['post_title']= ucfirst (sanitize_text_field($_POST['address'])).' '.ucfirst (sanitize_text_field($_POST['town'])).' '.ucfirst (sanitize_text_field($_POST['county']) );
endif;
return $data;
}
I have this function to set the title of a wordpress post which works properly for saving / updating posts. The major issue is that the function stops posts being deleted (it removes the title but the post still exists).
Is there a way to identify if the action is a delete post rather than save / update post and obviously i don't return the $data if a delete action?
add_filter( 'wp_insert_post_data', array($this, 'updatetitle' ) , '99', 1 );
public function updatetitle($data) {
if($data['post_type'] != $property->post_name || !$_POST[${$this->active_class_var_name}->noncefield] ):
return $data;
endif;
if($_POST['address'] && $_POST[$property->noncefield]):
$data['post_title']= ucfirst (sanitize_text_field($_POST['address'])).' '.ucfirst (sanitize_text_field($_POST['town'])).' '.ucfirst (sanitize_text_field($_POST['county']) );
endif;
return $data;
}
Share
Improve this question
asked Oct 11, 2014 at 16:58
DavidDavid
1231 silver badge6 bronze badges
2 Answers
Reset to default 1Not sure, but seems maybe $data['post_status'] would be set to 'trash' at that point. If so, then you could just do something like...
if ($data['post_status'] == 'trash') { return $data; }
...before the other manipulations.
EDIT
That code should work then - but needs to be at top of the function, like...
public function updatetitle($data) {
if ($data['post_status'] == 'trash') { return $data; }
if($data['post_type'] != $property->post_name || !$_POST[${$this->active_class_var_name}->noncefield] ):
return $data;
endif;
if($_POST['address'] && $_POST[$property->noncefield]):
$data['post_title']= ucfirst(sanitize_text_field($_POST['address'])).' '.
ucfirst (sanitize_text_field($_POST['town'])).' '.
ucfirst (sanitize_text_field($_POST['county']) );
endif;
return $data;
}
Your function in that case would simply return back the $data without making any other modifications to it.
Try the save_post hook instead of wp_insert_post_data.
It should solve your problem
本文标签: wp update postwpinsertpostdata filter hook identify current action
版权声明:本文标题:wp update post - wp_insert_post_data filter hook identify current action 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749084173a2313512.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论