WooCommerce: only numbers in product name - issue

2019-06-14 08:29发布

I have a website using WooCommerce.

I have found an issue: if product name including only numbers - you can't view this product. Browser is showing something like Category page.

If I add literal characters into the slug - the link is working correctly, but I can't do that all the time.

I thought that this problem creates one of the installed plugins. I disabled all plugins one by one (except WooCommerce), but it did not help.

How do I need to fix that?

woocommerce: only numbers in product name - issue

1条回答
Viruses.
2楼-- · 2019-06-14 09:17

I created a hook.

If only the numbers present in the product name - function adds "-product" to the Slug.

Even if you edit the slug and leave it only numbers - the function will work again

add_filter('wp_insert_post_data', 'edit_slug', 10);

function edit_slug($data) {
    global $post_ID;

    if (!empty($data['post_name']) && $data['post_status'] == "publish" && $data['post_type'] == "product") {
        if (ctype_digit($data['post_name'])) {
            $data['post_name'] = sanitize_title($data['post_title'], $post_ID);
            $data['post_name'] .= '-' . 'product';
        }
    }
    return $data;
}
查看更多
登录 后发表回答