【问题标题】:Wordpress - upload files and rename with custom user metaWordpress - 上传文件并使用自定义用户元重命名
【发布时间】:2017-09-27 15:42:47
【问题描述】:

当我的用户上传文件时,我需要使用特定的 user_meta 值对其进行重命名。因此,使用wp_handle_upload 我为$upload_overrides 设置了一个回调函数,如下所示:

$upload_overrides   = array( 'test_form' => false, 'unique_filename_callback' => 'change_document_name' );

我的回调函数是

function change_document_name($dir, $name, $ext){
    global $current_user;
    $doc_type = get_user_meta($current_user->ID, 'document_type', true);
    return $doc_type . '_mydoc' . $ext;
}

现在您可以看到,我们正在讨论用户文档,因此我需要根据他们上传的文档类型重命名它们。例如,如果他们上传了“passport”并选择了文档类型(当然),我应该得到user_meta'document_type',将其用作前缀并将其放在前面上传的文件名,输出类似

  • passport_mydoc.pdf

当然我的功能不起作用,我不明白为什么它不采用全局 $current_user 或至少是否有其他方法来完成此操作。

非常感谢。

编辑

为了更好地解释它,我的错,函数change_document_name()确实像这样重命名文件:

  • _mydoc.ext(例如 _mydoc.pdf)

这意味着函数被正确调用并运行,除了第一部分忽略了$doc_type变量。出于这个原因,我认为 $current_user 它不起作用。我上传文件的完整代码如下:

if(!empty($_FILES['docfile'])):
            require_once(ABSPATH . "wp-admin" . '/includes/file.php');
            $upload_overrides   = array( 'test_form' => false, 'unique_filename_callback' => 'change_document_name' );

            add_filter('upload_dir', 'my_user_folder'); //A documents custom folder
            $uploaded_file      = wp_handle_upload($_FILES['docfile'], $upload_overrides);
            remove_filter( 'upload_dir', 'my_user_folder' );

            $doc_file_loc       = $uploaded_file['file'];
            $doc_file_title     = $_FILES['docfile']['name'];
            $doc_file_arr       = wp_check_filetype(basename($_FILES['docfile']['name']));
            $doc_file_type      = $doc_file_arr['type'];
            $doc_file_att   = array(
                'post_mime_type'    => $doc_file_type,
                'post_title'        => addslashes($doc_file_title),
                'post_content'      => '',
                'post_status'       => 'inherit',
                'post_parent'       =>  0,
                'post_author'       => $uid
            );
            require_once(ABSPATH . "wp-admin" . '/includes/image.php');
            $doc_file_id = wp_insert_attachment( $doc_file_att, $doc_file_loc, 0 );
            $doc_file_url = wp_get_attachment_url( $doc_file_id );
            update_user_meta($uid,'document_file', $doc_file_url);
        endif;

钩子'unique_filename_callback'是按照这里的codex使用https://developer.wordpress.org/reference/functions/wp_unique_filename/

【问题讨论】:

  • $upload_overrides 只是一个变量,其中包含您的值数组。显示您实际使用它的代码以查看它是如何注册的。另外,回调函数是否被调用而不是重命名文档?或者它根本没有被调用?需要扩展“不起作用”。哪个部分不工作。
  • @AustinWinstanley 非常感谢您的评论。我的错!我编辑了我的问题,如果还有什么让我们谈谈!谢谢。

标签: php wordpress file-upload


【解决方案1】:

我不确定为什么全局没有返回用户。它应该是。但是试试get_current_user_id() 看看它是否有效:

function change_document_name( $dir, $name, $ext ){
    if ( ! is_user_logged_in() ) {
        error_log( "User not logged in." );

        return;
    }

    $user_id = get_current_user_id();

     // Uncomment to see if there is any value
     // var_dump( $user_id );

    $doc_type = get_user_meta( $user_id, 'document_type', true );

     // Uncomment to see if there is any value
     // var_dump( $doc_type );

    if ( ! $doc_type ) {
        error_log( "There is no doc type set for the current user with id $user_id" );

        return;
    }

    return $doc_type . '_mydoc' . $ext;
}

我在其中添加了一些 var_dumps,您可以使用它来查看返回的值,或者如果您设置了 xdebug,您可以进行调试。但这应该给你你所需要的。如果您不想记录这些错误,也可以删除错误记录。它们在那里,因此您可以检查站点日志并查看其中的内容。

【讨论】:

  • 感谢奥斯汀,非常感谢您的回答。我将对其进行测试并标记您的答案。再次感谢。
猜你喜欢
  • 1970-01-01
  • 2017-04-29
  • 1970-01-01
  • 2016-05-08
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多