I have a wordpress set up, in which a series of special products are advertised, of which the name cannot be known, and whose pages cannot be entered.
With a 301 redirect I have solved that the product page cannot be entered.
And for greater security, I want the permanent link of the product to be a random string, that way no matter how much you try, you will not be able to enter the product page.
This is my PHP code:
function generateRandomUrl(){
$char = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'O', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'y', 'Y', 'x', 'X', 'z', 'Z');
$cont = 0;
$str = "";
while ($cont < 20){
$random = random_int(0, count($char)-1);
$str .= $char[$random];
$cont ++;
}
return $str;
}
$url = generateRandomUrl();
$post_data = array(
'post_author' => $usuarioPropietario,
'post_name' => $url, /* SLUG */
'post_title' => $_POST['name'],
'post_content' => $_POST['comment'],
'post_excerpt' => $_POST['contenido'],
'post_status' => 'pending', /* PENDIENTE */
'ping_status' => 'closed',
'post_type' => 'product',
);
$product_id = wp_insert_post( $post_data );
My problem is that when the product is created with an administrator or editor user this works correctly. But when a user who has registered on the page, and who is not an administrator or editor, all the content is correct, but the 'post_name' does not work.
If I'm not mistaken, it is the value of the 'post_name' field that assigns the slug or permalink to the Woocommerce product.
Is there any way, to force the value of the post_name field? In the case of normal users, it seems to take the value of the 'post_title' field.
You can hook your random function to action save_post
.
You can use my below code in functions.php
in
add_action( 'save_post', 'random_product_slug', 10, 3 );
function random_product_slug( $post_id, $post, $update ) {
// Only run on product
if ($post->post_type != 'product' || $post->post_status == 'auto-draft') {
return;
}
// temporary remove this action because if will make loop when we call wp_update_post
remove_action( 'save_post', 'random_product_slug', 10, 3 );
// update product
wp_update_post( array(
'ID' => $post_id,
'post_name' => generateRandomUrl() // random slug/post_name
));
// add this action to save_post again to make next product will have this action
add_action( 'save_post', 'random_product_slug', 10, 3 );
}
function generateRandomUrl() {
$char = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'O', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'y', 'Y', 'x', 'X', 'z', 'Z');
$cont = 0;
$str = "";
while ($cont < 20){
$random = random_int(0, count($char)-1);
$str .= $char[$random];
$cont ++;
}
return $str;
}
Is there any way, to force the value of the post_name field? In the case of normal users, it seems to take the value of the 'post_title' field.
Product slug only uses post_name. And all of the actions like create/update product will run through save_post
action.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.