Using WordPress ‘media_upload_form_url’ PHP filter

The media_upload_form_url WordPress PHP filter allows you to modify the media upload form action URL.

Usage

add_filter('media_upload_form_url', 'my_custom_upload_form_url', 10, 2);

function my_custom_upload_form_url($form_action_url, $type) {
    // your custom code here
    return $form_action_url;
}

Parameters

  • $form_action_url (string) – The media upload form action URL.
  • $type (string) – The type of media. Default ‘file’.

More information

See WordPress Developer Resources: media_upload_form_url

Examples

Append a query parameter to the form action URL

In this example, we’ll append a query parameter my_param to the form action URL.

add_filter('media_upload_form_url', 'append_my_param_to_upload_form_url', 10, 2);

function append_my_param_to_upload_form_url($form_action_url, $type) {
    $form_action_url = add_query_arg('my_param', 'example_value', $form_action_url);
    return $form_action_url;
}

Change the form action URL based on media type

In this example, we’ll change the form action URL depending on the media type being uploaded.

add_filter('media_upload_form_url', 'change_upload_form_url_based_on_media_type', 10, 2);

function change_upload_form_url_based_on_media_type($form_action_url, $type) {
    if ($type == 'image') {
        $form_action_url = 'https://example.com/custom-image-upload-url';
    } elseif ($type == 'audio') {
        $form_action_url = 'https://example.com/custom-audio-upload-url';
    }
    return $form_action_url;
}

Add a subdirectory to the form action URL

In this example, we’ll add a subdirectory uploads to the form action URL.

add_filter('media_upload_form_url', 'add_subdirectory_to_upload_form_url', 10, 2);

function add_subdirectory_to_upload_form_url($form_action_url, $type) {
    $form_action_url = trailingslashit($form_action_url) . 'uploads/';
    return $form_action_url;
}

Modify the form action URL based on user role

In this example, we’ll modify the form action URL based on the current user’s role.

add_filter('media_upload_form_url', 'change_upload_form_url_based_on_user_role', 10, 2);

function change_upload_form_url_based_on_user_role($form_action_url, $type) {
    $current_user = wp_get_current_user();
    if (in_array('administrator', $current_user->roles)) {
        $form_action_url = 'https://example.com/admin-upload-url';
    } else {
        $form_action_url = 'https://example.com/regular-upload-url';
    }
    return $form_action_url;
}

Force the form action URL to use HTTPS

In this example, we’ll force the form action URL to always use HTTPS.

add_filter('media_upload_form_url', 'force_https_in_upload_form_url', 10, 2);

function force_https_in_upload_form_url($form_action_url, $type) {
    $form_action_url = set_url_scheme($form_action_url, 'https');
    return $form_action_url;
}