Remove custom post type and redirect to home page
P粉590428357
2023-09-04 12:16:33
<p>I'm using <code>get_delete_post_link</code> to delete a custom post from the frontend, but after deleting I'm getting a 404 page. How to redirect to the homepage after deleting a custom post? </p>
<p>I inserted this code in functions.php: </p>
<pre class="brush:php;toolbar:false;">function wp_delete_post_link($link = 'Delete Post', $before = '', $after = '') {
global $post;
$link = "<a href='" . wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&post=" .
$post->ID, 'delete-post_' . $post->ID) . "'>".$link."</a>";
echo $before . $link . $after;
}</pre>
<p>Then I created a shortcode to generate the delete button: </p>
<pre class="brush:php;toolbar:false;">function wpc_elementor_shortcode( $atts ) {
wp_delete_post_link();
}
add_shortcode( 'my_shortcode', 'wpc_elementor_shortcode');</pre>
<p>Is there any way to improve this code to redirect after deletion? </p>
Try this:
I tried many code snippets to redirect after deleting the custom post but none of them worked. So I tried another approach: redirecting the 404 page to a custom front-end editor dashboard I built for editor role users. code show as below:
I don't want visitors to the site to encounter this (they have regular 404 pages), so this redirect will only be applied if the user is logged in and has the editor role. This is achieved by using conditional generators from the WPCodeBox plugin.