• I have many old blogger permalinks that still cached on google and others directories. To redirect them to the correct posts I tried to use this function in my theme function.php.

    add_action('wp-head', 'wp_redirect_blogger_oldlink');
    function wp_redirect_blogger_oldlink() {
    $Rurl = $_SERVER['REQUEST_URI'];
    if (stripos($Rurl,'html') !== false) {
    $res = $wpdb->get_results("SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = 'blogger_permalink'");
    if ($res) {
    $Aurl= strstr($SERVER[REQUEST_URI], '', true);
    $Burl = strstr($_SERVER[REQUEST_URI], '.html', true);
    foreach ($res as $row) {
    if ( stripos($row->meta_value, $Burl) !== false || stripos($row->meta_value, $Aurl) !== false ) {
    wp_redirect( get_permalink($row->post_id), 301 );
    die();
    }}}}}

    [Moderator note: code fixed. Please wrap code in the backtick character or use the code button.]

    The function failed to work in my theme function.php but can works well when I put it in the 404.php, which is in redirection plugin log report counts as 404 reports.

    It seems like I have to use a right action hook to trigger the function “BEFORE” the redirection plugin in the theme function.php.

    Any ideas how to solve this problem?

    Thanks in advance.

    • This topic was modified 8 years, 6 months ago by bdbrown.

The topic ‘Action hook before redirection plugin’ is closed to new replies.