将上传时的图片名称替换为前端表单上的新帖子名称

时间:2019-11-29 作者:bpy

我正在使用此表单替换前端表单上的特色图像和帖子标题:

<form id="featured_upload" name="featured_upload" method="post" action="#" enctype="multipart/form-data">
<p><label for="title">Title</label><input type="text" id="title" value="<?php echo $post_to_edit->post_title; ?>" size="45" name="title" /></p>
<input type="file" name="my_image_upload" id="my_image_upload" multiple="false" required />
<p><input type="hidden" name="post_id" id="post_id" value="" /></p>
<?php wp_nonce_field( \'my_image_upload\', \'my_image_upload_nonce\' ); ?>
<input id="" name="" type="submit" value="Upload"/>
</form>

Here\'s the saving process

// Check the nonce is valid, and the user can edit this post.
if ( isset( $_POST[\'my_image_upload_nonce\'], $_POST[\'post_id\'] ) 
    && wp_verify_nonce( $_POST[\'my_image_upload_nonce\'], \'my_image_upload\' )
    && current_user_can( \'manage_options\', $_POST[\'post_id\'] )) {
    include(\'wp-admin/includes/image.php\' );
    include(\'wp-admin/includes/file.php\' );
    include(\'wp-admin/includes/media.php\' );
    // Let WordPress handle the upload.

    $attachmentID = get_post_thumbnail_id();                            
    $attachment_path = get_attached_file( $attachmentID);                   
    //Delete attachment from database only, not file
    $delete_attachment = wp_delete_attachment($attachmentID, true);
    //Delete attachment file from disk
    $delete_file = unlink($attachment_path);
    //Form attachment Field name.
    $file_handler = \'my_image_upload\'; 
    $attach_id = media_handle_upload( $file_handler, $_POST[\'post_id\'] );

    //changing the post title
    if(!empty($_POST[\'title\'])) {
    //$new_title = sanitize_title($_POST[\'title\']);
    $new_slug = sanitize_title($_POST[\'title\']);        
    $my_post = array(\'post_title\' => $_POST[\'title\'],\'post_name\'  => $new_slug);
    // Update new post title into the database
    wp_update_post( $my_post );

The problem:

我需要上传的图片与新的帖子标题一起保存($_POST[\'title\'])

Example:

所选图像为image1.jpg

新职位名称为renamed post (就像在$_POST[\'title\'])

在上载过程中,image1.jpg 应重命名为renamed-post.jpg

1 个回复
最合适的回答,由SO网友:Chetan Vaghela 整理而成

您可以使用sanitize_file_name 筛选以重命名文件。将此代码放入活动主题的函数中。php,如果仅获取title 要求我已经测试了这段代码,它工作得很好。职位名称:https://prnt.sc/q3thzw已上载重命名的图像:https://prnt.sc/q3tib7

function make_filename_as_post_name($filename) {
    $info = pathinfo($filename);
    $ext  = empty($info[\'extension\']) ? \'\' : \'.\' . $info[\'extension\'];
    $name = basename($filename, $ext);
    $new_name = $_REQUEST[\'title\'];
    if(!empty($new_name)){
        # Replace space with dash 
        $new_name = preg_replace(\'/\\s+/\', \'-\', $new_name);
        return $new_name . $ext;
    }else{
        return $name . $ext;
    }
}
add_filter(\'sanitize_file_name\', \'make_filename_as_post_name\', 10);
如果这对你有用,请告诉我!

相关推荐

Product-attribute-slug-is-too-long-28-characters-max

谷歌没有解决我的问题,所以。。这里的任何人都知道如何增加woocommerce产品属性slug的字符限制,默认情况下它的字符限制为28个(数据库中最多32个),我需要最多40个。(试图增加到最大128)。我正在测试一个本地站点,其中产品信息来自导入的xml文件,有时某些属性名称的长度刚刚超过28个字符。。将数据库中的相应列从vachar(32)更改为varchar(128),试图更改中的值wc-attribute-functions.php, 将28改为128,但这没有帮助。。// Validate sl