• Hi Nikolas

    Been trying to poke around to find a way to run a javascript function on completing a file upload. Is a simple way that I’m not seeing ??

    All the best

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author nickboss

    (@nickboss)

    Hi

    The plugin provides filters that run right after the upload. Here is a list of them.

    Check wfu_after_upload filter. You can set custom Javascript code inside $changable_data["js_script"] variable that will run right after the upload.

    You can add this filter inside functions.php file of your theme.

    Regards

    Nickolas

    • This reply was modified 4 years, 3 months ago by nickboss.
    Thread Starter masdaa

    (@masdaa)

    Thanks a lot – works great 🙂

    Reading trough the different filters, and wondering if it would be possible to rename the file using the user data field from the $additional_data ?

    All the best

    Plugin Author nickboss

    (@nickboss)

    Hi, yes you can do this using wfu_before_file_check filter. Here is an example script which uses the 2nd and 3rd userdata field (their inex is 1 and 2 respectively) to change the filename:

    if (!function_exists('wfu_before_file_check_handler')) {
    	function wfu_before_file_check_handler($changable_data, $additional_data) {
    		$filepath = wfu_basedir($changable_data["file_path"]);
    		$fileext = wfu_fileext($changable_data["file_path"], true);
    		$value1 = sanitize_file_name(trim($changable_data["user_data"][1]["value"]));
    		$value2 = sanitize_file_name(trim($changable_data["user_data"][2]["value"]));
    		if ( $value1 != "" && $value2 != "" )
    			$changable_data["file_path"] = $filepath.$value1."-".$value2."-".time().$fileext;
    		return $changable_data;
    	}
    	add_filter('wfu_before_file_check', 'wfu_before_file_check_handler', 10, 2); 
    }

    Regards

    Nickolas

    Thread Starter masdaa

    (@masdaa)

    Yeah I played around with that a little, but can’t get it working. No matter what the file name does not change. Using this code:

    function wfu_before_file_check_handler($changable_data, $additional_data) {
    $filename = wfu_basename($changable_data[‘file_path’]);
    $filepath = wfu_basedir($changable_data[‘file_path’]);
    $changable_data[‘file_path’] = $filepath.’-‘.$changable_data[‘user_data’][0][‘value’];
    return $changable_data;
    }

    The uploader only have one user data input

    • This reply was modified 4 years, 3 months ago by masdaa.
    • This reply was modified 4 years, 3 months ago by masdaa.
    • This reply was modified 4 years, 3 months ago by masdaa.
    Plugin Author nickboss

    (@nickboss)

    Try this one:

    function wfu_before_file_check_handler($changable_data, $additional_data) {
    	$filename = wfu_basename($changable_data['file_path']);
    	$filepath = wfu_basedir($changable_data['file_path']);
    	$fileext = wfu_fileext($changable_data['file_path], true);
    	$changable_data['file_path'] = $filepath.$changable_data['user_data'][0]['value'].'.'.$fileext;
    	return $changable_data;
    }

    Nickolas

    Hi, I’ve tried this solution:

    function wfu_before_file_check_handler($changable_data, $additional_data) {
    $filename = wfu_basename($changable_data[‘file_path’]);
    $filepath = wfu_basedir($changable_data[‘file_path’]);
    $fileext = wfu_fileext($changable_data[‘file_path], true);
    $changable_data[‘file_path’] = $filepath.$changable_data[‘user_data’][0][‘value’].’.’.$fileext;
    return $changable_data;
    }

    but every file I upload its renamed to “unnamed-file”
    Any ideas?
    Thanks in advance

    Plugin Author nickboss

    (@nickboss)

    Is there a user field inside your upload form?

    Regards

    Nickolas

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Run script on completed upload’ is closed to new replies.