我正在metabox中创建媒体上载部分。我希望能够做的一件事是将上载到的媒体的文件名重命名为与帖子标题相同的文件名。比如说,现在我上传了一张名为pic001的图片。jpg发帖子:“Test post”,在上传过程中,我想自动将文件名重命名为Test\\u post\\u 1。jpg公司
我看到了以下链接:
Rename files during upload using variables
但是,我不太确定您会将数据发布到哪里?我还找到了另一个链接,它演示了如何重命名文件-类似于此:
function make_new_filename($filename){
$info = pathinfo($filename);
$ext = empty($info[\'extension\']) ? \'\' : \'.\' . $info[\'extension\'];
$name = basename($filename, $ext);
$newname = "test";
return $newname . $ext;
}
add_filter(\'sanitize_file_name\', \'make_new_filename\');
因此,如果有任何想法,我将如何获得标题张贴通过媒体上传。php?
跟进:
我已尝试将数据发送到media\\u上载。php
jQuery(\'#upload_image_button_test\').click(function() {
var postTitleVal = jQuery("#title").val();
if(postTitleVal == \'\'){
jQuery("#title").after(\'<span class="error"> You forgot to enter the title</span>\');
} else{
jQuery(\'html\').addClass(\'Image\');
formfield = jQuery(\'#upload_image_test\').attr(\'name\');
tb_show(\'\', \'media-upload.php?type=image&TB_iframe=true;title=\'+postTitleVal);
console.log(\'media-upload.php?type=image&TB_iframe=true;title=\'+postTitleVal);
}
return false;
});
// user inserts file into post. only run custom if user started process using the above process
// window.send_to_editor(html) is how wp would normally handle the received data
window.original_send_to_editor = window.send_to_editor;
window.send_to_editor = function(html){
if (formfield) {
fileurl = jQuery(\'img\',html).attr(\'src\');
jQuery(\'#upload_image_test\').val(fileurl);
tb_remove();
jQuery(\'html\').removeClass(\'Image\');
} else {
window.original_send_to_editor(html);
}
};
});