WordPress.org

Support

Support » Plugins and Hacks » Redirect Loop when slug matches `post_type` LIKE 'nav_menu_item'

Redirect Loop when slug matches `post_type` LIKE 'nav_menu_item'

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author kpgraham

    @kpgraham

    I will fix it to look only for pages or posts.

    I think it should perhaps be a WHERE NOT for menus, etc so that custom post types make it through the search.

    Keith

    Plugin Author kpgraham

    @kpgraham

    OK,

    I added a filter to not redirect attachments or menu items.

    Please try downloading the beta test on http://www.blogseye.com and installing it.

    Please let me know if it works.

    Keith

    Here’s the patch that I created:

    # This patch file was generated by NetBeans IDE
    # It uses platform neutral UTF-8 encoding and \n newlines.
    --- <html>pf-404.php (<b>Today 10:10:07 AM</b>)</html>
    +++ <html><b>Current File</b></html>
    @@ -9,6 +9,7 @@
    
    +
     // this does the work of the 404 processing. It is not loaded unless there is a 404.
     // Guts of the plugin. This is where we do the redirect. We are already in a 404 before we get here.
    
    @@ -598,8 +599,6 @@
             $sql = $sql . " if(INSTR(CONCAT('-',LCASE(post_name),'-'),'-" . mysql_real_escape_string($ss[$j]) . "-'),1,0)+";
         }
         $sql = $sql . "0 as CNT FROM " . $wpdb->posts . " WHERE post_status = 'publish' " . kpg_post_type_filter() . " ORDER BY CNT DESC, post_modified DESC LIMIT 1";
    -    //mod by JJW 2013-04-04 above; filter   inclusive,  we should have an option with other types to allow
    -
         $row = $wpdb->get_row($sql);
         if ($row) {
             $ID = $row->ID;
    @@ -796,13 +795,12 @@
             $f.=')';
         }
         return $f;
    -
     }
    
     // do a quick check to see if the sanitized slug can be found
     function kpg_find_permalink_post_direct($flink) {
         global $wpdb; // useful db functions
    -    $sql = "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_status = 'publish'" . kpg_post_type_filter(); //  AND post_type NOT LIKE 'nav_menu_item'
    \ No newline at end of file
    +    $sql = "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_status = 'publish'" . kpg_post_type_filter();
    \ No newline at end of file
         $post = $wpdb->get_var($wpdb->prepare($sql, $flink));
         if (!empty($post))
             return $post;

    Thanks for the quick response. I’ll try your patch on another site that had the same issue.

    There are many other post types that may get through such as contact forms. I elected to go with an inclusive list. In time we could add inclusive or exclusive list fields to the settings panel.

    Plugin Author kpgraham

    @kpgraham

    I need kpg_post_type_filter function – I don’t think it is mine. I take it it that it returns the sql for filtering the post types.

    I hard coded attachment and nav_menu_item in my revisions.

    Keith

    function kpg_post_type_filter($types = array('page', 'post')) {
        $f = '';
        if (count($types) > 0) {
            $f = ' AND (';
            for ($i = 0; $i < count($types); $i++) {
                $f.=($i == 0 ? '' : ' OR') . " post_type LIKE '" . $types[$i] . "'";
            }
            $f.=')';
        }
        return $f;
    }

    Thanks again, I tested beta on http://johnrfuller.com/ and it works great.

    Another minor code addition around line 46 of BETA pf-404.php:

    $plink=str_replace('.aspx','',$plink);

    Plugin Author kpgraham

    @kpgraham

    The function has to be tweaked. By default it doesn’t redirect custom post types. I will have to think about it.

    I added aspx. It has been a long time since I coded much asp so I missed that one.

    Thanks,

    Keith

    Just had to tweak on of my favorite plugins.

    I had redirect loops from things like:
    domain.com/?p=17
    domain.com/?cat=3

    Around line 310 of pf-404.php

    $r404[6] = "empty search, send to home";
            kpg_find_permalink_fixed_log($options, $f404, $r404, $stats);
    wp_redirect($flink . $query, (int) $kpg_pf_301);

    wp_redirect line Becomes:

    wp_redirect($flink, (int) $kpg_pf_301);

    Now it can go home for real.

    Plugin Author kpgraham

    @kpgraham

    Unfortunately this breaks the ability to preserve the query, which is important to some people.

    I wll look into a way to keep the query, but not loop when there is a query 404.

    Keith

    If I get some time I’ll look into making a redirect-loop detection function.

    Plugin Author kpgraham

    @kpgraham

    My test version (available on blogseye.com) now has a check to see that the redirect is not to the current page. It should help somewhat.

    Keith

    Looks Good.
    The redirect-loop detection works great.
    I have your beta version on several sites and am happy with the results.

    It also handles malformed urls like: domain.com/1234/ where the slug is a number. As we know WordPress doesn’t handle numeric slugs without help.

    I bet the rest of the community will be happy to see it pushed live.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Redirect Loop when slug matches `post_type` LIKE 'nav_menu_item'’ is closed to new replies.