Title: replace &#8220;wp_is_post_revision&#8221;
Last modified: October 7, 2018

---

# replace “wp_is_post_revision”

 *  Resolved [Gustav](https://wordpress.org/support/users/4ever16/)
 * (@4ever16)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/replace-wp_is_post_revision/)
 * Im using this function to replace youtube links to titles.
    The sad thing is 
   that it only works if i post from the backend.
 * I have a front end post plugin and this function doesnt work with it. So i think
   the problem is in `if ( ! wp_is_post_revision( $post_id ) )` what can i replace
   it with so it works with the front end editor.
 *     ```
       function change_title_yt( $post_id ) {
           $current_title = $_POST['post_title'];
   
           $doc = new DOMDocument();
           $doc->preserveWhiteSpace = FALSE;
           $doc->loadHTMLFile($current_title);
   
           $title_div = $doc->getElementById('eow-title');
           $title = $title_div->nodeValue;
   
           $my_args = array(
               'ID' => $post_id,
               'post_title' => $title
           );
   
           if ( ! wp_is_post_revision( $post_id ) ){
   
               // unhook this function so it doesn't loop infinitely
               remove_action('save_post', 'change_title_yt');
   
               // update the post, which calls save_post again
               wp_update_post( $my_args );
   
               // re-hook this function
               add_action('save_post', 'change_title_yt');
           }
       }
       add_action('save_post', 'change_title_yt');
       ```
   

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/replace-wp_is_post_revision/#post-10758462)
 * Why don’t you use the ‘wp_insert_post_data’ filter to alter titles? Then you 
   don’t need to worry about infinite loops and and you are altering the title before
   it’s saved, removing the need to re-save the post to update the title.
 *  Thread Starter [Gustav](https://wordpress.org/support/users/4ever16/)
 * (@4ever16)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/replace-wp_is_post_revision/#post-10759163)
 * Can you show re-edited version cause i dont know how to do what you are saying.
   🙂
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/replace-wp_is_post_revision/#post-10760173)
 *     ```
       function change_title_yt( $data, $raw ) {
           $current_title = $data['post_title'];
   
           $doc = new DOMDocument();
           $doc->preserveWhiteSpace = FALSE;
           $doc->loadHTMLFile($current_title);
   
           $title_div = $doc->getElementById('eow-title');
           $title = $title_div->nodeValue;
   
           $data['post_title'] = $title;
           return $data;
       }
       add_action('wp_insert_post_data', 'change_title_yt', 10, 2 );
       ```
   
 * In this callback, if it’s still necessary to determine if the post is a revision
   or not, the $data[‘ID’] element is empty with new posts, defined for revisions.
 *  Thread Starter [Gustav](https://wordpress.org/support/users/4ever16/)
 * (@4ever16)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/replace-wp_is_post_revision/#post-10762943)
 * Thanks works perfectly!
 * **# youtube link to post title**
 *  Thread Starter [Gustav](https://wordpress.org/support/users/4ever16/)
 * (@4ever16)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/replace-wp_is_post_revision/#post-10766780)
 * **I found an error.**
 * With this function i can only create 1 post.
    If i make a post all the old ones
   gets deleted and just the new one is showing.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/replace-wp_is_post_revision/#post-10769440)
 * Well, that’s strange! All we’re doing is changing post titles. I don’t see how
   that can cause files to be deleted. I’m not doubting you, I’m just puzzled. I
   didn’t really study your initial code, I only pasted in the relevant portion 
   to a standard filter callback.
 * Looking closer now, loadHTMLFile($current_title) is potentially troublesome. 
   Are these titles as they come in _always_ valid URLs? Maybe initially, but I 
   think not in the case of post updates. With an invalid URL as title, the loadHTMLFile()
   call will fail, resulting in the updated post’s title being empty. I think the
   posts are not really deleted, but become invisible because there is no post_title
   field value in the DB. You could probably go into the DB through phpMyAdmin, 
   assign titles, and the posts might reappear.
 * Anyway, maybe the solution is to conditionally set the title only for brand new
   posts (no post_id value) or if there is a post_id value and the current post 
   status is not “publish”. Or maybe there’s an easier way based on the content 
   of the title? Like only set the title if the initial title contains “youtu.be”?
   The point is to not try to automatically set the title if it has already been
   automatically set. There’s any number of ways to do this, but I don’t know the
   nature of your data, so I couldn’t really say which is best.
 * If you can give me some idea what would work, I can help you code it. I’m not
   100% sure this is the problem, but this condition should be in place no matter
   what.
 *  Thread Starter [Gustav](https://wordpress.org/support/users/4ever16/)
 * (@4ever16)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/replace-wp_is_post_revision/#post-10769453)
 * Made a video so you can see the error
 * [https://www.youtube.com/watch?v=wcfHhpACvgk](https://www.youtube.com/watch?v=wcfHhpACvgk)
 * What im trying to do is to convert youtube links to their original titles.
    For
   example if there is a post called: [https://www.youtube.com/watch?v=YVkUvmDQ3HY](https://www.youtube.com/watch?v=YVkUvmDQ3HY)
   I want it to be named **Eminem – Without Me** cause whats the links name.
    -  This reply was modified 7 years, 7 months ago by [Gustav](https://wordpress.org/support/users/4ever16/).
    -  This reply was modified 7 years, 7 months ago by [Gustav](https://wordpress.org/support/users/4ever16/).
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/replace-wp_is_post_revision/#post-10771004)
 * Huh, I’m blocked from that vid. It’s OK, I think I get what you are doing. Assuming
   you use no other video source than youtube.com and there will never be a vid 
   title containing the string “youtube.com/”, try this version:
 *     ```
       function change_title_yt( $data, $raw ) {
           $current_title = $data['post_title'];
           if ( false !== stripos( $current_title, 'youtube.com/')) {
             $doc = new DOMDocument();
             $doc->preserveWhiteSpace = FALSE;
             $doc->loadHTMLFile($current_title);
   
             $title_div = $doc->getElementById('eow-title');
             $title = $title_div->nodeValue;
   
             $data['post_title'] = $title;
           }
           return $data;
       }
       add_action('wp_insert_post_data', 'change_title_yt', 10, 2 );
       ```
   
 * If there are a few other vid source domains, they can be accommodated.
 *  Thread Starter [Gustav](https://wordpress.org/support/users/4ever16/)
 * (@4ever16)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/replace-wp_is_post_revision/#post-10771105)
 * Yes now it works perfectly, thank you!
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/replace-wp_is_post_revision/#post-10774691)
 * Awesome! You’re welcome.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘replace “wp_is_post_revision”’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 10 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [7 years, 7 months ago](https://wordpress.org/support/topic/replace-wp_is_post_revision/#post-10774691)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
