How to get ID of Edit User page during wp_handle_upload_prefilter, whilst in Media picker?

I have an Advanced Custom Fields field group on Edit User pages (user-edit.php), containing an Image field (avatar). I aim to rename that uploaded filename to match the username of the on-page user.

My current code uses wp_handle_upload_prefilter to catch the upload, and, to ensure the rename does not happen to all WP file uploads, I think I need to check that the upload field in question is my specific ACF Image…

In the below code, (1) I can successfully get the ACF field key and field for this check (check not fully implemented)…

add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );

function custom_upload_filter( $file ) {

    // 1. GET SOURCE FIELD, COPIED FROM ACF'S MEDIA.PHP

    $field = false;
    // Search for field key within available data.
    // Case 1) Media modal query.
    if ( isset( $_POST['query']['_acfuploader'] ) ) {
        $field_key = (string) $_POST['query']['_acfuploader'];

        // Case 2) Media modal upload.
    } elseif ( isset( $_POST['_acfuploader'] ) ) {
        $field_key = (string) $_POST['_acfuploader'];
    }

    // Attempt to load field.
    // Note the `acf_get_field()` function will return false if not found.
    if ( isset( $field_key ) ) {
        $field = acf_get_field( $field_key );
    }


    // 2. GET USER'S ID AND USERNAME

    // Code here


    // 3. TEST RENAME CODE
    $file['name'] = $user_id . 'and-everything-is-awesome-' . $file['name'];
    return $file;

}

However, I’m struggling to (2) get the ID of the user on the user-edit.php page.

I have tried two methods found here on StackExchange

1. wp_get_current_user

This…

$user_id = (int) $user_id;
$current_user = wp_get_current_user();
if ( ! defined( 'IS_PROFILE_PAGE' ) )
    define( 'IS_PROFILE_PAGE', ( $user_id == $current_user->ID ) );

if ( ! $user_id && IS_PROFILE_PAGE )
    $user_id = $current_user->ID;
elseif ( ! $user_id && ! IS_PROFILE_PAGE )
    wp_die(__( 'Invalid user ID.' ) );
elseif ( ! get_userdata( $user_id ) )
    wp_die( __('Invalid user ID.') );

2. $_GET['user_id']

This…

// If is current user's profile (profile.php)
if ( defined('IS_PROFILE_PAGE') && IS_PROFILE_PAGE ) {
    $user_id = get_current_user_id();
// If is another user's profile page
} elseif (! empty($_GET['user_id']) && is_numeric($_GET['user_id']) ) {
    $user_id = $_GET['user_id'];
// Otherwise something is wrong.
} else {
    die( 'No user id defined.' );
}

But neither result in a user_id being located. It is always PHP Notice: Undefined variable: user_id

Is this because, whilst the Edit User page is still at user-edit.php?user_id=2, the Media modal has somehow denied us the ability to get user_id?

TL;DR – I need to get user_id of currently-editing user on user-edit.php whilst in a Media picker initiated by ACF. How can I do this?

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

How to get ID of Edit User page during wp_handle_upload_prefilter, whilst in Media picker?

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.