WordPress Custom MD5 slug page not found - wordpress

My "werknemers" post type need an MD5 generated slug to make them unique. In order to do that, I have added the following code:
function isValidMd5($md5 =''){
return preg_match('/^[a-f0-9]{32}$/', $md5);
}
function custom_unique_post_slug( $slug, $post_ID, $post_status, $post_type ) {
if(isValidMd5($slug)) { } else {
if ( 'werknemers' == $post_type ) {
$slug = md5( time() );
}
}
return $slug;
}
add_filter( 'wp_unique_post_slug', 'custom_unique_post_slug', 10, 4 );
Works perfectly however, the posts are now not accessible, giving a "Page not found" error. Changing the permalinks doesn't help and neither did resetting ".htaccess". I assume I need something specific to be placed in ".htaccess", but I don't know what. Any ideas?

Because I removed the post type slug, the post couldn't be found.
It actually had nothing to do with the MD5 generator.
To fix it, I had to apply the following code (in case anyone else has this problem)
function na_remove_slug( $post_link, $post, $leavename ) {
if ( 'werknemers' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );
function na_parse_request( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'werknemers', 'page' ) );
}
}
add_action( 'pre_get_posts', 'na_parse_request' );

Related

Get Post ID of post you're currently editing in WordPress

I'm attempting to get the ID of the post I am editing in functions.php for the purpose of dynamically rewriting a custom post type slug.
This is what I'm working with so far.
function change_post_type_slug( $args, $post_type ) {
if ( 'custom_post' == $post_type ) {
global $post;
$location = get_field('custom_field', $post->ID);
$args['rewrite']['slug'] = $location;
}
return $args;
}
add_filter( 'register_post_type_args', 'change_post_type_slug', 10, 2 );
I am not sure if the hook register_post_type_args is firing before I am able to get the ID, or if this is even the best way to go about what I am trying to accomplish. Can't find much out there on the subject.
I was able to get it to work with the following:
function change_post_type_slug( $args, $post_type ) {
if ( 'lead_page' == $post_type ) {
$post_id = $_GET['post'];
$location = get_field('leadpage_location', $post_id);
$args['rewrite']['slug'] = $location->post_name;
}
return $args;
}
add_filter( 'register_post_type_args', 'change_post_type_slug', 10, 2 );
However it resulted in a notice on the front-end:
Notice: Undefined index: post in /path/to/wordpress/functions.php on line 623
Line 623 is $post_id = $_GET['post'];
You should use the updated_postmeta hook for this, as its run every time you update your custom fields.
Then you can update your post data with wp_update_post() function.
add_action( 'updated_postmeta', function( $meta_id, $object_id, $meta_key, $meta_value ) {
if ( 'location' === $meta_key ) {
wp_update_post([
'ID' => $object_id,
'post_name' => $meta_value,
]);
}
}, 10, 4 );
Update:
Try this:
function change_post_types_slug( $args, $post_type ) {
if ( 'your-custom_post' === $post_type ) {
// Check and get the custom post ID
$id = isset($_GET[ 'post' ]) ? $_GET[ 'post' ] : '' ;
// $location = get_field('leadpage_location', $id);
$args['rewrite']['slug'] = 'new-slug-here';
}
return $args;
}
add_filter( 'register_post_type_args', 'change_post_types_slug', 10, 2 );
Try this out:
function change_post_type_slug( $args, $post_type ) {
if ( 'lead_page' === $post_type && is_admin() && $_GET['action'] === 'edit' ) {
$post_id = $_GET['post'];
$location = get_field('leadpage_location', $post_id);
$args['rewrite']['slug'] = $location->post_name;
}
return $args;
}
add_filter( 'register_post_type_args', 'change_post_type_slug', 10, 2 );
It adds two more conditionals, to check if you're on the admin screen and to check of there is a GET parameter of edit. Probably overkill to do is_admin() as well, but now you're super safe.

How to rewrite URL of custom post type without post type in URL

