Forum Replies Created

Viewing 5 replies - 1 through 5 (of 5 total)
  • I tried that, but for some reason, $wpdb->prepare was adding extra quotes into the second argument for the $sql variable. I found when I moved the trailing slash to be part of the argument to $wpdb->prepare it worked, like so:

    $sql = $wpdb->prepare("SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type FROM $wpdb->posts  ".
                        "LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
                        "  meta_key = 'custom_permalink' AND ".
                        "  meta_value != '' AND ".
                        "  ( LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) OR ".
                        "    LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) ) ".
                        "  AND post_status != 'trash' AND post_type != 'nav_menu_item'".
                        " ORDER BY LENGTH(meta_value) DESC, ".
                        " FIELD(post_status,'publish','private','draft','auto-draft','inherit'),".
                        " FIELD(post_type,'post','page'),".
                        "$wpdb->posts.ID ASC  LIMIT 1",$request_noslash,$request_noslash . "/");

    I actually decided to work within the framework of WP itself, and applied this (better) fix than my above fix. WordPress provides SQL sterilization natively if the right commands are use, so here it is, with no warranties:

    In the WP plugins directory, in custom-permalinks, in custom-permalinks.php find the following lines:

    $sql = “SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type FROM $wpdb->posts “.
    “LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE “.
    ” meta_key = ‘custom_permalink’ AND “.
    ” meta_value != ” AND “.
    ” ( LOWER(meta_value) = LEFT(LOWER(‘”.mysql_real_escape_string($request_noslash).”‘), LENGTH(meta_value)) OR “.
    ” LOWER(meta_value) = LEFT(LOWER(‘”.mysql_real_escape_string($request_noslash.”/”).”‘), LENGTH(meta_value)) ) “.
    ” AND post_status != ‘trash’ AND post_type != ‘nav_menu_item'”.
    ” ORDER BY LENGTH(meta_value) DESC, “.
    ” FIELD(post_status,’publish’,’private’,’draft’,’auto-draft’,’inherit’),”.
    ” FIELD(post_type,’post’,’page’),”.
    “$wpdb->posts.ID ASC LIMIT 1”;

    Replace them with:

    $sql = $wpdb->prepare(“SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type FROM $wpdb->posts “.
    “LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE “.
    ” meta_key = ‘custom_permalink’ AND “.
    ” meta_value != ” AND “.
    ” ( LOWER(meta_value) = LEFT(LOWER(‘%s’), LENGTH(meta_value)) OR “.
    ” LOWER(meta_value) = LEFT(LOWER(‘%s’), LENGTH(meta_value)) ) “.
    ” AND post_status != ‘trash’ AND post_type != ‘nav_menu_item'”.
    ” ORDER BY LENGTH(meta_value) DESC, “.
    ” FIELD(post_status,’publish’,’private’,’draft’,’auto-draft’,’inherit’),”.
    ” FIELD(post_type,’post’,’page’),”.
    “$wpdb->posts.ID ASC LIMIT 1”,$request_noslash,$request_noslash . “/”);

    This restored my permalinks to working order. As always, make a backup of the custom-permalinks.php file before modifying it in case something goes horribly wrong. Good luck!

    In the WP plugins directory, in custom-permalinks, in custom-permalinks.php find the following lines:

    $sql = “SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type FROM $wpdb->posts “.
    “LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE “.
    ” meta_key = ‘custom_permalink’ AND “.
    ” meta_value != ” AND “.
    ” ( LOWER(meta_value) = LEFT(LOWER(‘”.mysql_real_escape_string($request_noslash).”‘), LENGTH(meta_value)) OR “.
    ” LOWER(meta_value) = LEFT(LOWER(‘”.mysql_real_escape_string($request_noslash.”/”).”‘), LENGTH(meta_value)) ) “.
    ” AND post_status != ‘trash’ AND post_type != ‘nav_menu_item'”.
    ” ORDER BY LENGTH(meta_value) DESC, “.
    ” FIELD(post_status,’publish’,’private’,’draft’,’auto-draft’,’inherit’),”.
    ” FIELD(post_type,’post’,’page’),”.
    “$wpdb->posts.ID ASC LIMIT 1”;

    Replace them with:

    $sql = $wpdb->prepare(“SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type FROM $wpdb->posts “.
    “LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE “.
    ” meta_key = ‘custom_permalink’ AND “.
    ” meta_value != ” AND “.
    ” ( LOWER(meta_value) = LEFT(LOWER(‘%s’), LENGTH(meta_value)) OR “.
    ” LOWER(meta_value) = LEFT(LOWER(‘%s’), LENGTH(meta_value)) ) “.
    ” AND post_status != ‘trash’ AND post_type != ‘nav_menu_item'”.
    ” ORDER BY LENGTH(meta_value) DESC, “.
    ” FIELD(post_status,’publish’,’private’,’draft’,’auto-draft’,’inherit’),”.
    ” FIELD(post_type,’post’,’page’),”.
    “$wpdb->posts.ID ASC LIMIT 1”,$request_noslash,$request_noslash . “/”);

    This restored my permalinks to working order. As always, make a backup of the custom-permalinks.php file before modifying it in case something goes horribly wrong. Good luck!

    On lines 167 and 168 of custom-permalinks.php, there are references to a function called mysql_real_escape_string. This function does not seem to be supported any more. I found simply removing the references seemed to work, though I cannot say it would work for other sites.

    Original lines:

    ” ( LOWER(meta_value) = LEFT(LOWER(‘”.mysql_real_escape_string($request_noslash).”‘), LENGTH(meta_value)) OR “.
    ” LOWER(meta_value) = LEFT(LOWER(‘”.mysql_real_escape_string($request_noslash.”/”).”‘), LENGTH(meta_value)) ) “.

    My changes:

    ” ( LOWER(meta_value) = LEFT(LOWER(‘”.$request_noslash.”‘), LENGTH(meta_value)) OR “.
    ” LOWER(meta_value) = LEFT(LOWER(‘”.$request_noslash.”/’), LENGTH(meta_value)) ) “.

    Remember to make backups of any files you modify BEFORE you modify.

    Hope this helps!

    The problem is with mysql_real_escape_string in custom_permalinks.php. There seem to be some solutions online, but I found simply removing that part of the code worked best for now, at least if you’re familiar with doing so.

Viewing 5 replies - 1 through 5 (of 5 total)