admin管理员组文章数量:1320634
I got some plugins that auto-generates custom fields. Does anyone know how to automatically remove empty custom fields from post when I press "publish"?
A check I can put in my functions.php
that does a check and removes the custom field if there is no value?
I got some plugins that auto-generates custom fields. Does anyone know how to automatically remove empty custom fields from post when I press "publish"?
A check I can put in my functions.php
that does a check and removes the custom field if there is no value?
2 Answers
Reset to default 1Demilio, unfortunately the WordPress API seems to assume that custom fields do not have an 'empty' value, in the sense that update_post_meta
and delete_post_meta
, if given ''
as the (previous) meta value perform the update/delete operation for all values for that key.
This makes it difficult, for instance, if a custom field key has multiple values associated with it, some of which are empty and want to be removed.
Below is the basic logic: It will only remove fields where all associated fields are 'empty'. Exactly what 'empty' means, you can decide by specifying a callback to array_filter
, by default, as used in the code below this includes '0', false etc.
The hook you are after is save_post
. This is fired after the post and all it's post-meta, has been saved having clicked 'update'/'publish', but also on auto-saves...
add_action('save_post','my_cf_check');
function my_cf_check($post_id) {
// verify this is not an auto save routine.
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
//authentication checks
if (!current_user_can('edit_post', $post_id)) return;
//obtain custom field meta for this post
$custom_fields = get_post_custom($post_id);
if(!$custom_fields) return;
foreach($custom_fields as $key=>$custom_field):
//$custom_field is an array of values associated with $key - even if there is only one value.
//Filter to remove empty values.
//Be warned this will remove anything that casts as false, e.g. 0 or false
//- if you don't want this, specify a callback.
//See php documentation on array_filter
$values = array_filter($custom_field);
//After removing 'empty' fields, is array empty?
if(empty($values)):
delete_post_meta($post_id,$key); //Remove post's custom field
endif;
endforeach;
return;
}
As noted above, if a custom field key has multiple values associated with it, some of which are empty - they will not be removed. This will probably be ok for your use. There seems to be no easy 'WordPress' way around this. One way would be a custom MYSQL statement.
Thanks Stephen Harris! You rock!
Additionally, if you want to do this only for specific fields, you can put your specific values in an array, and compare it to the $custom_fields array with array_intersect()
.
$result = !empty(array_intersect($specific_values, $custom_fields));
本文标签: functionsAutoremove custom field with no value on publish
版权声明:本文标题:functions - Auto-remove custom field with no value on publish 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742071772a2419167.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论