admin管理员组文章数量:1330579
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 badges1 Answer
Reset to default 2For 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 );
本文标签: pluginsRandomize attachment IDs
版权声明:本文标题:plugins - Randomize attachment IDs 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742251877a2440914.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论