return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>Rename image file by post title
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Rename image file by post title

programmeradmin2浏览0评论

Best regards, I want to change the name of the automatic image with the title of the post.

For example, I have the image called 1231231252345.jpg and Title of the article is "Article for a test", I want to change the name of the image in "article-for-a-test.jpg"

Best regards, I want to change the name of the automatic image with the title of the post.

For example, I have the image called 1231231252345.jpg and Title of the article is "Article for a test", I want to change the name of the image in "article-for-a-test.jpg"

Share Improve this question edited Apr 9, 2019 at 13:17 norman.lol 3,2313 gold badges30 silver badges35 bronze badges asked Apr 9, 2019 at 12:36 Panait Andrei AlexandruPanait Andrei Alexandru 637 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 6

I think what you're looking for is the filter: wp_handle_upload_prefilter.

From the Codex:

The single parameter, $file, represent a single element of the $_FILES array. The wp_handle_upload_prefilter provides you with an opportunity to examine or alter the filename before the file is moved to its final location.

Example code from the Codex:

add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );

function custom_upload_filter( $file ){
    $file['name'] = 'wordpress-is-awesome-' . $file['name'];
    return $file;
}

How to use this hook

add_filter( 'wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file ) {
    if ( ! isset( $_REQUEST['post_id'] ) ) {
        return $file;
    }
    $id           = intval( $_REQUEST['post_id'] );
    $parent_post  = get_post( $id );
    $post_name    = sanitize_title( $parent_post->post_title );
    $file['name'] = $post_name . '-' . $file['name'];
    return $file;
}

Found this article but unable to put it on place: https://wordpress/support/topic/how-to-auto-rename-based-on-current-post-title-a-media-defined-as-featured-image/

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论