I am working on a tour site. Where they have a custom post type built with a custom plugin.
That custom post type has base slug like "/destination/".$post->post_name.
They want to me to remove that base slug so that it could be only $post->post_name
I have tried a code from internet listed below.
It works for single level for that destination.
But when I have a parent destination like New York in United State America.
So it does not work.
Here is an example:
function update_destination_page_link_filter($post_link, $post, $leavename = null, $sample = null ) {
if ( $post->post_type === 'destination' ) {
$post_link = str_replace('/destination/', '/', $post_link);
if($post->post_parent !== 0){
$parent_slug = get_parent_link($post->post_parent, '');
$post_link = '/'.$parent_slug.$post->post_name;
}
$post_link = update_url_base( $post, $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'update_destination_page_link_filter', 1, 3 );
function allow_destination_direct_by_name( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query )
|| ! isset( $query->query['page'] ) )
return;
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) )
$query->set( 'post_type', array( 'post', 'destination', 'page' ) );
}
add_action( 'pre_get_posts', 'allow_destination_direct_by_name', 1);
Single Level
http://siteurl/united-state-america works well
http://siteurl/united-state-america/new-york not working. It should open the new-york location page but it is showing 404
It may also be more specification in location
Like http://siteurl/united-state-america/new-york/brooklyn
The following code may help you, in this regards.
add_filter( 'post_type_link', 'my_post_type_link', 10, 3);
function my_post_type_link($permalink, $post, $leavename) {
if ($post->post_type == <your-post-type>)
{
$p_name=$post->post_name;
$parent_slug = get_parent_link($post->post_parent, '');
if (isset($parent_slug) && !empty($parent_slug))
{
$permalink = home_url( "" . $parent_slug . "/" . $p_name . "/");
}
}
return $permalink;
}
add_filter( 'rewrite_rules_array', 'my_rewrite_rules_array');
function my_rewrite_rules_array($rules) {
$rules = array('([^/]*)/([^/]*)/?$' => 'index.php?post_type=<your-post-type>&name=$matches[2]&meta=$matches[1]') + $rules;
return $rules;
}

Wordpress: remove one category in permaling /%category%/%postname%/

I am looking for a solution in order to remove /%category%/ from my permalinks for only one category ("general").
My permalink is currently set to /%category%/%postname%/. How can I create the following URLs only for the "general" category /%postname%/ ?
Thanks!
You can set custom structure in Permalinks Settings: Dashboard - Settings>Permalinks
This works for me:
function remove_uncategorized( $permalink, $post, $leavename ) {
if( $post->post_type != 'post' ) return $permalink;
$cats = get_the_category($post->ID);
if( ! count($cats) ) return $permalink;
usort($cats, '_usort_terms_by_ID');
$category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
$category_object = get_term( $category_object, 'category' );
return _clear_uncategorized($category_object, $permalink);
}
function _clear_uncategorized($cat, $permalink) {
if( $cat->slug == 'sin-categoria' ) {
return str_replace('%category%/', '', $permalink);
}
$parent = $cat->parent;
if ( !$parent )
return $permalink;
return _clear_uncategorized($parent, $permalink);
}
add_filter( 'pre_post_link', 'remove_uncategorized', 9, 3 );

How to remove taxonomy slug from custom post type url?

