Note: this may be Horribly Bad Advice™ but I’m hoping someone can expand or use some of this to help you pull off what you are looking for. Please make a backup of your database before using this plugin, I have a really horrible suspicion that updating the database the way I am below really is all wrong.
So I have a kinda, sorta, ish solution but I’m not happy with it for a couple of reason. But the code may help you find a better solution so here it is.
What you want to do is
- Locate every post or page that has the blackbirdpie short code
- Delete just the short code and leave the specific URL behind
- Save the post without the shortcode
- Force the oembed discover for that post to happen so that instead of just a link you get the embedded tweet
It’s that last part that’s giving me grief. What I’ve done is make a plugin that finds and removes the shortcode, saves the updated post (likely using the wrong way), but doesn’t get the new oEmbed results.
When the post is displayed the plugin will locate the shortcode and remove it. Then it will save the updated content for that post.
https://pastebin.com/1PsbCVjH
<?php
/*
Plugin Name: Replace old shortcode
Description: This will remove the Blackbird Pie shortcode when it's found via a filter
Version: 0.1
*/
add_filter( 'the_content' , 'mh_remove_shortcode' , 0 );
function mh_remove_shortcode( $content ) {
global $wpdb, $post;
// Regex to find all [blackbirdpie url="twitter"] shotcodes.
$mh_url_regex = "/\[blackbirdpie\ url\=\"(http|https)\:\/\/twitter\.com\/.*\"]/";
// If we get any hits then remove the the shortcode
if ( preg_match_all( $mh_url_regex , $content, $mh_matches ) ) {;
// Go through that array and remove the shortcode
for ( $mh_count = 0; $mh_count < count( $mh_matches[0] ); $mh_count++ )
{
$mh_with_shortcode = $mh_matches[0][$mh_count];
$mh_without = str_replace( '[blackbirdpie url="' , '' , $mh_matches[0][$mh_count] );
$mh_without = str_replace( '"]' , '' , $mh_without );
$content = str_replace( $mh_with_shortcode , $mh_without , $content );
// This is probably all wrong...
// delete_oembed_caches( $post->ID );
$wpdb->update( $wpdb->posts, array( 'post_content' => $content ),
array( 'ID' => $post->ID )
);
}
// cache_oembed( $post->ID );
}
return $content;
}
I’ve commented out the delete_oembed_caches()
and cache_oembed()
because it wasn’t working as I thought it would.
Hope this gets you closer to a solution.