• Hello
    I use code number 1 in functions.php and number 2 in .htaccess to get Products Permalink based on SKU.
    Something like this: xyz.com/product-title/#sku
    How could I remove “#” from the permalink (in front of the sku)?
    When I remove it manually from the code number 1 without changing the code number 2, the links will become correct but they go to 404 page.
    Also what should I do if I wanted to change my products URLs to something like this: xyz.com/sku/product-title/

    Code 1
    "function jpb_custom_meta_permalink( $link, $post ){
    $post_meta = get_post_meta( $post->ID, '<insert your meta key here>', true );
    if( empty( $post_meta ) || !is_string( $post_meta ) )
    $post_meta = '<insert your default value (could be an empty string) here>';
    $link = str_replace( '!!custom_field_placeholder!!', $post_meta, $link );
    return $link;}
    add_filter( 'post_link', 'jpb_custom_meta_permalink', 10, 2 );
    function append_sku_string( $link, $post ) {
    $post_meta = get_post_meta( $post->ID, '_sku', true );
    if ( 'product' == get_post_type( $post ) ) {
    $link = $link . '#' .$post_meta;
    return $link;}
    }
    add_filter( 'post_type_link', 'append_sku_string', 1, 2 );"
    ===============================================
    Code 2
    "<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^whisky/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$ ?sku=$1&product=$2 [NC,L]
    </IfModule>"
    • This topic was modified 2 weeks, 4 days ago by bcworkz. Reason: formatting fixed
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    How could I remove “#” from the permalink (in front of the sku)?

    Don’t use it in your append_sku_string() function ??
    $link = $link . $post_meta;

    I’m sure you knew that much. The real question is how to avoid the resulting 404s.

    Instead of using .htaccess rewrite rules (aside from the WP default), use WP rewrite rules that will match against your URL structure and rewrite the request into something WP will understand. Matching would help immensely if your permastruct had a static element in it for ease in matching. If you went with just “xyz.com/sku/product-title/” it would conflict with other WP permalinks and be difficult to match independently. A better URL would similar to “xyz.com/sku/12345678/whiskey/” where the “/sku/” part is always there and unchanging. This request might be rewritten to “index.php?sku=12345678&product=whiskey”. Presumably you have other code that can use passed query string values to create an appropriate query.

    Thread Starter Abdolaziz

    (@abdolaziz)

    I appreciate the time you spent for me, but as I’m not a developer and only designer, I don’t know how to accomplish what you said.
    The second part of my Q is not so important to me but the first part (Removing # from links and make links not to go to 404) is important to me. If you help me to write (or fix) the second code to put in .htaccess or some where else I’d appreciate it. For developers like you it’s a cup of tea!

    Thanks

    Moderator bcworkz

    (@bcworkz)

    Remove the hash mark in append_sku_string() by changing the if() conditional to this:

    if ( 'product' == get_post_type( $post ) ) {
           $link = $link . $post_meta;
           return $link;
    }

    But if you want the SKU to occur before the product name you could search/replace it into place. For example:
    $link = str_replace( '.com/', ".com/sku/$post_meta/", $link);
    This is assuming your domain name uses a .com TLD.

    This kind of link will 404 unless you add a custom rewrite rule to handle it. For example:

    function custom_rewrite_rule() {
        add_rewrite_rule('^sku/([^/]*)/([^/]*)/?','index.php?sku=$matches[1]&product=$matches[2]','top');
    }
    add_action('init', 'custom_rewrite_rule', 10, 0);

    After adding this last code snippet you must visit the permalinks settings admin screen, which causes all rewrite rules to be regenerated. This assumes that “sku” and “product” query vars already have meaning in your installation. This will match and rewrite an URL such as “xyz.com/sku/12345678/product-name/” into a index.php request with appropriate query string vars. This index.php request is done behind the scene, end users never see it.

    FYI, when someone posts in the “Developing” sub-forum, most other members assume the poster has some coding skills. It’s OK if you don’t, but be ready for others to assume you know more than you do ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.