How To Consistently Enforce Rewrite Rule in Plugin Development

Here’s a solution. See comments at the end… // I am a functions.php file of a plugin called “sample” — implement me slightly differently for a theme. // Note this code requires WP 3.0 or greater. class SAMPLE { public static function activatePlugin() { self::rewriteURL(); flush_rewrite_rules(); if (get_option(‘permalink_structure’) == ”) { self::updatePermalinks(); } // add … Read more

Custom Post Types, URL rewrite on multiple CPTs

Assuming the code snippet provided works then extending the conditional statement like so will help you, function filter_post_type_link($link, $post) { if ($post->post_type=”custom_post_type_1″) { if ($cats = get_the_terms($post->ID, ‘custom_cat_1’)) $link = str_replace(‘%custom_cat_1%’, array_pop($cats)->slug, $link); return $link; } elseif ($post->post_type=”custom_post_type_2″) { if ($cats = get_the_terms($post->ID, ‘custom_cat_2’)) $link = str_replace(‘%custom_cat_2%’, array_pop($cats)->slug, $link); return $link; } else { return … Read more

Rewrite rule for a query string

Try this code: function geotags_add_rewrite_rules($wp_rewrite_rules) { global $wp_rewrite; $rule_key = ‘%cityid%’; $url_pattern = ‘([^/]+)’; $query_string = ‘cityid=’; $wp_rewrite->add_rewrite_tag($rule_key, $url_pattern, $query_string); $url_structure = $wp_rewrite->root . “cityid/$rule_key/”; $rewrite_rules = $wp_rewrite->generate_rewrite_rules($url_structure); $wp_rewrite_rules = $rewrite_rules + $wp_rewrite_rules; return $wp_rewrite_rules; } add_filter(‘rewrite_rules_array’, ‘geotags_add_rewrite_rules’); function geotags_add_query_var($query_vars) { array_push($query_vars, ‘cityid’); return $query_vars; } add_filter(‘query_vars’,’geotags_add_query_var’); NOTE: Remember to save permalinks so your new … Read more

wp_list_categories() – current-cat class also inside posts?

Yes off course. You just need to get the term id and put it on the args. Check wp_list_categories() function wr_list_taxonomy($taxonomy, $orderby, $hierarchical, $cat_id) { $show_count = 0; $pad_counts = 0; $title=””; $cat_id = 0; $args = array( ‘taxonomy’ => $taxonomy, ‘orderby’ => $orderby, ‘show_count’ => $show_count, ‘pad_counts’ => $pad_counts, ‘hierarchical’ => $hierarchical, ‘title_li’ => … Read more

Need to add rewrite rule that adds in additional information about the post to url

Sorry I took so long to write this up. Hope it’s still useful. This is based off of a tutorial found here and some general playing around. <?php //This function sets up the permastructure add_action(‘init’, ‘setup_permastruct’, 40); function setup_permastruct() { //Setup query vars add_rewrite_tag(‘%state%’,'([^/]+)’); add_rewrite_tag(‘%suburb%’,'([^/]+)’); //Permastruct for custom post type add_permastruct(‘venue’, ‘venue/%state%/%suburb%/%postname%’, false); } //This … Read more

Rewrite post type slug only for child theme

Make the slug translatable when you register the custom post type: register_post_type( ‘post_type_name’, array ( ‘rewrite’ => array ( ‘slug’ => _x( ‘post_type_name’, ‘URL slug’, ‘your_text_domain’ ) ) ) ); Then create a small plugin for the site where you want to change the slug: add_filter( ‘gettext_with_context’, ‘change_my_slug’, 10, 4 ); function change_my_slug( $translation, $text, … Read more

How to redirect to correct pages after permalink structure change

WordPress should handle all redirections correctly, without you needing to worry about them. For example, if your old post was http://example.com/2012/10/11/i-like-stackexchange any links should auto-magically be redirected to http://example.com/favorite-things/i-like-stackexchange. I just tested it on a site with WordPress 3.4 installed, and it worked just fine. WordPress can read your mind.