Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

custom taxonomy and permalink rewrite WordPress

  • SOLVED

I've registered custom taxonomy by using this code:
<?php
add_action( 'init', 'build_taxonomies', 0 );
function build_taxonomies() {
register_taxonomy( 'event_type', 'post', array( 'hierarchical' => true, 'label' => 'Event Type', 'query_var' => true, 'rewrite' => true ) );
} ?>


Currently, my url looks like this sample.com/event_type/conventions but I need to remove event_type out of the URL so it becomes sample.com/conventions

I know it's possible through wp_rewrite in functions file, but I lack enough knowledge to actually do it.Would appreciate help. Should be pretty easy for a knowledgeable coder based on Codex information I've read.
[[LINK href="http://codex.wordpress.org/Function_Reference/WP_Rewrite"]]codex.wordpress.org/Function_Reference/WP_Rewrite[[/LINK]]

Thanks.

Answers (5)

2010-07-28

wjm answers:

Hi Viktor,
here is the code that does what you need.

add this line to your functions.php
it can go right bellow build_taxonomies()

require_once( TEMPLATEPATH . '/rewrite_rules_event_type.php');

and save the following file as "rewrite_rules_event_type.php" in the same dir as functions.php
http://wordpress.pastebin.com/SpxbmFgV

it will do the job.
I tested it locally.

after running for the first time, and having it to work,
comment this line
$flush_rules = TRUE;
to improve perfomance (that avoids having to update the rewrite rules to the database)

let me know if you need anything else.
- wjm


Viktor Nagornyy comments:

I wish this would be that easy. For some reason it's not working. Does it matter how my taxonomy.php is structured?

This is what my functions.php looks like in case something in there matters. The new file is in the theme's dir. I even resaved permalinks to be certain.

<?php
define("INC", TEMPLATEPATH . "/functions");

require_once INC . "/wpzoom-functions.php";
require_once INC . "/wpzoom-core.php";
require_once INC . "/hidemenus.php";

/*Function to hide child category display if empty*/
function empty_dont_display_it($content) {
if (!empty($content)) {
$content = str_ireplace('<li>' .__( "No categories" ). '</li>', "", $content);
}
return $content;
}
add_filter('wp_list_categories','empty_dont_display_it');

//Function to check if a post is in a subcategory
function post_is_in_descendant_category( $cats, $_post = null )
{
foreach ( (array) $cats as $cat ) {
$descendants = get_term_children( (int) $cat, 'category');
if ( $descendants && in_category( $descendants, $_post ) )
return true;
} //end foreach
return false;
} //end function
?>
<?php
if ( !current_user_can( 'edit_users' ) ) {
add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
}
?>
<?php
add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');

function my_custom_dashboard_widgets() {
global $wp_meta_boxes;

wp_add_dashboard_widget('custom_help_widget', 'Widget', 'custom_dashboard_help');
}

function custom_dashboard_help() {
echo '<p>Custom dashboard widget</p>';
}
?>
<?php

function custom_colors() {
echo '<style type="text/css">#wphead{background:#41474d; color:#ffffff !important;}</style>';
}

add_action('admin_head', 'custom_colors');
?>
<?php
add_action( 'init', 'build_taxonomies', 0 );
function build_taxonomies() {
register_taxonomy( 'event_type', 'post', array( 'hierarchical' => true, 'label' => 'Event Type', 'query_var' => true, 'rewrite' => true ) );
}
require_once( TEMPLATEPATH . '/rewrite_rules_event_type.php');
?>


Thanks for your help.


wjm comments:

i think that's wrong.
you should move
<?php

add_action( 'init', 'build_taxonomies', 0 );

function build_taxonomies() {

register_taxonomy( 'event_type', 'post', array( 'hierarchical' => true, 'label' => 'Event Type', 'query_var' => true, 'rewrite' => true ) );

}

require_once( TEMPLATEPATH . '/rewrite_rules_event_type.php');
?>


to your functions.php

located in your theme directory
that is wp-content/themes/YOUR_THEME_FOLDER/


