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
- Customizing subject in comment notification e-mails
- How to disable Gutenberg editor?
- WP showing “warning: call_user_func_array()”, What to do?
- WordPress apply_filters() Arguments Missing
- Which hook should be used to validate custom form fields on the login form?
- Removing user contact methods works from functions.php but not from a plugin
- Hook (upgrader_process_complete) running moment
- How to get all attachment image from post ?? WordPress
- How to prepend to the_title for admin-side plugin’s use
- Adding dynamic section to WordPress
- woocommerce product attributes
- How can i change an image’s author?
- Creating search filter through plugin
- Add Fields with Sub-Fields to WP Job Manager
- template_redirect not being called when using ajax
- Plugin custom Action Hook not working
- Hook to generate a dynamic sitemap with “All in One SEO Pack” Plugin
- How to correctly detect accessing wp-content/uploads?
- WP Job Manager Category Drop-down; Change Placeholder Text Via Filter
- Are there hooks for WordPress updates?
- Plugin activation hook in an abstract class
- Is there an earlier hook than login_head or login_enqueue_scripts?
- Gravity Forms plugin: How to use “gform_editor_js” action hook? [closed]
- Calling plugin function inside custom plugin for onclick event
- Proper way to replace the_content only for pages created by custom plugin
- how to change automatic placing of social plugins below content?
- Deactivate JS Script in Plugin Shortcode
- Filter WooCommerce Orders
- Adding Plugin Assets to Header
- What are admin hooks
- Change text string in a plugin
- Advanced Custom Fields (acf) – Filter Relationship by Taxonomies not Post Type
- How to use apply_filters?
- How to append new form elements in “Add New” form of Users in WordPress admin panel?
- Filtered content appears in the excerpt
- Is there a hook similar to “save_post” which only fires AFTER the post is completely published?
- How do I add a medium to the WordPress database with my own plugin?
- save_post hook partly firing in update post
- Show post object of any page in frontend
- Passing function into add_action always returns the first argument
- Customize plugin’s output: filters or setters: looking for an advice
- Is it possible to disable a theme programmatically?
- plugin not hooking to my custom hook
- Modify a plugin’s content
- Plugin hook breaks new WP 4.0 media library grid view?
- Which action/filter can i use for a Member Plugin [closed]
- WordPress Hooks : Where to place callbacks that repetitively yield the same effect?
- Can’t use updated variables in handle function