Site icon Hip-Hop Website Design and Development

How you can get ID of Edit Person web page throughout wp_handle_upload_prefilter, while in Media picker?

I’ve an Superior Customized Fields discipline group on Edit Person pages (user-edit.php), containing an Picture discipline (avatar). I goal to rename that uploaded filename to match the username of the on-page person.

My present code makes use of wp_handle_upload_prefilter to catch the add, and, to make sure the rename doesn’t occur to all WP file uploads, I feel I must verify that the add discipline in query is my particular ACF Picture…

Within the beneath code, (1) I can efficiently get the ACF discipline key and discipline for this verify (verify not totally applied)…

add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );

operate custom_upload_filter( $file ) {

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

    $discipline = false;
    // Seek for discipline key inside out there knowledge.
    // Case 1) Media modal question.
    if ( isset( $_POST['query']['_acfuploader'] ) ) {
        $field_key = (string) $_POST['query']['_acfuploader'];

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

    // Try and load discipline.
    // Notice the `acf_get_field()` operate will return false if not discovered.
    if ( isset( $field_key ) ) {
        $discipline = acf_get_field( $field_key );
    }


    // 2. GET USER'S ID AND USERNAME

    // Code right here


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

}

Nonetheless, I am struggling to (2) get the ID of the person on the user-edit.php web page.

I’ve tried two strategies discovered right here on StackExchange

1. wp_get_current_user

This…

$user_id = (int) $user_id;
$current_user = wp_get_current_user();
if ( ! outlined( 'IS_PROFILE_PAGE' ) )
    outline( '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 person ID.' ) );
elseif ( ! get_userdata( $user_id ) )
    wp_die( __('Invalid person ID.') );

2. $_GET['user_id']

This…

// If is present person's profile (profile.php)
if ( outlined('IS_PROFILE_PAGE') && IS_PROFILE_PAGE ) {
    $user_id = get_current_user_id();
// If is one other person's profile web page
} elseif (! empty($_GET['user_id']) && is_numeric($_GET['user_id']) ) {
    $user_id = $_GET['user_id'];
// In any other case one thing is mistaken.
} else {
    die( 'No person id outlined.' );
}

However neither end in a user_id being situated. It’s at all times PHP Discover: Undefined variable: user_id

Is that this as a result of, while the Edit Person web page continues to be at user-edit.php?user_id=2, the Media modal has one way or the other denied us the power to get user_id?

TL;DR – I must get user_id of currently-editing person on user-edit.php while in a Media picker initiated by ACF. How can I do that?