Search code examples
wordpressbashwp-cli

Wordpress WP-CLI search-replace syntax for URLs


I want to change this sentence:

<iframe width="850" height="478" src="https://www.youtube.com/embed/4zH9Zca1vRM" frameborder="0" allowfullscreen></iframe>

To this:

https://www.youtu.be/4zH9Zca1vRM

I can do it for every iframe of youtube video in the database. What is the right sentence for do it? I gess something like this:

step 1. Replacing first part:

wp search-replace '<iframe width="*" height="*" src="https://www.youtube.com/embed/' 'https://www.youtu.be/'  --regex

step 2. Replacing last part:

wp search-replace '" frameborder="0" allowfullscreen></iframe>' ''  --regex

Is it right? I'm not sure about quotes and wildcards.

Thank you!


Solution

  • To update with regular expressions, be sure to escape all the forward slashes for the first argument:

    $ wp search-replace '<iframe width="([0-9]+)" height="([0-9]+)" src="https:\/\/www.youtube.com\/embed\/([a-zA-Z0-9]+)" frameborder="0" allowfullscreen><\/iframe>' 'https://www.youtu.be/\3' --regex 
    

    Disregard the first two matched, since they're not important and use third match in the second (replacement) argument.

    Also remember to:

    $ wp cache flush
    

    It's a good idea to do a --dry-run first to see if there are any tables affected that you might not intend to modify.

    http://wp-cli.org/commands/search-replace/