• Resolved Chris Huff

    (@brochris)


    I’m calling my plugin’s function via a filter (wp_handle_upload_prefilter), so I can’t just directly write a notice as output, correct? At least, when I tried doing that, it interrupted WordPress and would not continue processing the upload.

    I’m currently trying to nest a function within the main function, which actually works slightly better, but still doesn’t seem to allow WordPress to continue processing the upload either (it just uploads it, then gets stuck on “Crunching”). So how can I write a notice or error to the user?

    Here’s some relevant code I’m working on.

    function aou_upload_file($file) {
    
        $file_no_ext = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file['name']);
    
        // Determine if the file can be uploaded
        if (strpos($file['type'],'video') !== false) {
            $mediatype = 'movies';
            }
        elseif (strpos($file['type'],'audio') !== false) {
            $mediatype = 'audio';
            }
        else {
            function aou_not_supported_notice(){
                echo '<div class="error"><p>File type not supported.</p></div>';
                }
            add_action('add_attachment', 'aou_not_supported_notice');
            return $file;
            }
        }
    
    add_filter('wp_handle_upload_prefilter','aou_upload_file');
Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter Chris Huff

    (@brochris)

    I’ve yet to figure this out…any ideas?

    It seems like there should be a way to, instead of writing an error immediately, tell WordPress to write the error at the proper time. Does this exist in some fashion?

    Thread Starter Chris Huff

    (@brochris)

    Even just a nudge in the right direction would be helpful, lol. I’m really at a loss at this point.

    It’s not ideal, but I might just take the error reporting out of the plugin, and release a first version without that part. Although I’m worried if I do, I’ll get lots of questions I can’t answer, but would be able to address very easily if I were able to ask for the error they received.

    Hi,

    This filter is checked in line 240 of /wp-admin/includes/file.php

    Is checks for an array entry with the key error. So instead you could simply add your errors to that key.

    $file['error'] = 'File type not supported.'

    Thread Starter Chris Huff

    (@brochris)

    Thanks for the suggestion. When I tried your code, it then cancels the upload as well because it sees there is an error.

    Basically, I want my plugin to upload the media file to a remote server, but give an error if that upload fails (whether because it’s an unsupported file type, or because the remote server is down, etc), and then continue the upload the normal way if it failed. All of that works fine, except that there’s no way of knowing why it failed unless I can throw a notice back to the user somehow.

    Is there some way to tell WordPress to continue to process an upload, even when there’s an error?

    Thread Starter Chris Huff

    (@brochris)

    I thought of another possible solution. When a file is uploaded normally, it writes the filename back to the screen. Is there any way to filter this line so that it writes not only the filename, but anything else I want it to say?

    I would perhaps try hooking in to the end of the upload process. When a file has finished uploading you run the code that upload to the remote server. If the remote upload fails you can save something to the database, e.g. update_option() and have a hook running that checks for the value of said option e.g. get_option() and then displays the notice. Could this work?

    Thread Starter Chris Huff

    (@brochris)

    What hook could I use at that fires after the file has finished uploading? I’ve been trying to use add_attachment, or edit_attachment, but these actually interrupt the upload and saving process, so that it gets stuck on “Crunching…”

    Attachments are posts in a post type, so a regular save_post would suffice. Be sure to check if the saved post is of the attachment-post type.

    Read more about save_post in the Codex.

    Thread Starter Chris Huff

    (@brochris)

    Really hoped this would work, but it looks like the save_post action isn’t fired when media is first uploaded, only if it is subsequently edited.

    I definitely think that using update_option and get_option is the way to go, though. Maybe I could somehow add javascript to the media-new.php page that checks for the option and displays it if it’s added?

    Thread Starter Chris Huff

    (@brochris)

    It’s not pretty, but I did figure out a method that will work for now.

    I embedded an iframe at the bottom of the page (via admin_footer) that displays any errors logged (via get_option), and it also includes a button to refresh the iframe after you upload files. If I can figure out how to auto-refresh the iframe when a file is uploaded, it would work so much better, but this is good enough for now.

    Thanks for all your help!

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Add admin notices when using WordPress filters’ is closed to new replies.