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
(@4ever16)
Can you show re-edited version cause i dont know how to do what you are saying. đ
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
(@4ever16)
Thanks works perfectly!
# youtube link to post title
Thread Starter
Gustav
(@4ever16)
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.
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
(@4ever16)
Made a video so you can see the error
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
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.
-
This reply was modified 7 years, 7 months ago by
Gustav.
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
(@4ever16)
Yes now it works perfectly, thank you!