嘿,你可能会很幸运prior plugin I wrote (here\'s the code itself). 这是一个重定向插件,它可以查看传入的URL,如果要生成404,则根据Posteta表进行检查,如果找到匹配项,则重定向用户。
如果将整个URI存储在帖子的自定义字段中,它可能看起来像:
/**
* Redirect old Tumblr URLs to new WP if the URI exists in the database
*/
function tumblr_legacy_redirect() {
global $wpdb; // We\'re going to use this for the db lookup
// Only run this lookup on URLs that are going to 404 anyway
if ( is_404() ) {
// We\'re getting the incorrect URI in hopes that it\'s an old Tumblr link
$requested_url = $_SERVER[\'SERVER_NAME\'] . $_SERVER[\'REQUEST_URI\'];
// Prepare the query so we protect ourselves against bad SQL queries
$query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key=\'syndication_permalink\' AND meta_value=\'%s\'", $requested_url );
$post_id = $wpdb->get_results( $query, \'ARRAY_N\' );
// Catch if there are duplicate results in the database
$post_id = $post_id[0][0];
// Build the redirect if the post_id exists
if ( $new_url = get_permalink( $post_id ) ) {
wp_redirect( $new_url, 301 );
} else {
return;
}
} // END - if ( is_404() )
} // END - tumblr_legacy_redirect()
// A good place for our template redirect to hook into
add_action( \'template_redirect\', \'tumblr_legacy_redirect\' );
需要注意的是,我还没有实际测试代码,所以如果您遇到任何错误,请告诉我!