How to generate random numeric slugs for a custom post type?

function wpse_18293_get_random_string ($number_of_chars_to_use) { $upper_case_charachters = range(‘A’, ‘Z’); while($cnt < $number_of_chars_to_use) { $random_string.= $upper_case_charachters[mt_rand(0, count($upper_case_charachters)-1)]; $cnt++; } return $random_string } $random_string = wpse_18293_get_random_string(’10’); The ’10’ can be any number, and the function will pass back a string of random charachters that many charachters long. Note, the function name can be anything you want, I named … Read more

custom slug always ends in “-2”

Modification of a slug happens when wordpress detect that the slug is already in use. “In use” in this case includes the posts that are trashed. In your case if you don’t need the id to be associated with the date then you might get better result by using the post id as the id, … Read more

Set taxonomy slug as taxonomy title

The terms are inserted with wp_insert_term() and updated with wp_update_term(). There’s the pre_insert_term filter, where one can modify the term’s name but not the slug. If we dig further, we see that both functions call sanitize_term() that again calls the sanitize_term_field() function. There we have various filters available. Example #1 Here’s one combination if we … Read more

What user roles should have wp_unique_post_slug_is_bad_flat_slug filter applied?

Actually you should skip the condition altogether – you don’t need to worry about permissions or context here, you are merely saying: If WordPress, for whatever reason, is determining if a slug is bad: run my additional conditions Currently, if a theme/front-end plugin/REST API call was triggering the filter (i.e. insert/updating a post), your checks … Read more

Add Slug Metabox to posts for contributors

I approached by doing the opposite with using remove_meta_box() for the Slug metabox if the user is not an administrator or contributor: add_action( ‘admin_menu’, ‘wpse_237291_remove_slug_metabox’ ); function wpse_237291_remove_slug_metabox() { $user = wp_get_current_user(); $allowed_roles = array( ‘administrator’, ‘contributor’ ); if ( !array_intersect( $allowed_roles, $user -> roles ) ) { remove_meta_box( ‘slugdiv’, ‘post’, ‘normal’ ); } }

Load a template page based on part of slug in wordpress

You can filter template_include, check the request URL for your search words and return a custom template. Here is a primitive example: add_filter( ‘template_include’, function( $template ) { $template_map = [ ‘advice-on’ => ‘advice’, ‘links-‘ => ‘link-collection’, ]; foreach ( $template_map as $find => $match ) { if ( 0 === strpos( $_SERVER[ ‘REQUEST_URI’ ], … Read more