Last active
March 15, 2016 20:53
-
-
Save khlbrg/8333973 to your computer and use it in GitHub Desktop.
Specify custom post types by code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function my_custom_tpa() { | |
$labels = array( | |
'name' => _x( 'TPA', 'tpa' ), | |
'singular_name' => _x( 'TPA', 'tpa' ), | |
'add_new' => _x( 'Add New', 'book' ), | |
'add_new_item' => __( 'Add New TPA' ), | |
'edit_item' => __( 'Edit TPA' ), | |
'new_item' => __( 'New TPA' ), | |
'all_items' => __( 'All TPA' ), | |
'view_item' => __( 'View TPA' ), | |
'search_items' => __( 'Search TPA' ), | |
'not_found' => __( 'No TPA found' ), | |
'not_found_in_trash' => __( 'No TPA found in the Trash' ), | |
'parent_item_colon' => '', | |
'menu_name' => 'TPA' | |
); | |
$args = array( | |
'labels' => $labels, | |
'description' => 'Holds our TPA and product specific data', | |
'public' => true, | |
'menu_position' => 8, | |
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ), | |
'has_archive' => true, | |
'taxonomies' => 'tpac', | |
'publicly_queryable' => true, | |
'query_var' => 'tpa', | |
'rewrite' => false | |
); | |
register_post_type( 'tpa', $args ); | |
global $wp_rewrite; | |
$wp_rewrite->add_rewrite_tag('%tpa%', '([^/]+)', 'tpa='); | |
//the root of the post type, ie mysite.com/movie-reviews/ will be the landing page for the post type | |
$permalink_prefix = 'tpa'; | |
//the permalink structure for the post type that will be appended to the prefix, mysite.com/movie-reviews/2010/11/25/test-movie-review/ | |
$permalink_structure = '/%tpa%/'; | |
//we use the WP_Rewrite class to generate all the endpoints WordPress can handle by default. | |
$rewrite_rules = $wp_rewrite->generate_rewrite_rules($permalink_prefix.'/'.$permalink_structure, EP_ALL, true, true, true, true, true); | |
$rewrite_rules = array_merge($wp_rewrite->generate_rewrite_rules($permalink_prefix), $rewrite_rules); | |
$rewrite_rules[$permalink_prefix.'/?$'] = 'index.php?paged=1'; | |
foreach($rewrite_rules as $regex => $redirect) { | |
if(strpos($redirect, 'attachment=') === false) { | |
//add the post_type to the rewrite rule | |
$redirect .= '&post_type=tpa'; | |
} | |
//turn all of the $1, $2,... variables in the matching regex into $matches[] form | |
if(0 < preg_match_all('@\$([0-9])@', $redirect, $matches)) { | |
for($i = 0; $i < count($matches[0]); $i++) { | |
$redirect = str_replace($matches[0][$i], '$matches['.$matches[1][$i].']', $redirect); | |
} | |
} | |
//add the rewrite rule to wp_rewrite | |
$wp_rewrite->add_rule($regex, $redirect, 'top'); | |
} | |
add_filter('post_type_link', 'filter_movie_review_link', 10, 2); | |
function filter_movie_review_link($permalink, $post) { | |
if(('tpa' == $post->post_type) && '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) { | |
$rewritecode = array( | |
'%tpa%' | |
); | |
$unixtime = strtotime($post->post_date); | |
$date = explode(" ",date('Y m d H i s', $unixtime)); | |
$rewritereplace = array( | |
$post->post_name, | |
); | |
$permalink = str_replace($rewritecode, $rewritereplace, '/tpa/%tpa%/'); | |
$permalink = user_trailingslashit(home_url($permalink)); | |
} | |
return $permalink; | |
} | |
} | |
add_action( 'init', 'my_custom_tpa' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment