Using WordPress ‘pre-upload-ui’ PHP action

The pre-upload-ui WordPress action fires just before the legacy (pre-3.5.0) upload interface is loaded.

Usage

add_action('pre-upload-ui', 'your_custom_function');

function your_custom_function() {
    // your custom code here
}

Parameters

  • None

More information

See WordPress Developer Resources: pre-upload-ui

Examples

Display a notice before the upload interface

Display a custom message above the legacy upload interface.

add_action('pre-upload-ui', 'display_notice_before_upload');

function display_notice_before_upload() {
    echo '<strong>Note:</strong> This is the legacy upload interface.';
}

Redirect users to a custom upload page

Redirect users to a custom upload page instead of using the legacy upload interface.

add_action('pre-upload-ui', 'redirect_to_custom_upload_page');

function redirect_to_custom_upload_page() {
    wp_redirect('https://yourwebsite.com/custom-upload-page/');
    exit;
}

Add custom styles to the upload interface

Inject custom styles into the legacy upload interface.

add_action('pre-upload-ui', 'add_custom_styles_to_upload_interface');

function add_custom_styles_to_upload_interface() {
    echo '<style>.your-custom-class { color: red; }</style>';
}

Add custom JavaScript to the upload interface

Inject custom JavaScript into the legacy upload interface.

add_action('pre-upload-ui', 'add_custom_js_to_upload_interface');

function add_custom_js_to_upload_interface() {
    echo '<script>console.log("Legacy upload interface");</script>';
}

Perform a check before showing the upload interface

Perform a custom check before displaying the legacy upload interface. If the check fails, show a message and prevent the interface from being displayed.

add_action('pre-upload-ui', 'perform_custom_check');

function perform_custom_check() {
    if (!current_user_can('upload_files')) {
        echo '<strong>Error:</strong> You do not have permission to upload files.';
        exit;
    }
}