I have a custom post type(product) with taxonomy product-type. One of my url like this:
http://www.naturesbioscience.com/product-type/immune-support-supplements/
I want this like:
http://www.naturesbioscience.com/immune-support-supplements/
I have used "rewrite" => array('slug' => '/ ', 'with_front' => false in register_taxonomy function and I got the url like:
http://www.naturesbioscience.com/immune-support-supplements/
But I got 404 not found in other pages.
Anyone can help me?
I think you forgot to rewrite custom taxonomy post slug.
Write this in your register_post_type methord.
'rewrite' => array('slug' => 'product-type')
Now you have to remove product-type slug from your custom products
/**
* Remove the slug from published post permalinks.
*/
function custom_remove_cpt_slug($post_link, $post, $leavename)
{
if ('product-type' != $post->post_type || 'publish' != $post->post_status)
{
return $post_link;
}
$post_link = str_replace('/' . $post->post_type . '/', '/', $post_link);
return $post_link;
}
add_filter('post_type_link', 'custom_remove_cpt_slug', 10, 3);
Now as you have removed the custom post type slug so WordPress will try to match it with page or post so you have tell WP to check the URL in you custom post type also. So use this for that:
function custom_parse_request_tricksy($query)
{
// Only noop the main query
if (!$query->is_main_query())
return;
// Only noop our very specific rewrite rule match
if (2 != count($query->query) || !isset($query->query['page']))
{
return;
}
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if (!empty($query->query['name']))
{
$query->set('post_type', array('post', 'product-type', 'page'));
}
}
add_action('pre_get_posts', 'custom_parse_request_tricksy');
Reference: Remove The Slugs from Custom Post Type URL
Hope this helps!
add_filter( 'post_type_link', 'change_product_request', 10, 3 );
function change_product_request( $post_link, $post, $leavename ) {
if ( 'product' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( "/{$post->post_type}/" , '/', $post_link );
return $post_link;
}
Now, you'll get a 404 page because WordPress only allow posts and pages to behave in this way. You'll also have to add the following:
add_action( 'pre_get_posts', 'product_permalink' );
function product_permalink( $query ) {
if ( ! $query->is_main_query() ) {
return;
}
if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['pagename'] ) ) { // name has been updated to pagename so $query->query['pagename']
global $wpdb;
$pt = $wpdb->get_var(
"SELECT post_type FROM `{$wpdb->posts}` " .
"WHERE post_name = '{$query->query['pagename']}'"
);
$query->set( 'post_type', $pt );
}
return $query;
}
I had similar issue. The question is quite old, but anyone looking for apt answer can see this.
Do not pass "/" for rewrite slug, since it causes more problem than it solves, as in this case, causing 404 error in other pages.
First, we need to remove the slug from the URL for the published post. Paste the code in functions.php
/**
* Remove the slug from published post permalinks. Only affect our CPT though.
*/
function sh_remove_cpt_slug( $post_link, $post, $leavename ) {
if ( in_array( $post->post_type, array( 'product-type' ) )
|| 'publish' == $post->post_status )
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'sh_remove_cpt_slug', 10, 3 );
This will still cause error since it specifies that only 'post' and 'page' post types can have url without post-type slug.
Now to teach WP that out CPT will also have URL without slug, we need to get this in our functions.php
function sh_parse_request_tricksy( $query ) {
// Only loop the main query
if ( ! $query->is_main_query() ) {
return;
}
// Only loop our very specific rewrite rule match
if ( 2 != count( $query->query )
|| ! isset( $query->query['page'] ) )
return;
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'product-type' ) );
}
}
add_action( 'pre_get_posts', 'sh_parse_request_tricksy' );
This is it. Ref: https://wordpress.stackexchange.com/a/320711/98322

How to replace the custom post slug by the category name in wordpress

I need to change the permalink struture of my custom post "product"
the default one is Domain_name.com./product/product_name
I need to change it to :
Domain_name.com./category_name/product_name
i used the plugin "Types" to create the custom post
i installed a plugin named wp-permastructure to change the permalink but
when i add %category%/%postname%/ in the new format structure i get a 404 error.
i add the follow code to remove the product slug :
function gp_remove_cpt_slug( $post_link, $post, $leavename )
{
if ( 'product' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link);
return $post_link;
}
add_filter( 'product', 'gp_remove_cpt_slug', 10, 3 );
function gp_parse_request_trick( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 3!= count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'product', 'page' ) );
}
}
add_action( 'pre_get_posts', 'gp_parse_request_trick' );
But i still get the 404 error
have you some ideas for my problem

Resources