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 );
Related Posts:
- Passing a parameter to filter and action functions
- Get a list of all registered actions
- How can I edit post data before it is saved?
- How to only hook on Single.php after content?
- How To Determine If A Filter Is Called In A Sidebar/Widget Context?
- Edit plugin without hooks in functions.php
- Filter Hook on plugin update
- Hide WordPress Plugin Deactivation Links
- Use external link in the add sub menu
- Pass A Value From Outside To A Plugin Variable
- Failed to invoke other hook from the init hook
- Does the ‘nav_menu_css_class’ filter accept 2 or 3 arguments?
- do_action and hook methods
- How can I filter blog name?
- why does the add_action(‘the_content’) overwrite my page
- How to filter content post only on save
- Filter or action hook to insert content on admin pages before tag
- plugin_action_links_{$plugin_file} hook not in the main plugin file
- Woocommerce Shipping module available only for type of products [closed]
- Determine plugin name from within plugin_action_links filter
- merging an array to an existing array using add_filter
- Reposition Woocommerce Message
- Pass info from functions.php to plugin
- Change permalink structure hidden button edit
- How do action and filter hooks understand where to look for the core function that we hooked our function to them
- Inserting above the comment template
- Modifying the comments section through a plugin regardless of theme
- Get Time Taken By Each Action Hook in WordPress
- Filtering WooCommerce Orders by Category
- Template filter for custom taxonomy terms
- Add sub menu page in your plugin
- What exactly happens to function argument availability when using a filter?
- remove_action not removing add_action from constructor
- How to use the pre_option filter before a plugin loads?
- How to use filter to disable adding a product to wishlist?
- WordPress: Add custom add_filter for custom functions
- Hook for altering the content of all wp mails
- How to hook into action/filter call
- Use a hook or filter, or overwrite this Gamipress function?
- Add hook after content without formatting
- Unpublished Pages Failing To Appear On Custom Path
- Randomize post (and page) IDs on generation
- Plugin function in child theme
- Use action, filter, or hook to append HTML to WordPress plugin function
- Customize plugin templates
- Why can’t I shove an instance of a class into a variable from a do_action hook?
- Passing a parameter to filter and action functions
- change output location of plugin function using a custom hook
- add query string to all pages after user logged in
- Order shipped by which driver[hook for woocoomerce order staus changed and popup in admin panel ] [closed]
- How to Add Extra Text In WordPress Title Before Post Publish
- Change social icon in twenty twenty three theme
- Customize WP Filter Hook
- How to stop activating a plugin and show admin notice when dependent plugins minimum version is not met
- Using a post-signup hook to get user details
- How to change File URL in file uploaded to s3 compatible storage with self written plugin [closed]
- Map a custom id to wordpress post id
- How to get the element ID from new menu list that added with add_filter()?
- woocommerce_package_rates not fired when wordpress woocommerce accessed as non ajax
- Trigger Elementor Popup via code [closed]
- wp_get_post_terms is difference obj model than the one in rest api response
- Calling plugin function inside custom plugin for onclick event
- White page by using filter template_include
- How to check current user before all actions and filters?
- How do I create a filter feature on WordPress? [closed]
- get current date + 90 days and checking every day
- Create a navbar filter that filters by a custom field
- Passing an array from shortcode-function to filter-function
- Calculate price and display on woocommerce product single page under price (simple price, variable price) [closed]
- Are functions in main plugin file called before function bound to register_activation_hook runs?
- Best place for short bio,image and button [closed]
- How to remove plugin metaboxes from edit.php
- Plugin developer automated documentation
- qTranslate remove default directory from link
- How to disable a plugin for certain page?
- Output content to the_content before a plugin does
- Is there a hook to Intercept al urls from a webpage and redirect to a page
- WordPress user account activation
- do_action() hook into load-(page)
- Howto check version of another plugin and then add a submenu page to it
- External Script Using WP – Hooks / Actions Not Working?
- password_reset hook is not working
- The Hook registration_errors was not called
- URL parameters causing 404 on home page, but nowhere else
- Which filter affects the ‘entry-title’ post class
- How to change the name of the “edit my profile” link in the WordPress admin backend
- Filtering get_permalink in Jetpack / ShareDaddy
- Hook into existing tinyMCE button
- Proper way to replace the_content only for pages created by custom plugin
- add_filter doesn’t work
- How to get post ID with hooks publish_post, new_to_publish, etc
- Redirect to another page using contact form 7? [closed]
- edit_user_profile and show_user_profile are not firing inside a class
- Is there a way to figure out which action/filter invoked a function?
- how to change automatic placing of social plugins below content?
- Using add_action before add_filter on a plugin?
- Check if variable is set in filter
- Combining several CSS files into one for optimization
- the_post hook is not firing for me
- Why do plugins often ask to add in to templates?