在wordpress中将产品图片重命名为产品标题

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

我正在尝试重命名我的 woocommerce 商店中的图像。 他们正在以 123456.png 或 main.jpg 等名称上传到库中。我的目标是使用 php 片段使图像名称、描述和替代文本与产品标题相同。我发现在wordpress的媒体库中有一个名为“上传到”的字段,它是图像附加的后期产品,我想保留该产品的标题并将其设置为我的图像中的标题,as此处显示。但我不知道如何获取 wordpress“上传到”值并将其用作图像中的名称。

经过一番研究,我发现了这个问题(Wordpress 根据 post_title 重命名上传的图像),但代码对我不起作用。这是我尝试使用的代码。我还尝试了一些插件和其他代码,但它们并没有帮助我找到解决方案。我是 php 新手,如果有人可以帮助我,我会很高兴!

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

    } 
}
php wordpress woocommerce file-upload
2个回答
0
投票

我在 MySQL 中为每个可能需要它的人找到了一个解决方案!

图片标题:

UPDATE wp_posts AS attachments
JOIN wp_posts AS parent_posts ON attachments.post_parent = parent_posts.ID
SET attachments.post_title = parent_posts.post_title, attachments.guid = REPLACE(attachments.guid, SUBSTRING_INDEX(attachments.guid, '/', -1), parent_posts.post_name)
WHERE attachments.post_type = 'attachment';

图片描述

UPDATE wp_posts AS attachments
JOIN wp_posts AS parent_posts ON attachments.post_parent = parent_posts.ID
SET attachments.post_content = CONCAT(parent_posts.post_title, ' ', parent_posts.post_excerpt)
WHERE attachments.post_type = 'attachment';

图像的替代文本

UPDATE wp_postmeta AS alt_text
JOIN wp_posts AS attachments ON alt_text.post_id = attachments.ID
JOIN wp_posts AS parent_posts ON attachments.post_parent = parent_posts.ID
SET alt_text.meta_value = parent_posts.post_title
WHERE attachments.post_type = 'attachment' AND alt_text.meta_key ='_wp_attachment_image_alt';

-1
投票

我们如何添加此代码?

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