你要找的钩子是created_{$taxonomy}
但还有一个问题。我认为逻辑上最大的缺陷是把州和城市放在同一个分类法中。你永远无法区分这两个词(哪个是城市,哪个是州)。这将打开用户以执行以下部分操作,所有操作都不正确
输入无州城市我认为解决方法是将州和城市划分为两个不同的分类法。tax_states
和tag_cities
. 这将确保每个帖子都有一个州,每个城市标签都以州为前缀。我将开始下面的代码,假设这两个分类法已注册。
首先,让我们添加一些jQuery以确保设置了状态—您可能需要考虑enqueueing 但由于它是一个小脚本,可能不是:
function post_type_custom_script() {
global $post_type;
if( \'post\' === $post_type ) {
?>
<script type="text/javascript">
/* Checks if cat is selected when publish button is clicked */
jQuery( \'#submitdiv\' ).on( \'click\', \'#publish\', function( e ) {
var $checked = jQuery( \'#category-all li input:checked\' );
//Checks if cat is selected
if( $checked.length <= 0 ) {
alert( "Please select a State" );
return false;
} else {
return true;
}
} );
</script>
<?php
}
}
add_action( \'admin_footer\', \'post_type_custom_script\' );
上述脚本的最大问题是,它假设您的分类法是分层的(复选框),但由于理论上只应选择一个状态,因此分类法都不是最佳选择。相反,您可以创建一个术语选择列表并使用
save_post
要保存术语或(我建议)将层次复选框更改为单选按钮(
PHP Class,
Radio Buttons for Taxonomies Plugin ).
现在我们应该有一个州的分类法,需要选择它才能发布,还有一个城市的标记框。下一步是在发布城市标记时将状态附加到城市标记。这是一个庞大的条件列表,所以我在必要时添加了注释。如果某个州没有被选中,我们会把帖子变成草稿,作为最后的结果:
/**
* This function runs whenever a post is saved
*
* @param Int $post_id
*
* @return void
*/
function state_appended_tags( $post_id ) {
global $post;
if( ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) || ( ! current_user_can( \'edit_post\', $post_id ) || ! is_object( $post ) ) ) {
return $post_id;
}
if( \'post\' === $post->post_type ) {
$state = false;
$set_draft = false;
if( isset( $_POST[\'tax_input\'] ) && ! empty( $_POST[\'tax_input\'] ) ) {
// Grab State
// If using the Radio Buttons Plugin linked above, use this conditional
// if( isset( $_POST[\'radio_tax_input\'][\'tax_states\'] ) && ! empty( $_POST[\'radio_tax_input\'][\'tax_states\'] ) && 0 !== $_POST[\'radio_tax_input\'][\'tax_states\'][0] ) {
// $state_obj = get_term_by( \'id\', $_POST[\'radio_tax_input\'][\'tax_states\'][0], \'tax_states\' ); // Grab Term Object
// $state_slug = $state_obj->slug;
if( isset( $_POST[\'tax_input\'][\'tax_states\'] ) && ! empty( $_POST[\'tax_input\'][\'tax_states\'] ) && 0 !== $_POST[\'tax_input\'][\'tax_states\'][0] ) {
$state_obj = get_term_by( \'id\', $_POST[\'tax_input\'][\'tax_states\'][0], \'tax_states\' ); // Grab Term Object
$state_slug = $state_obj->slug;
} else {
$set_draft = true;
}
// Prefix Cities
if( ! empty( $state_slug ) && isset( $_POST[\'tax_input\'][\'tag_cities\'] ) && ! empty( $_POST[\'tax_input\'][\'tag_cities\'] ) ) {
$new_tags = array();
foreach( $_POST[\'tax_input\'][\'tag_cities\'] as $city ) {
$city_obj = false;
$city_slug = \'\';
// Post tags come in two forms - slugs and integers
if( is_numeric( $city ) ) {
$city_obj = get_term_by( \'id\', $city, \'tag_cities\' ); // Grab Term Object
$city_slug = $city_obj->slug;
} else {
$city_obj = get_term_by( \'slug\', $city, \'tag_cities\' ); // Grab Term Object
$city_slug = $city_obj->slug;
}
// If user has already entered the correct state-city formatted tag, skip it
// Otherwise we\'ll enter the conditional
if( 0 !== strcmp( "{$state_slug}-{$city_slug}", $city_slug ) ) {
$new_tags[] = "{$state_slug}-{$city_slug}";
wp_delete_term( $city_obj->term_id, \'tag_cities\' ); // Delete Term
}
}
// Set New State Appended Tags
if( ! empty( $new_tags ) ) {
wp_set_object_terms( $post_id, $new_tags, \'tag_cities\', false );
}
}
} else {
$set_draft = true;
}
// Finally, should something have failed, don\'t publish the post and investigate
if( $set_draft ) {
remove_action( \'save_post\', \'state_appended_tags\' );
wp_update_post( array(
\'ID\' => $post_id,
\'post_status\' => \'draft\',
) );
add_action( \'save_post\', \'state_appended_tags\' );
}
}
}
add_action( \'save_post\', \'state_appended_tags\' );
你需要换掉
tag_cities
和
tax_states
使用实际的分类法,但这应该适合您。