WordPress.org

Make WordPress Core

Changes between Initial Version and Version 1 of Ticket #39829, comment 2


Ignore:
Timestamp:
02/10/2017 05:30:56 AM (4 years ago)
Author:
jaschaio
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #39829, comment 2

    initial v1  
    1 Hey @SergeyBiryukov, thanks for the fast reply. I have seen the other filters, but there is still no filter for the USER ID.
     1Hey @SergeyBiryukov, thanks for the fast reply. I have seen the other filters, but there is still no filter for the user `ID`.
    22
    3 As I explained: Within the "wp_insert_post_data" function you have the ability to filter through all the $data array. That means that you can modify the $data['ID'] and assign a different one before the post gets actually inserted into the database.
     3As I explained: Within the `wp_insert_post_data` function you have the ability to filter through all the `$data` array. That means that you can modify the `$data[ID]` and assign a different one before the post gets actually inserted into the database.
    44
    5 The "wp_insert_user" function seems to work different, as it inserts the user first based on the nickname and than works with the ID returned by the database going forward. Meaning that I can't hook anywhere to change the ID of the to be created user.
     5Example:
    66
    7 I just wondered why there are filters available for every other field of the user, but not for the ID. Especially if its possible within the "wp_insert_post_data" function. These two should work similar.
     7{{{#!php
     8/**
     9 * Filter to use random 6 digit number as post ID
     10 **/
     11function create_random_unique_id( $data, $postarr ) {
     12
     13    // Check if we are updating an existing post or importing with a suggested post ID
     14    if ( ! empty( $postarr['ID'] ) || ! isset( $postarr['import_id'] ) ) {
     15        return $data;
     16    } else {
     17        // Get Wordpress Database
     18        global $wpdb;
     19
     20        // Create random 6 digit number
     21        $random   = substr( rand() * 900000 + 100000, 0, 6 );
     22
     23        // Check if ID is unique in database
     24        while ( $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $random ) ) ) {
     25            // Generate new random ID
     26            $random   = substr( rand() * 900000 + 100000, 0, 8 );
     27        }
     28
     29        // Assign new post id random number
     30        $data['ID'] = $random;
     31
     32        return $data;
     33    }
     34
     35}
     36add_action( 'wp_insert_post_data', 'create_random_unique_id', 10, 2 );
     37}}}
     38
     39
     40The `wp_insert_user` function seems to work different, as it inserts the user first into the database based on the nickname (and not like the post based on the ID) and than works with the ID returned by the database going forward. Meaning that I can't hook in anywhere to change the ID of the to be created user. The only thing I can do is updating the database after the user is created with the `user_register` action. Here is an example for that:
     41
     42{{{#!php
     43function create_random_unique_user_id( $user_id ) {
     44
     45    // Get global Wordpress Database Object
     46    global $wpdb;
     47
     48    // Get newly created user ID
     49    $user_object = get_user_by( 'id', $user_id );
     50
     51    // Create random 6 digit number
     52    $random   = substr( rand() * 900000 + 100000, 0, 6 );
     53
     54    // Check if ID is unique in database
     55    while ( $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE ID = %d", $random ) ) ) {
     56        // Generate new random ID
     57        $random   = substr( rand() * 900000 + 100000, 0, 8 );
     58    }
     59
     60    // Update all tables with new user ID
     61    $wpdb->update( $wpdb->users, array( 'ID' => $random ), array( 'ID' => $user_id ) );
     62    $wpdb->update( $wpdb->posts, array( 'ID' => $random ), array( 'post_author' => $user_id ) );
     63    $wpdb->update( $wpdb->usermeta, array( 'ID' => $random ), array( 'user_id' => $user_id ) );
     64    $wpdb->update( $wpdb->comments, array( 'ID' => $random ), array( 'user_id' => $user_id ) );
     65    $wpdb->update( $wpdb->links, array( 'ID' => $random ), array( 'link_owner' => $user_id ) );
     66
     67}
     68add_action( 'user_register', 'create_random_unique_user_id', 10, 1 );
     69}}}
     70
     71But this is problematic as the `user_id` returned by `wp_insert_user` is still the old one. As such other functions who depend on the return value of `wp_insert_user` fail.
     72
     73I just wondered why there are filters available for every other field of the user, but no one for the ID. Especially if its possible within the "wp_insert_post_data" function. These two should work similar.