0

validateFile function is triggered when user upload image to Media Library.

How do I pass $file to php_vars ? So that I can manipulate the php_vars value inside validation.js

$myArray = array();

add_action('admin_enqueue_scripts', 'registerValidationScript'); 
function registerValidationScript() {
    global $myArray;
    wp_enqueue_script( 'my_js_library', get_template_directory_uri() . '/js/validation.js' );
    wp_localize_script( 'my_js_library', 'php_vars', $myArray);
};

add_filter('wp_handle_upload_prefilter', 'validateFile' );
function validateFile( $file ){
    array_push($myArray, $file);
    return $file;
}

** Edit **

I think this question is bit different with possible duplication.

Reason is, I'm trying to get PHP variable that only available after 'post back'. Meaning, after user upload the picture to Media Library. Hence the usage of wp_handle_upload_prefilter hook.

3
  • 1
    Possible duplicate of Get php var inside javascript file (making plugin) Commented Jan 12, 2018 at 7:47
  • I've updated the question. Commented Jan 12, 2018 at 8:05
  • In that case you can't use the localization. You need to directly interact with the media uploaded. Here is a useful link. Commented Jan 12, 2018 at 10:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.