• Hi,

    I’m stucked for 4 days right now. I have created a little plugin in which one feature is the ability to change the upload folder. Everything is working as expected for already saved post since we can $_POST to retrieve the different values which makes the folder structure… but I want to cover people which uploads files in an unsaved post.

    For this, I have created a JS file which retrieves the value of the title field each 5 seconds. This is processed by ajax btw, and the JS itself is working fine. The main problem I’m encoutering is that I’m unable to grab this value and put it into another hook : my upload filter.

    Here are some details :

    1 – My script

    setInterval( function() {
        var title = $('#post-title-0').val();
        jQuery.post(
        	ajaxurl,
        	{
                'action': 'my_action',
                'param': title
            },
            function(response){
            	console.log(response);
            }
        );
    }, 5000);

    2 – I include a wp-localize script in order to be sure that ajax files will be available

    function add_js_scripts() {
    	// pass Ajax Url to script.js
    	wp_localize_script('script', 'ajaxurl', admin_url( 'admin-ajax.php' ) );
    }
    add_action('admin_enqueue_scripts', 'add_js_scripts');

    3 – This is the related ajax hook

    add_action( 'wp_ajax_my_action', 'my_action' );
    
    function my_action() {
        $output=$_POST['param'];
        if($output){
            print_r($output);
        }
        else{
            echo ' error : ' . $output;
         }
         die;
    }

    ===> With all this, I’m able to grab the actual title value. But I’m nevertheless unable to pick this value in order to put it in my upload filter. It’s kinda like a scope issue : the jQuery is able to grab the proper data, the Ajax response is at 200:OK in my browser inspector, I can see the 2 values of “action” and “param”, but can’t get the ‘param’ value in my wp_handle_upload_prefilter/wp_handle_upload.

    Here’s a simplified version of my wp_handle_upload_prefilter/wp_handle_upload filter :

    function my_custom_upload_dir($path) {
        $mydir .= '/'.$_POST['param'];
        $path['subdir'] = $mydir.'/';
        $path['path']   = $path['basedir'].$mydir;
        $path['url']    = $path['baseurl'].$mydir;
    
        return $path;
    }
    
    // Filters which handle the new directory
    add_filter('wp_handle_upload_prefilter', 'my_upload_prefilter');
    add_filter('wp_handle_upload', 'my_handle_upload');
    
    function my_upload_prefilter( $file ) {
        add_filter('upload_dir', 'my_custom_upload_dir');
        return $file;
    }
    
    function my_handle_upload( $fileinfo ) {
        remove_filter('upload_dir', 'my_custom_upload_dir');
        return $fileinfo;
    }

    I really need some help, I’m stucked for too much time. Thanks in advance.

    PS : I tried a JSON solution too, but doesn’t work too ?? I have created a proper route, but it returns an empty array inside the upload filter. I’m disapointed…

    • This topic was modified 5 years, 8 months ago by moxymore.
    • This topic was modified 5 years, 8 months ago by moxymore.
Viewing 4 replies - 1 through 4 (of 4 total)
  • There is not a running instance of WordPress on the server. It only runs when you invoke it. So getting the title all the time and sending it to the server is pointless. The upload code only runs when the upload happens. The two requests are separate and don’t share any memory.

    If you really need the title of the post (Why?), you can use the click on the upload to get the title and put it in a new parameter that is sent with the upload request or use ajax to store it in a transient that is then used in your upload filter.

    Thread Starter moxymore

    (@moxymore)

    Hi Joy and thanks for your answer. I need the post title because it’s a part of the new upload directory. Moreover, when you create a new post, and before it’s saved for the first time, there’s no way to get this title with a standard $_POST. That’s why I expected to cover this case, by taking in real time what is written in the title field.

    Regarding what you said, do you mean that there is an action hook for the upload button in which I can pass a parameter in it? Which one is this? I’m interested.

    Regards.

    It doesn’t make much sense to use the title for the upload folder, since the title can be changed at any time and you wouldn’t want to have to search all the posts to change the folder that might be referencing uploads in that post.

    What I was referring to was not an action hook (that is in PHP). I meant a javascript event that runs when the upload is started. It could get the title from the data and put it in a new field in the POST request.

    You could also just wait until the post is saved, and move the files (and update their meta data), because you can have a draft without a title and you can publish without a title, but you can’t publish with no title and no content.

    Moderator bcworkz

    (@bcworkz)

    I can imagine reasons to have unique upload folders that have human readable names. Like Joy points out, titles can change, but you don’t want to rename folders along with any change. If you’re going to use post titles for upload folder names, you should save the folder name used in post meta and rely upon that name to determine the upload folder every time regardless of what the current title is.

    Your upload filter thus would need the actual folder name or the ID of the post where the name is stored. I’m unsure of your overall scheme. Perhaps you could place such data in a hidden form field with the file upload field. It’ll then be available when the form is submitted. Otherwise one of the other ways of passing values could be used. Transients like Joy mentioned, session variables, or cookies. The best choice depends on your scheme and how long the data needs to persist.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Unable to pass an available variable in an ajax hook into another hook’ is closed to new replies.