且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WooCommerce:只有产品名称中的数字 - 问题

更新时间:2023-11-30 12:24:40

我创建了一个钩子.

如果只有产品名称中的数字 - 函数会在 Slug 中添加-product".

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

即使您编辑了 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;
}