最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Randomize attachment IDs

programmeradmin3浏览0评论

I'd like to have my attachments IDs being random (not sequential). Just like what this plugin does, but for attachments. I've tried to look into the source code of the plugin, but it uses filters that are not applicable to attachments.

So, basically this is what the user ID randomization plugin does

if ( ! function_exists( 'dfx_random_user_id_user_register' ) ) {

    /**
     * Randomizes the user_id for new created users
     *
     * @param array $data
     * @param bool $update
     *
     * @return array
     */
    function dfx_random_user_id_user_register( $data, $update ) {

        if ( ! $update )  {

            // Locate a yet-unused user_id
            do {
                $ID = random_int( 1, dfx_random_user_id_get_max_id() );
            } while ( get_userdata( $ID ) );

            $data += compact( 'ID' );
        }

        return $data;
    }

}

add_filter( 'wp_pre_insert_user_data', 'dfx_random_user_id_user_register', 10, 2 );

Is there a way to do something similar for the attachments IDs?

I'd like to have my attachments IDs being random (not sequential). Just like what this plugin does, but for attachments. I've tried to look into the source code of the plugin, but it uses filters that are not applicable to attachments.

So, basically this is what the user ID randomization plugin does

if ( ! function_exists( 'dfx_random_user_id_user_register' ) ) {

    /**
     * Randomizes the user_id for new created users
     *
     * @param array $data
     * @param bool $update
     *
     * @return array
     */
    function dfx_random_user_id_user_register( $data, $update ) {

        if ( ! $update )  {

            // Locate a yet-unused user_id
            do {
                $ID = random_int( 1, dfx_random_user_id_get_max_id() );
            } while ( get_userdata( $ID ) );

            $data += compact( 'ID' );
        }

        return $data;
    }

}

add_filter( 'wp_pre_insert_user_data', 'dfx_random_user_id_user_register', 10, 2 );

Is there a way to do something similar for the attachments IDs?

Share Improve this question edited Jul 18, 2020 at 12:11 Alex C. asked Jul 18, 2020 at 10:44 Alex C.Alex C. 1351 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

For attachment posts, the following should do it, i.e. use the wp_insert_attachment_data hook along with get_post():

function my_random_post_id( $data, $postarr ) {

    // Runs if the post is being created and not updated.
    if ( empty( $postarr['ID'] ) )  {

        // Locate a yet-unused ID in the (wp_)posts table.
        do {
            // Based on the dfx_random_user_id_get_max_id() function.
            $max_post_id = ( ( 9007199254740991 + 1 ) / 2 ) - 1;

            $ID = random_int( 1, $max_post_id );
        } while ( get_post( $ID ) );

        $data += compact( 'ID' );
    }

    return $data;

}

add_filter( 'wp_insert_attachment_data', 'my_random_post_id', 10, 2 );
// The following is for non attachments.
//add_filter( 'wp_insert_post_data', 'my_random_post_id', 10, 2 );

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论