过滤器知识title_save_pre
在将贴子写入数据库之前,以及在将其任何贴子元保存到数据库之前运行。
wp_get_object_terms
此时将为空白。
要更正此问题,您需要save_post
并从那里更新帖子。save_post
将帖子保存到数据库并保存内置“自定义字段”后运行。
来源@https://wordpress.stackexchange.com/a/54713/190376使用前需要了解一些事情save_post
就像infinite loop case. 总的来说,你非常接近。
演示我已经对所有内容发表了评论。我添加了一些jquery来禁用标题输入,并添加了一个过滤器来更新默认占位符。如果没有选择分类法,我还限制了发布的能力。
<?php
add_action( \'init\', function() {
/**
* Determines whether the current request is for an administrative interface page
* @link https://developer.wordpress.org/reference/functions/is_admin/
*/
if( ! is_admin() ) {
return;
};
global $custom_taxonomies, $custom_post_type;
$custom_post_type = \'_custom_post_type_\'; // set your custom post type
$custom_taxonomies = [ \'_first_custom_taxonomy_\', \'_second_custom_taxonomy_\' ]; // set your custom taxonomies
/**
* Disable title input
* @link https://developer.wordpress.org/reference/hooks/enter_title_here/
*/
add_action( \'admin_head\', function() {
global $custom_post_type, $custom_taxonomies;
if( get_post_type() !== $custom_post_type ) {
return;
};
echo "<script type=\'text/javascript\'>
jQuery( document ).ready( function( $ ) {
$( \'#title\' ).css( \'cursor\', \'not-allowed\' ).attr( \'readonly\', true ).attr( \'disabled\', true );
var term = $( \'input[name^=\\"tax_input\\"]:checkbox\' );
term.change( function() {
$( \'#publish, #save-post\' ).prop( \'disabled\', term.filter( \':checked\' ).length < 1 );
} );
term.change();
} ); </script>";
} );
/**
* Replace the default placeholder
* @link https://developer.wordpress.org/reference/hooks/enter_title_here/
*/
add_filter( \'enter_title_here\', function( $placeholder ) {
global $custom_post_type, $custom_taxonomies;
if( get_post_type() !== $custom_post_type ) {
return;
};
$placeholder = "Title automatically generated";
return $placeholder;
} );
/**
* Fires once a post has been saved
* @link https://developer.wordpress.org/reference/hooks/save_post/
*/
add_action( \'save_post\', \'automatically_generated_title\', 10, 2 );
function automatically_generated_title( $post_id, $post ) {
/**
* Prevent function if custom the post type isn\'t matching
* ...or if the post is published as auto-draft
* @link https://wordpress.stackexchange.com/a/218291/190376
* @link https://wordpress.stackexchange.com/a/217101/190376
*/
global $custom_post_type, $custom_taxonomies;
if ( $post->post_type != $custom_post_type || $post->post_status == \'auto-draft\' ) {
return;
};
/**
* Prevent function on auto save
* @link https://wordpress.stackexchange.com/a/218291/190376
*/
if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
return;
};
/**
* Prevent title from updating on post update
* @link https://wordpress.stackexchange.com/a/177670/190376
*/
$created = new DateTime( $post->post_date_gmt ); // get post publication date
$modified = new DateTime( $post->post_modified_gmt ); // get post modification date
$delta = $created->diff( $modified ); // calculate delta between $created and $modified
$delay = ( ( ( $delta->y * 365.25 + $delta->m * 30 + $delta->d ) * 24 + $delta->h ) * 60 + $delta->i ) * 60 + $delta->s; // handle the processing when it begins at the end of a given second, the post_date_gmt and the post_modified_gmt might be different
if( $delay <= 1 ) {
/**
* wp_get_object_terms instead of get_the_terms to retrieve an array() of taxonomies
* @link https://developer.wordpress.org/reference/functions/wp_get_object_terms/
*/
$terms = join( \'\', wp_list_pluck( wp_get_object_terms( $post_id, $custom_taxonomies ), \'name\' ) );
$self = str_replace( \'-\', \'\', strtoupper( get_post_type() . $terms . $post_id ) ); // set your title structure
/**
* Prenvent infinite loop case
* @link https://developer.wordpress.org/reference/hooks/save_post/#avoiding-infinite-loops
*/
remove_action( \'save_post\', \'automatically_generated_title\' ); // unhook this function so it doesn\'t loop infinitely
/**
* Update a post with new post data
* @link https://developer.wordpress.org/reference/functions/wp_update_post/
* Parameters based on wp_insert_post @see https://developer.wordpress.org/reference/functions/wp_insert_post/#parameters
*/
wp_update_post( array(
\'ID\' => $post_id,
\'post_title\' => $self,
\'post_name\' => $self,
) ); // update the post, which calls save_post again
add_action( \'save_post\', \'automatically_generated_title\', 10, 2 ); // re-hook this function
};
};
} ); ?>
标题将在首次发布提交时自动设置。然后,它将忽略对帖子所做的任何更新,以保持一致的slug。通过删除中间的代码,可以轻松地重写该行为
if( $delay <= 1 ) { ...
, 将其移到语句之外,并删除以下行,包括
if( $delay <= 1 ) { ...
自身:
$created = new DateTime( $post->post_date_gmt );
$modified = new DateTime( $post->post_modified_gmt );
$delta = $created->diff( $modified );
$delay = ( ( ( $delta->y * 365.25 + $delta->m * 30 + $delta->d ) * 24 + $delta->h ) * 60 + $delta->i ) * 60 + $delta->s;