Wordpress 根据 post_title 重命名上传的图片

问题描述 投票:0回答:2

我正在寻找一种方法来告诉 WordPress 按帖子名称重命名帖子中附加的图像。

我发现这个插件可以做到这一点,但是..使用插件来实现如此简单的功能是愚蠢的。 。所以我在这篇文章

中找到了这段代码
/* 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 );

    } 
}

还有另一个这里

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 );

两者都可以工作,但是如何将它们组合成一个代码?

在不使用插件的情况下处理这个问题的最佳方法是什么?

enter image description here

php wordpress function file-upload elementor
2个回答
3
投票

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

但是当涉及属性时,类别会保留文件名并将其添加到 alt、标题等中。

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 );

    } 
}

我试图找到一种方法从属性或类别名称中获取标题并将其添加到元中,但没有成功。如果您有任何想法,我非常感激。 我不太擅长 WordPress codex。

希望这段代码有帮助。


0
投票

如何以帖子名称重命名但不保存帖子?我的意思是,我创建了一个新帖子,我还没有保存它,并且图像应该重命名为帖子的名称?

© www.soinside.com 2019 - 2024. All rights reserved.