WordPress.org

Ready to get started?Download WordPress

Forums

Permalink Finder Plugin
Redirect Loop when slug matches `post_type` LIKE 'nav_menu_item' (13 posts)

  1. Jason Wilson
    Member
    Posted 1 year ago #

    I get a redirect Loop if the page requested matches the slug of a nav_menu item. We need a bit more filtering to make sure that it is only redirecting to pages or posts.
    Examples:
    http://www.stevenlouth.com/robbery/
    http://www.stevenlouth.com/burglary/

    http://wordpress.org/extend/plugins/permalink-finder/

  2. kpgraham
    Member
    Plugin Author

    Posted 1 year ago #

    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

  3. kpgraham
    Member
    Plugin Author

    Posted 1 year ago #

    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

  4. Jason Wilson
    Member
    Posted 1 year ago #

    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;
  5. Jason Wilson
    Member
    Posted 1 year ago #

    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.

  6. kpgraham
    Member
    Plugin Author

    Posted 1 year ago #

    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

  7. Jason Wilson
    Member
    Posted 1 year ago #

    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);
  8. kpgraham
    Member
    Plugin Author

    Posted 1 year ago #

    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

  9. Jason Wilson
    Member
    Posted 1 year ago #

    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.

  10. kpgraham
    Member
    Plugin Author

    Posted 1 year ago #

    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

  11. Jason Wilson
    Member
    Posted 1 year ago #

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

  12. kpgraham
    Member
    Plugin Author

    Posted 1 year ago #

    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

  13. Jason Wilson
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic