WordPress自定义媒体文件上传时间的方法

追格_咖小啡/ 2023年07月04日/ WordPress/ 浏览 837

WordPress上传媒体文件(图片/音视频等)会自动默认以当前时间为时间戳。若我们想自定义这个时间戳,不妨看看追格小编如何操作的。


相关推荐:
WordPress怎么自定义媒体库上传路径


function zhuige_custom_upload_time() {
	// 获取自定义上传时间
	$custom_time = strtotime('2023-07-04 00:00:00'); 
	return $custom_time;
}
 
add_filter( 'wp_insert_attachment_data', function( $data, $postarr ) {
	$custom_time = zhuige_custom_upload_time();  
	// 设置自定义时间
	$data['post_date'] = date( 'Y-m-d H:i:s', $custom_time );
	return $data;
}, 10, 2 );


我们只需将上面代码,添加到当前使用的WordPress模版的functions.php 中即可。


这样,我们后续再上传媒体文件的时候,即以设定的时间为时间戳。

发表评论

暂无评论,抢个沙发...

客服 工单