【问题标题】:Wordpress rename uploaded images based on the post_titleWordpress 根据 post_title 重命名上传的图片
【发布时间】:2020-12-13 17:20:17
【问题描述】:

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

我发现这个plugin 可以解决问题,但是.. 使用插件来实现如此简单的功能是愚蠢的。 .所以我在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 );

    } 
}

还有一个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 );

两者都有效,但如何将它们组合成一个代码?

在不使用插件的情况下解决此问题的最佳方法是什么?

【问题讨论】:

    标签: php wordpress function file-upload elementor


    【解决方案1】:

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

    但是当它关​​于属性时,类别会保留文件名并将其添加到 alt、title 等中。

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

    我试图找到一种方法从属性或类别名称中获取标题以将其添加到 metas 但没有运气。如果您有任何想法,我将不胜感激。 我对 WordPress codex 不太擅长。

    希望这段代码有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-21
      • 2019-06-10
      • 2014-05-04
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      相关资源
      最近更新 更多