Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save birgire/05300dee75b62fab795b to your computer and use it in GitHub Desktop.
Save birgire/05300dee75b62fab795b to your computer and use it in GitHub Desktop.
WordPress: Replace Viper's quick tags (youtube, vimeoh, veoh, viddler) with the native video shortcode.
<?php
/**
* Plugin Name: Replace Viper's Video Quicktags
* Description: Replace Viper's video quicktags (youtube, vimeo, viddler, veoh) with the native video tag [video src="..."]
* Author: birgire
* Author URI: https://gist.github.com/birgire
* Plugin URI: https://gist.github.com/birgire/05300dee75b62fab795b
* Version: 0.0.3
*
* Examples:
* Old:
* [veoh width="340" height="438"]http://www.veoh.com/videos/v14185855NK3BNfQa[/veoh]
* New:
* [video src="http://www.veoh.com/videos/v14185855NK3BNfQa"]
*
* Old:
* [youtube]http://www.youtube.com/watch?v=Mn4_40hAAr0[/youtube]
* New:
* [video src="http://www.youtube.com/watch?v=Mn4_40hAAr0"]
*
* Install:
* Create the file 'vipers_replacer.php' in your plugin directory and activate it.
*
* Usage:
* - Modify the plugin settings (i.e. posts_per_page, offset, post_type, vipers quick tags).
* - Reload the frontpage once.
* - Check the messages in the frontpage's footer.
* - Deactivate the plugin.
*
*/
add_action( 'wp_footer', function(){
// Only for admins:
if ( ! current_user_can( 'manage_options' ) )
return;
//--------------------
// EDIT to your needs:
$posts_to_fetch = 50;
$offset = 0;
$post_type = array( 'post' );
$vipers_quick_tags = array( 'youtube', 'vimeo', 'viddler', 'veoh' );
//--------------------
// Fetch posts:
$posts = get_posts( array( 'posts_per_page' => $posts_to_fetch, 'offset' => $offset, 'post_type' => $post_type ) );
// Regex setup:
$s = join( '|', $vipers_quick_tags );
$pattern = "/\[(" . $s . ")[^\]]*\](.*)?\[\/(" . $s . ")\]/i";
$replacement = "[video src=\"$2\"]";
// Loop:
print( '<h1>RegEx Updates:</h1><pre>' );
foreach( $posts as $post )
{
// Setup:
$title = $post->post_title;
$content = $post->post_content;
$pid = $post->ID;
// RegEx replace:
$content = preg_replace( $pattern, $replacement, $content );
// Only update the current post if the replace had any effect:
if( $content !== $post->content && ! empty( $content ) ):
// Update current post:
if( 0 == wp_update_post( array( 'ID' => $pid, 'post_content' => $content ) ) )
$updated = 'no';
else
$updated = 'yes';
// Show results:
printf( "<hr /> Post: %s - ID: %d - %s",
$title,
$pid,
( 'yes' === $updated ) ? 'Succesfully updated!' : 'Error when trying to update!'
);
endif;
}
print( '</pre>' );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment