Wordpress 根据 post_title 重命名上传的图像 [英] Wordpress rename uploaded images based on the post_title

查看:48
本文介绍了Wordpress 根据 post_title 重命名上传的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来告诉 wordpress 以帖子的名称重命名附加到帖子的图片.

我发现这个

解决方案

此代码适用于帖子类型(帖子、页面、产品).将文件名和广告替代、标题、说明和描述元更改为图像.

但是当它是关于属性时,类别保留文件名并将其添加到 alt、标题等.

function file_renamer( $filename ) {$info = pathinfo( $filename );$ext = empty( $info['extension'] ) ?'' : '.'.$信息['扩展名'];$name = basename( $filename, $ext );if( $post_id = array_key_exists("post_id", $_POST) ? $_POST["post_id"] : null) {if($post = get_post($post_id)) {返回 $post->post_title .$ext;}}$my_image_title = $post;$file['name'] = $my_image_title .- uniqid() .$ext;//uniqid 方法//$file['name'] = md5($name) .$ext;//md5 方法//$file['name'] = base64_encode($name) .$ext;//base64 方法返回 $ 文件名;}add_filter('sanitize_file_name', 'file_renamer', 10, 1);/* 自动设置图片标题、替换文字、标题和;上传时说明---------------------------------------------------------------------------------------*/add_action('add_attachment', 'my_set_image_meta_upon_image_upload');函数 my_set_image_meta_upon_image_upload( $post_ID ) {//检查上传的文件是否是图片,否则什么都不做如果 ( wp_attachment_is_image( $post_ID ) ) {//获取父帖子ID,如果有的话if( isset($_REQUEST['post_id']) ) {$post_id = $_REQUEST['post_id'];} 别的 {$post_id = false;}如果($post_id != 假){$my_image_title = get_the_title($post_id);} 别的 {$my_image_title = get_post( $post_ID )->post_title;}//清理标题:删除连字符、下划线 &额外的空格:$my_image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ', $my_image_title );//清理标题:将每个单词的第一个字母大写(其他字母小写):$my_image_title = ucwords( strtolower( $my_image_title ) );//创建一个包含要更新的图像元(标题、标题、描述)的数组//注意:如果不需要,注释掉摘录/标题或内容/描述行$my_image_meta = 数组('ID' =>$post_ID,//指定要更新的图片(ID)'post_title' =>$my_image_title,//将图片标题设置为已清理的标题'post_excerpt' =>$my_image_title,//将图像标题(摘录)设置为经过处理的标题'post_content' =>$my_image_title,//将图像描述(内容)设置为经过处理的标题);//设置图片Alt-Textupdate_post_meta( $post_ID, '_wp_attachment_image_alt', $my_image_title );//设置图像元(例如标题、摘录、内容)wp_update_post( $my_image_meta );}}

我试图找到一种方法从属性或类别名称中获取标题以将其添加到元数据中,但没有成功.如果您有任何想法,我将不胜感激.我对 WordPress codex 不太好.

希望此代码有帮助.

I'm looking for a way to tell wordpress to rename the images attached to the post, by the name of the post.

I found this plugin to do the trick but.. it's stupid to use a plugin for such a simple function. . So I found this code in this article

/* Automatically set the image Title, Alt-Text, Caption & Description upon upload
--------------------------------------------------------------------------------------*/
add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );
function my_set_image_meta_upon_image_upload( $post_ID ) {

    // Check if uploaded file is an image, else do nothing

    if ( wp_attachment_is_image( $post_ID ) ) {

        $my_image_title = get_post( $post_ID )->post_title;

        // Sanitize the title:  remove hyphens, underscores & extra spaces:
        $my_image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ',  $my_image_title );

        // Sanitize the title:  capitalize first letter of every word (other letters lower case):
        $my_image_title = ucwords( strtolower( $my_image_title ) );

        // Create an array with the image meta (Title, Caption, Description) to be updated
        // Note:  comment out the Excerpt/Caption or Content/Description lines if not needed
        $my_image_meta = array(
            'ID'        => $post_ID,            // Specify the image (ID) to be updated
            'post_title'    => $my_image_title,     // Set image Title to sanitized title
            'post_excerpt'  => $my_image_title,     // Set image Caption (Excerpt) to sanitized title
            'post_content'  => $my_image_title,     // Set image Description (Content) to sanitized title
        );

        // Set the image Alt-Text
        update_post_meta( $post_ID, '_wp_attachment_image_alt', $my_image_title );

        // Set the image meta (e.g. Title, Excerpt, Content)
        wp_update_post( $my_image_meta );

    } 
}

