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

知更鸟 WordPress1 220阅读模式

WordPress上传媒体文件会自动以当前时间为时间戳,如想自定义这个时间戳,可以将下面的代码添加添加到当前主题函数模板 functions.php 中:

function zm_custom_upload_time() {
	// 获取自定义上传时间
	$custom_time = strtotime('2019-01-01 00:00:00'); 
	return $custom_time;
}

add_filter( 'wp_insert_attachment_data', function( $data, $postarr ) {
	$custom_time = zm_custom_upload_time();  
	// 设置自定义时间
	$data['post_date'] = date( 'Y-m-d H:i:s', $custom_time );
	return $data;
}, 10, 2 );

之后,上传媒体文件会以设定的时间为时间戳。文章源自知更鸟-https://zmingcx.com/customize-wordpress-media-file-upload-times.html

另外,想自定义媒体文件路径,可以参考:文章源自知更鸟-https://zmingcx.com/customize-wordpress-media-file-upload-times.html

自定义WordPress媒体文件上传时间
默认WordPress自定义媒体上传路径wp-content\uploads并以年和月文件夹归档,如果想修改这个默认的存放目...
20846

 文章源自知更鸟-https://zmingcx.com/customize-wordpress-media-file-upload-times.html 文章源自知更鸟-https://zmingcx.com/customize-wordpress-media-file-upload-times.html

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
    • 云图
      云图 0

      非常实用,感谢分享

    匿名

    发表评论

    匿名网友 填写信息

    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    确定