Wordpress WP-CLI搜索替换URL的语法



我想更改这句话:

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

对此:

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

我可以为数据库中youtube视频的每个iframe做这件事。做it的正确句子是什么?我有这样的东西:

步骤1。更换第一部分:

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

步骤2。更换最后一部分:

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

对吗?我不确定引号和通配符。

谢谢!

要用正则表达式更新,请确保转义第一个参数的所有正斜杠:

$ 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 

忽略前两个匹配,因为它们不重要,并在第二个(替换)参数中使用第三个匹配。

还记得:

$ wp cache flush

最好先做一次——试运行,看看是否有任何受影响的表可能不打算修改。

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

您不需要regex,您可以用一个勾号引用它。您可以运行此程序来测试和显示可能发生的替换。删除--dry-run标志以进行实际替换。

$ wp search-replace '<iframe width="850" height="478" src="https://www.youtube.com/embed/4zH9Zca1vRM" frameborder="0" allowfullscreen></iframe>' 'https://www.youtu.be/4zH9Zca1vRM' --dry-run

还有一个正则表达式,这个修复了youtube链接或iframe http或https

  • http://www.youtube.com/watch?v=gVoUPnXqvNE&无论#t=30
  • http://www.youtube.com/embed/gVoUPnXqvNE?feature=whatever#t=30

并通过一个简单的字符串重新插入,该字符串将显示为嵌入wordpress:

https://youtu.be/gVoUPnXqvNE


$ wp search-replace '<(?:a|iframe)s[^>]*(?:href|src)="https?://www.youtube.com/(?:embed/|watch?v=)([a-zA-Z0-9-]+)(?:(?:?&)[^"#s]*)?(#t=[0-9]+)?"[^>]*>.*?</(?:a|iframe)>' 'https://youtu.be/12' --regex --regex-flags='s'

最新更新