Viktor Nagornyy comments:

Hey,
I think you misunderstood me. That's exactly where it is. I copy and pasted my functions.php code above.

I was just simply asking about taxonomy.php, if the contents could break rewrite code.


wjm comments:

oh.. sorry, i misread that, i thought you pasted the contents of taxonomy.php

the first answer to your question is no, as the template loads after the rewrite rule is resolved.
can i have access to your server. it may be easier.
msg me with the wordpress and ftp info if you want to.

what happens when you enter
sample.com/conventions/

what is the url of your site?


Viktor Nagornyy comments:

Hmmmm thats interesting.
both URLs working, but the menu link keeps it as /event_type/convention
I'm using new nav_menu feature. Is there a way to make sure that /event_type/convention no longer works? It's going to be SEO problem being double content.

If you still need to look at it, I can provide you with my WP login to my test blog where I'm testing this, so you can use Editor to look at files.

Thanks.


wjm comments:

I found the best way to deal with this.
forget about rewrite_rules_event_type.php

all the code you need is:

add_action( 'init', 'build_taxonomies', 0 );
function build_taxonomies() {
register_taxonomy(
'event_type',
'post',
array(
'hierarchical' => true,
'label' => 'Event Type',
'query_var' => true,
'rewrite' => true,
)
);
global $wp_rewrite;
$wp_rewrite->extra_permastructs['event_type'] = array('%event_type%', EP_NONE);
}



there is a missing filter to alter the extra_permastructs, i am submitting a patch to wordpress trac.

i have fixed it in your blog.
you can check it.

- wjm


Viktor Nagornyy comments:

Sweetness! Like I said in the another comment above, I always find something ridiculous. ha!

Thanks for your help. I appreciate.


Viktor Nagornyy comments:

Sweetness! Like I said in the another comment above, I always find something ridiculous. ha!

Thanks for your help. I appreciate it.

2010-07-27

Pippin Williamson answers:

You could change your permalink settings to "/%postname%/" in Settings -> Pemalinks


Viktor Nagornyy comments:

That's what I have as my permalink structure. It still shows up. The sample.com/event_type/conventions points to an archive type page with a list of posts under that custom taxonomy. Not sure if that info helps.

2010-07-27

Jarret Minkler answers:

http://www.deanlee.cn/wordpress/permalinks-migration-plugin/

Use the permalink migrator to safely migrate

2010-07-28

Chris Olbekson answers:

To remove event_type from your permalink change your code to as follows:


<?php

add_action( 'init', 'build_taxonomies', 0 );

function build_taxonomies() {

register_taxonomy( 'event_type', 'post', array( 'hierarchical' => true, 'label' => 'Event Type', 'query_var' => true, 'rewrite' => array( 'slug' => 'event_type', 'with_front' => false ) );

} ?>


<strong>NOTE:</strong> You may possibly need to click save on the permalink page after making the necessary code change.


<strong>Edit: </strong>I apologize. I left off the end parentheses after the array. This is the correct code:

<?php

add_action( 'init', 'build_taxonomies', 0 );

function build_taxonomies() {

register_taxonomy( 'event_type', 'post', array( 'hierarchical' => true, 'label' => 'Event Type', 'query_var' => true, 'rewrite' => array( 'slug' => 'event_type', 'with_front' => false )) );

} ?>


Viktor Nagornyy comments:

Something about your code breaks the site, once I hit save it all goes to white screen and I have to ftp old version back in to fix it. So something's wrong.


Viktor Nagornyy comments:

Still nothing, I always seems to find something ridiculous ha.

2010-07-28

Rashad Aliyev answers:

Mr. Viktor Nagornyy,

I think you put this registering code to your default theme function. ( in functions.php ). So it changed your permalink structure. You should put it to your child theme functions. It'll register for your child theme and don't forget remove it from your default theme.

Then change your permalinks structure to default, then save and again change to what you prefer.

I think it'll help you.

best regards,


Viktor Nagornyy comments:

Hey,
I'm not quiet sure what you mean by child theme. If you could elaborate.

Thanks.