• For a long time I’ve been using a plugin to embed Tweets on my website (before oEmbed)

    I’m in the process of changing themes and with Tweets automatically embedding thanks to oEmbed, I no longer need the plugin.

    However, I still need the URLs I’ve inserted in this plugin. The current shortcode looks something like this:

    [blackbirdpie url=”https://twitter.com/user/status/123465790″%5D

    What I would like is to remove everything from that shortcode, except the URL.

    Is there anything I could do that would achieve what I want?

    Thanks in advance for any answers.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Are the tweets always https://twitter.com/...?

    Thread Starter namli

    (@namli)

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    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

    1. Locate every post or page that has the blackbirdpie short code
    2. Delete just the short code and leave the specific URL behind
    3. Save the post without the shortcode
    4. 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.

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Updated the pastebin.com link with code that seems work.

    https://pastebin.com/1PsbCVjH

    When someone views a post or page with the [blackbirdpie url="..."] shortcode in it, this plugin will remove the blackbirdpie shortcode and force the page to reload in the browser.

    The next time the post or page is viewed then the shortcode will be removed. The plugin will then ignore that post since the shortcode is already gone.

    I’m still not comfortable with the updating the post content this way though so make sure you have a good database backup.

    Thread Starter namli

    (@namli)

    I will give it a try and come back with the result.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How do I remove only a part of a shortcode?’ is closed to new replies.