我有几个人和我一起在一个网站上工作,他们需要使用一组非常简单的规则创建自定义分类术语;然而,他们似乎不知道如何做到这一点。所以,我试图创建一个某种过滤器,它会自动将它们的值替换为正确的值。例如,他们应该是这样的:
[name] -> UTA_1234 // type of training + _ + post id of training
[slug] -> uta_1234 // which should be automatic if left blank
[description] -> 1234 // just the post id
看起来很简单,对吧?嗯,他们一直把它们改成小写,用破折号代替下划线,忘记添加描述(因此,以下是我试图做的不起作用的事情:
/**
* \'codes\' is the taxonomy slug
*/
add_action( \'created_codes\', \'eri_created_incorrect_access_id\', 10, 2 );
function eri_created_incorrect_access_id( $term_id, $tt_id ) {
// Catch the term info
$name = $_POST[\'name\'];
$slug = $_POST[\'slug\'];
$desc = isset($_POST[\'description\']) ?? \'\';
// Replace dashes with underscores, and make name uppercase
$name = strtoupper(str_replace(\'-\', \'_\', $name));
$slug = str_replace(\'-\', \'_\', $slug);
// Check if there is a description
if (!$desc || $desc == \'\') {
// Then we split the name and add the post id to the description
$split_name = explode(\'_\', $name);
$desc = $split_name[1];
}
// Now add the items
add_term_meta( $term_id, \'name\', $name, true );
add_term_meta( $term_id, \'slug\', $slug, true );
add_term_meta( $term_id, \'description\', $desc, true );
}
相同的概念适用于其他自定义字段,但不适用于默认字段