and another one here

function file_renamer( $filename ) {
    $info = pathinfo( $filename );
    $ext  = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
    $name = basename( $filename, $ext );
    if( $post_id = array_key_exists("post_id", $_POST) ? $_POST["post_id"] : null) {
        if($post = get_post($post_id)) {
            return $post->post_title . $ext;
        }
    }
    get_currentuserinfo();

    return   $current_user->user_login . $ext;
  }
add_filter( 'sanitize_file_name', 'file_renamer', 10, 1 );

both work but how do you combine them into one code?

what is the best way to deal with this problem without using a plugin?

解决方案

This code works with post types (posts, pages, products). Change filename and ads alt, title, caption and description meta to image.

But when its about attributes, categories keeps the filename and add it to alt, title, etc.

function file_renamer( $filename ) {
    $info = pathinfo( $filename );
    $ext  = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
    $name = basename( $filename, $ext );


    if( $post_id = array_key_exists("post_id", $_POST) ? $_POST["post_id"] : null) {
        if($post = get_post($post_id)) {
            return $post->post_title . $ext;
        }
    }
    
    $my_image_title = $post;
    

    $file['name'] = $my_image_title  . - uniqid() . $ext; // uniqid method
    // $file['name'] = md5($name) . $ext; // md5 method
    // $file['name'] = base64_encode($name) . $ext; // base64 method

    return $filename;


  }
add_filter( 'sanitize_file_name', 'file_renamer', 10, 1 );

/* Automatically set the image Title, Alt-Text, Caption & Description upon upload
--------------------------------------------------------------------------------------*/
add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );
function my_set_image_meta_upon_image_upload( $post_ID ) {

    // Check if uploaded file is an image, else do nothing

    if ( wp_attachment_is_image( $post_ID ) ) {
        
       // Get the parent post ID, if there is one

        if( isset($_REQUEST['post_id']) ) {
          $post_id = $_REQUEST['post_id'];
          } else {
          $post_id = false;
          }


            if ($post_id != false) {
        $my_image_title = get_the_title($post_id);
            } else {
        $my_image_title = get_post( $post_ID )->post_title;
            }
        // Sanitize the title:  remove hyphens, underscores & extra spaces:
        $my_image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ',  $my_image_title );

        // Sanitize the title:  capitalize first letter of every word (other letters lower case):
        $my_image_title = ucwords( strtolower( $my_image_title ) );

        // Create an array with the image meta (Title, Caption, Description) to be updated
        // Note:  comment out the Excerpt/Caption or Content/Description lines if not needed
        $my_image_meta = array(
            'ID'        => $post_ID,            // Specify the image (ID) to be updated
            'post_title'    => $my_image_title,     // Set image Title to sanitized title
            'post_excerpt'  => $my_image_title,     // Set image Caption (Excerpt) to sanitized title
            'post_content'  => $my_image_title,     // Set image Description (Content) to sanitized title
        );

        // Set the image Alt-Text
        update_post_meta( $post_ID, '_wp_attachment_image_alt', $my_image_title );

        // Set the image meta (e.g. Title, Excerpt, Content)
        wp_update_post( $my_image_meta );

    } 
}

I was try to find a way to take the title from attribute or category name to add it to metas but no luck. If you have any idea i do appreciate it. I'm not so good to WordPress codex.

Hope this code help.

这篇关于Wordpress 根据 post_title 重命名上传的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