覆盖wp_unique_filename

时间:2014-12-19 17:29:34

标签: php wordpress

如何将回调参数传递给WordPress函数?

wp_unique_filename需要3个参数($dir$filename$unique_filename_callback = null)。我无法知道如何通过$unique_filename_callback

我已尝试add_actionadd_filter,每个都有10/3

add_action( 'wp_unique_filename', 'my_unique_filename', 10, 3 );
add_filter( 'wp_unique_filename', 'my_unique_filename', 10, 3 );

然后

function my_unique_filename( $dir, $filename, my_unique_filename_cb ){
}
function my_unique_filename_c(){
}

无论我为此参数添加了什么,我都会得到一个解析错误,它预计会有一个变量。我试过了:

my_unique_filename_cb
'my_unique_filename_cb'
my_unique_filename_cb()
'my_unique_filename_cb()'

更新

我认为它需要作为覆盖传递给wp_handle_upload,它接受​​一系列覆盖,提取它,并在函数后面传递$unique_filename_callback作为参数。在这种情况下,我如何将我的函数作为数组中的参数传递(如果这是解决方案):

function my_wp_handle_upload( $file, array( 'unique_filename_callback' => myfunction ) ){
  //Which would then extract unique_filename_callback to $unique_filename_callback
  //and pass it to wp_unique_filename as the third parameter
}

1 个答案:

答案 0 :(得分:0)

wp_unique_filename()检查是否指定了回调函数,是否存在且可调用。传递的Args是$dir$name$ext。这是一个(无用的)例子:

function my_unique_filename_function( $dir = '', $filename = 'bar.exe' )
{
    echo wp_unique_filename( $dir, $filename, 'my_unique_filename_callback' );
}

function my_unique_filename_callback( $dir, $name, $ext )
{
    return $name . $ext;
}

my_unique_filename_function(); // echoes bar.exe
my_unique_filename_function( '', 'qux.gif' ); // echoes qux.gif

指定wp_unique_filename()使用的wp_handle_upload()的自定义回调(改编为Codex示例):

$uploadedfile = $_FILES['file'];
$upload_overrides = array( 'unique_filename_callback' => 'my_unique_filename_callback' );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
    echo "File is valid, and was successfully uploaded.";
    var_dump( $movefile);
} else {
    echo "Possible file upload attack!";
}