As the title suggests, I’d like to completely skip the “edit” page once I add a new product, and go directly to adding a new product instead. The time it takes my cart to switch pages is just horrible, this will greatly speed up my process of adding thousands of products.
I am aware that ChatGPT isn’t always the best route to go when looking for coding help, but it did suggest some code for me to add to functions.php, however, it’s not working. When I finish publishing a new product, it’s still returning me to the edit product page. Any idea why this might not be working?
Code:
add_action('save_post_product', 'redirect_to_add_new_product', 20, 2);
function redirect_to_add_new_product($post_id, $post) {
// Check if it's a new product and not an autosave
if ($post->post_status === 'publish' && defined('DOING_AUTOSAVE') && !DOING_AUTOSAVE) {
// Remove the custom meta to prevent redirect loop
delete_post_meta($post_id, '_is_new_product');
// Redirect to the "Add New Product" page
wp_redirect(admin_url('post-new.php?post_type=product'));
exit;
}
}
add_action('load-post-new.php', 'set_new_product_meta');
function set_new_product_meta() {
// Check if it's a new product being created
if (isset($_GET['post_type']) && $_GET['post_type'] === 'product') {
$screen = get_current_screen();
if ($screen->id === 'post') {
add_post_meta(get_the_ID(), '_is_new_product', true);
}
}
}
Added code to functions.php
Added a new product and clicked publish.
Expected the page to then return to add a new product, but instead, it is still returning to the edit product page where I have to click on “add new product”.
Aaron Townsend is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.