Hook save_post on multiple posts edit (bulk action)
-
Hi,
Wordpress enable to apply a bulk action on a group of selected posts, like “edit”.
I created a plugin that calls a function that add some shortcodes in a custom field when the “save_post” action is triggered.It works fine when I edit and save a single post.
But when I want to use the bulk action “edit”, and save all the post that I selected in a row, the hook seems to not be called.I need to apply my function in around 200 posts… So I can’t do it manually one by one.
Is there another hook or something to solve my problem ?Thank you in advance.
-
As of WordPress 4.7 you can create your own bulk actions, which sounds like it’s what you want:
This is a great alternative.
However, I got another problem with that : when I try to apply my function, it seems to work because I can see what is added in my custom field when I edit a post (single). However, if I don’t update manually this post after that, it seems to not be registered in the database (only in a kind of cache ?). I tried to force update with the function wp_update_post(), but it doesn’t work :
Here’s my part of code :
/* Custom bulk action (multiple posts) */ // register add_filter( 'bulk_actions-edit-post', 'register_add_auto_forms' ); function register_add_auto_forms($bulk_actions) { $bulk_actions['add_auto_forms'] = __( 'Ajouter les formulaires', 'add_auto_forms'); return $bulk_actions; } // handler add_filter( 'handle_bulk_actions-edit-post', 'add_auto_forms_handler', 10, 3 ); function add_auto_forms_handler( $redirect_to, $doaction, $post_ids ) { if ( $doaction !== 'add_auto_forms' ) { return $redirect_to; } foreach ( $post_ids as $post_id ) { $all_terms = get_the_terms( $post_id, 'cm'); if ($all_terms[0]->name != null){ $cm = $all_terms[0]->name; $shortcode = form_filter($cm); $city_select = '[WG_zip2City zipcodeFieldId="cp" cityFieldId="ville"]'; $shortcodes = ''.$shortcode.$city_select; update_field('formulaire', $shortcodes, $post_id); $post_args = array('ID' => $post_id,); wp_update_post($post_args); } } $redirect_to = add_query_arg( 'bulk_add_auto_forms_posts', count( $post_ids ), $redirect_to ); return $redirect_to; }
$shortcode is declared higher.
Hi Nokaa,
I regret that I am equally mystified by your code not working. It all looks good to me. Is adding these shortcodes going to be an ongoing task, or is it a one time task to bring a bunch posts up to date? Is there a way to query for the posts that need shortcodes, or is manually checkmarking in the posts list required?
If it’s a one time task, it would be more appropriate to create a one time script to accomplish this. Such a script would need to be able to determine which posts to modify on its own. For example, querying for posts within a certain date range whose field value does not contain a particular shortcode.
Even if manual checkmarking is required, if this is a one time task, you could type in an array of post IDs for the script to use. With the right hardware setup and decent 10-key skills, this would not take very long even with hundreds of IDs.
If this is an ongoing task, then bulk actions would be a good approach. You might try placing the field value directly into the DB with SQL queries. The original bulk action concept was to pass IDs to another code page that does the processing. Processing within the bulk action callback was not really part of the concept. You could try redirecting to a custom code page that does the processing. Afterwards, redirect back to the posts list screen.
Yes it can be an ongoing script. The bulk actions is a precaution for the future but I don’t think that we’ll need it later.
This seems to be very pernickety to launch a ongoing script on WordPress. I don’t really know how to process without any hook… I tried it by creating a plugin that calls a function without conditions (on activation), it result in a blank page. My entiere WP was broken until I take off my plugin on the FTP.
Running a custom script on plugin activation is certainly one approach. It does need to be setup correctly of course. The trouble is such scripts are difficult to debug. It would not be my first choice.
One way to launch a one time custom script would be to create a custom page template that contains the script. Add a WP page that uses the template — mainly to establish a permalink that can be requested. When that page is requested, the script runs. It’s actually a bit too easy, anyone requesting the page executes the script. Some sort of limiting mechanism would be a really good idea. Perhaps by only making actual changes if the logged in user ID is yours.
Or the initial GET request produces a GO button. Clicking the button makes a POST request to the same page. The part that actually does something only runs on POST requests. You’ll want to initially limit how many posts are changed until you’re sure the script works correctly. Coding mistakes are inevitable, which is why most devs will not work directly on a live site. The code is only applied to the target site once all the bugs are squashed in a local installation. Setting up a local development installation takes some doing, but it’s well worth the effort. You will not wish to ever go back to working on live sites.
Even then, it happens that coding problems creep in on the live site even though it works fine locally. It’s relatively rare though. Being template based, your script is not easily implemented through a plugin. Templates are the realm of themes. As a one time script, there’s little concern for the code being overwritten during a theme update, so just adding the template file to the current theme’s folder should be OK. For launching script that is applied as an intermittent, on-going feature of a plugin, the script could be launched from the plugin’s admin page.
Plugin admin pages are added with add_menu_page() or add_submenu_page(). Once again, the equivalent of a GO button can be displayed with the initial GET request. Clicking it results in a POST request to the same page. Only on POST requests is the actual script executed. The bulk action hooks are also a viable approach. Like activation hooks, these are difficult to debug. It’s easier to develop an external script and only have the bulk action request the external script. The external script could essentially be the same one launched by the admin page GO button. The difference being where the posts to act upon are specified.
For applying a bulk action to 200 selected posts, I would not even want to checkmark that many. I would figure out a different way to determine the proper posts. So a bulk action may not be the best choice. For a quick, just get it done script, I’d go with a custom page template. For something that could be repeated on occasion, I would create a plugin admin page from which to launch the script.
If the entire admin page would only contain the one GO button, that could appear to be a poorly considered solution. One response would be “So what? It works.” Or you could add the page in a way so that your user is the only one who sees it. Or the button could be added to a related settings page that already exists. Or added to an existing dashboard widget or add a new dashboard widget that contains the button. BTW, dashboard widgets are unrelated to front end sidebar widgets. They are actually meta boxes.
There are all sorts of possibilities! I haven’t even touched upon launching from action hooks. These are more appropriate for automatic execution. For manually launched script, one of the above mentioned approaches would be better.
- The topic ‘Hook save_post on multiple posts edit (bulk action)’ is closed to new replies.