Overwrite WordPress Page if Title Already Used
-
Hi (Joey?) I don’t know how to contact you but I need someone’s help.
1. Can someone (Joey?) please rewrite this code so this overwrite WordPress page if Title Already Used?
2. Can someone please tell me how to activate this code on my wordpress site.
The following code is working to overwrite post if title is already used:
<?php /* Plugin Name: Overwrite WordPress Posts if Title Already Used Description: Checks to see if post title has already been used and if so deletes the old post and creates a new one with the given data. Author: JPG Version: 1.0 */ // Filter used to access data before it is written to the database add_filter( 'wp_insert_post_data', 'overwrite_wordpress_post_if_title_exists', 99, 2 ); // Our function to delete the old post if there is a title clash (title must be identical) function overwrite_wordpress_post_if_title_exists( $data, $postarr ) { // Return if we aren't saving/updating a post if ( $data['post_status'] !== 'publish' ) { return $data; } $new_title = my_clean_string_function( $data['post_title'] ); $new_title = strtolower( $new_title ); // Return all existing posts $posts = get_posts( array( 'post_type' => 'post' ) ); // Loop through exisiting posts foreach ( $posts as $post ) { $old_title = my_clean_string_function( get_the_title( $post ) ); $old_title = strtolower( $old_title ); // If title already exists, delete the old post before we save the new one (change 'false' to 'true' to delete the post rather than trash it) if ( $old_title == $new_title && $post->ID != $postarr['ID'] ) { wp_delete_post( $post->ID, false ); $data['post_name'] = sanitize_title( $data['post_title'] ); } } return $data; } function my_clean_string_function( $string ) { return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars. } ?>
Thanks so much
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Overwrite WordPress Page if Title Already Used’ is closed to new replies.