我正在更新一个Wordpress网站。它使用一个短代码来制作youtube嵌入视频,但现在已经不再需要它了。我是一个前端开发人员,我希望在接触数据库之前要小心。如何使用MySQL搜索和替换来转换:
[sc:youtube id="abcdefghijk"]其中abcdefghijk是youtube视频id。我想把它转换成一个标准的嵌入代码,如下所示:
<iframe width="775" height="436" src="http://www.youtube.com/embed/abcdefghijk?rel=0" frameborder="0" allowfullscreen></iframe>唯一真正需要保留的就是id。
发布于 2013-05-23 06:22:09
我不知道用Wordpress怎么做,但MySQL语句如下所示。假设表名为meta_data,列为meta_key,meta_value,其中meta_key将具有youtube,而meta_value将具有abcdefghijk。将列和表名改为实际信息。
UPDATE
`meta_data`
SET
`meta_value` = CONCAT('<iframe width="775" height="436" src="http://www.youtube.com/embed/', meta_value, '?rel=0" frameborder="0" allowfullscreen></iframe>')
WHERE
`meta_key` = 'youtube'发布于 2013-05-23 09:16:11
很有趣的问题。但我担心这不能用REPLACE或REGEXP语法在MySQL中完成。所以我写了一个小的wordpress插件,用于在文章中搜索和替换正则表达式。在使用插件之前,请确保备份了wp_posts表。
CREATE TABLE wp_posts_temp LIKE wp_posts;
INSERT INTO wp_posts_temp SELECT * FROM wp_posts;插件代码
将此代码放入wp-content/ plugin /replace-in-posts/文件夹中的replace.php文件中,并激活该插件。
<?php
/*
Plugin Name: Replace in posts
Author: Danijel
*/
add_action('admin_menu', 'replace_in_posts_menu');
function replace_in_posts_menu() {
add_options_page("Replace in posts", "Replace in posts", 'manage_options', "replace_in_posts", "replace_in_posts_admin");
}
function replace_in_posts_admin() {
if ( !empty($_POST['pattern']) && isset($_POST['replacement']) ) {
$pattern = stripslashes_deep( $_POST['pattern'] );
$replacement = stripslashes_deep( $_POST['replacement'] );
$count = replace_in_posts( '/'.$pattern.'/', $replacement );
}
?><div id="icon-options-general" class="icon32"></div><h2><?php echo 'Replace in posts' ?></h2>
<div class="wrap"><form method="post" action="<?php echo admin_url()."admin.php?page=".$_GET["page"] ?>">
Pattern (without delimiter) : <input type="text" name="pattern" value="">
Replacement: <input type="text" name="replacement" value="">
<input class="button-primary" type="submit" value="Replace" >
</form><?php if (isset($pattern)) : ?><p>Pattern "<?php echo $pattern; ?>" replaced in <?php echo ( $count ? $count : '0' ); ?> posts</p><?php endif; ?></div><?php
}
function replace_in_posts( $pattern, $replacement ) {
$myposts = get_posts('numberposts=-1');
$count = 0;
foreach ( $myposts as $post ) {
if ( preg_match( $pattern, $post->post_content ) ) {
$post->post_content = preg_replace( $pattern, $replacement, $post->post_content );
wp_update_post( $post );
$count++;
}
}
return $count;
}
?>正则表达式模式和您的问题的替换:
\[sc:youtube id="(\w+?)"\]
<iframe width="775" height="436" src="http://www.youtube.com/embed/$1?rel=0" frameborder="0" allowfullscreen></iframe>插件已经在该模式上进行了测试,没有出现错误。
https://stackoverflow.com/questions/16701954
复制相似问题