Quantcast
Channel: Topic Tag: plugin | WordPress.org
Viewing all articles
Browse latest Browse all 26892

toddhol on "[Plugin: WP Permastructure] WP 4.0 broke my custom permalinks"

$
0
0

When I updated to WordPress v4.0 it removed all of my custom post type permalinks and replaced them with "/". In other words, clicking on a custom permalink takes you to the homepage.

I have 2 custom post types: "Business" and "Coupon". The permalink settings for Business are "/%business_cat%/%postname%/" with has_archive: false / with_front: true

For Coupon, settings are "/coupon/%postname%/" with has_archive: false / with_front: false.

The box is unchecked for "Use custom permalink of custom taxonomy archive"

here is post-types.php

<?php

// Fire this during init
register_post_type('business', array(
    'label' => __('Businesses'),
    'singular_label' => __('Business'),
    'public' => true,
    'show_ui' => true,
    'capability_type' => 'page',
    'hierarchical' => true,
    'hierarchical_uri' => true,
    'query_var' => 'business',
    'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields', 'comments', 'revisions', 'page-attributes'),
    'taxonomies' => array('business_cat'),
    'rewrite' => array('slug' => 'business', 'with_front' => true)
));

register_taxonomy('business_cat', array('business'), array(
    'label' => __('Business Categories'),
    'singular_label' => __('Business Category'),
    'hierarchical' => true,
    'hierarchical_uri' => true,
    'show_ui' => true,
    'query_var' => 'business_cat',
    'rewrite' => array('slug' => 'businesses', 'with_front' => true)
    // 'with_front' => false
));

register_post_type('coupon', array(
    'label' => __('Coupons'),
    'singular_label' => __('Coupon'),
    'public' => true,
    'show_ui' => true,
    'capability_type' => 'page',
    'hierarchical' => true,
    'hierarchical_uri' => true,
    'rewrite' => false,
    'query_var' => 'coupon',
    //'rewrite' => array('slug' => 'coupon', 'with_front' => false),
    // 'with_front' => false
    'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields', 'comments', 'revisions', 'page-attributes')
));

add_action('init', '_business_taxonomy');
function _business_taxonomy() {
    global $wp_rewrite;

    //$wp_rewrite->add_permastruct('business_cat', '%business_cat%', true, true);

    $wp_rewrite->extra_permastructs['business_cat'] =
        array('businesses/%business_cat%', false);
    $wp_rewrite->add_rewrite_tag("%business_cat%", '.*?/?([^/]+)', "business_cat=");

    $wp_rewrite->extra_permastructs['business'] =
        array('business/%business_cat%/%business%', false); // business/%business_cat%/%business%
    $wp_rewrite->add_rewrite_tag("%business%", '([^/]+)', "business=");
    //print_r($wp_rewrite);
}

add_filter('term_link', '_hierarchical_terms_hierarchical_links', 12, 3);
function _hierarchical_terms_hierarchical_links($termlink, $term, $taxonomy) {
    global $wp_rewrite;

    if ( 'business_cat' != $taxonomy ) // Change 'state' to your required taxonomy
        return $termlink;

    $termstruct = $wp_rewrite->get_extra_permastruct($taxonomy);

    if (empty($termstruct)) // If rewrites are disabled, fall back to the current link
        return $termlink;

    if ( empty($term->parent) ) // Fall back to the current link for parent terms
        return $termlink;

    $nicename = $term->slug;

    while ( !empty($term->parent) ) {
        $term = get_term($term->parent, $taxonomy);
        $nicename = $term->slug . '/' . $nicename;
    }

    $termlink = str_replace("%$taxonomy%", $nicename, $termstruct);
    $termlink = home_url( user_trailingslashit($termlink, 'category') );

    return $termlink;
}

add_filter('post_link', '_hierarchical_posts_hierarchical_links', 8, 3);
add_filter('post_type_link', '_hierarchical_posts_hierarchical_links', 8, 3);
function _hierarchical_posts_hierarchical_links($permalink, $post, $leavename) {
    global $wp_rewrite;

    if ($post->post_type != 'business')
        return $permalink;

    $postlink = $wp_rewrite->get_extra_permastruct('business');

    $post_name = $post->post_name;

    if (!empty($post->post_parent)) {
        $parent_posts = get_post($post->post_parent);
        $post_name = $parent_posts->post_name . '/' . $post_name;
    }

    $postlink = str_replace("%business%", $post_name, $postlink);

    $terms = get_the_terms($post->ID, 'business_cat');
    if (!empty($terms)) {
        foreach ($terms as $term_id => $term) {
            $termname = $term->slug . '/' . $termname;
        }
        $termname = substr($termname, 0, -1); // remove the trailing slash
    }

    $postlink = str_replace("%business_cat%", $termname, $postlink);

    $postlink = str_replace("//", '/', $postlink);

    return home_url(user_trailingslashit($postlink, 'single'));
}

//add_action('init', 'flush_rewrite_rules');

?>

https://wordpress.org/plugins/wp-permastructure/


Viewing all articles
Browse latest Browse all 26892

Trending Articles