添加此代码段的主要原因都是关于SEO的。我有时为之创建网站的人往往对WP不太了解,往往会照样上传他们的东西。由于这些网站通常是与媒体相关的网站,我试图通过自动重命名上传的内容来帮助他们DSC_0010.JPG
到my-optimized-post-title-001.JPG
, 等
我目前正在使用this 上载期间重命名文件的代码段。
add_filter( \'wp_handle_upload_prefilter\', \'my_modify_uploaded_file_names\', 1, 1 );
function my_modify_uploaded_file_names( $image_name ) {
// get the parent post id, if there is one
if ( isset( $_GET[\'post_id\'] ) ) {
$post_id = $_GET[\'post_id\'];
} elseif ( isset( $_POST[\'post_id\'] ) ) {
$post_id = $_POST[\'post_id\'];
}
// only do this if we got the post id, otherwise they\'re probably in the media section rather than uploading an image from a post
if ( is_numeric( $post_id ) ) {
// get the post slug
$post_obj = get_post( $post_id );
$post_slug = $post_obj->post_name;
// if we found a slug
if ( $post_slug ) {
$random_number = rand( 10000, 99999 );
$image_name[\'name\'] = $post_slug . \'-\' . $random_number . \'.jpg\';
}
}
return $image_name;
}
然而,当
WP_DEBUG
设置为true时,会出现以下错误:
Notice: Undefined variable: post_id in /home/lorem/public_html/clients/ipsum/wp-content/plugins/myplugin/test.php on line 18
Warning: Cannot modify header information - headers already sent by (output started at /home/lorem/public_html/clients/ipsum/wp-content/plugins/myplugin/test.php:504) in /home/lorem/public_html/clients/ipsum/wp-includes/pluggable.php on line 864
从与人们相关的问题来看,它缺少
$_GET
变量。一些我自己无法解决的问题。。。
是否有人知道如何让这个脚本再次工作?(或者知道类似的解决方案?)
最合适的回答,由SO网友:kaiser 整理而成
发送标头
以下错误仅表示错误消息是直接输出的(发送标头)
Warning: Cannot modify header information - headers already sent by (output started at /home/lorem/public_html/clients/ipsum/wp-content/plugins/myplugin/test.php:504) in /home/lorem/public_html/clients/ipsum/wp-includes/pluggable.php on line 864
PHP错误通知
Notice: Undefined variable: post_id in /home/lorem/public_html/clients/ipsum/wp-content/plugins/myplugin/test.php on line 18
$_REQUEST
是很多东西的组合,它也从
$_POST
和
$_GET
. 所以我要做的第一件事就是
if/else
语句并将其替换为简单
$_REQUEST[\'post_id\']
.
其他一些想法我理解,这个片段背后的想法是,你也可以修改那些刚刚选中的图像,而不是专门为那篇文章上传的图像。否则我不明白为什么这一切都是必要的。因此,我会尝试以下方法:
function modify_uploaded_file_names( $image )
{
// Use part of the post or user object to rename the image
get_currentuserinfo();
global $post, $current_user;
$random_number = rand( 10000, 99999 );
// only do this if we got the post id,
// otherwise they\'re probably in the media section
// rather than uploading an image from a post
if ( isset( $_REQUEST[\'post_id\'] ) )
{
// get the ID
$post_id = absint( $_REQUEST[\'post_id\'] );
// get the post OBJECT
$post_obj = get_post( $post_id );
// get the post slug
$post_slug = $post_obj->post_name;
switch( $image[\'type\'] )
{
case \'image/jpeg\' :
$suffix = \'jpg\';
break;
case \'image/png\' :
$suffix = \'png\';
break;
case \'image/gif\' :
$suffix = \'gif\';
break;
}
// if we found a slug
if ( $post_slug )
$image[\'name\'] = "{$post_slug}-{$random_number}.{$suffix}";
}
else
{
$image_name = str_place( \' \', \'-\', strtolower( $current_user->data->user_nicename ) );
$image[\'name\'] = "{$image_name}-{$random_number}.jpg";
}
return $image;
}
// Only one arg, so 4th attr not needed - Priority set to later 20
add_filter( \'wp_handle_upload_prefilter\', \'my_modify_uploaded_file_names\', 20 );