Created
October 17, 2014 16:23
-
-
Save mikejolley/34c56160d9ac8c607144 to your computer and use it in GitHub Desktop.
Resume Manager - Add meta data to the permalinks
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
<?php | |
/** | |
* Add rewrite tags for the data we want to insert and define the structure of the permalink. | |
* | |
* You need to go to Settings > Permalinks and save for these rules to be added. | |
*/ | |
function add_resume_rewrite_rules(){ | |
add_rewrite_tag( '%resume%', '([^/]+)', 'resume=' ); | |
add_rewrite_tag( '%candidate_title%', '([^/]+)', 'candidate_title=' ); | |
add_permastruct( 'resume', 'resumes/%candidate_title%/%resume%', true ); | |
} | |
add_action( 'init', 'add_resume_rewrite_rules', 11 ); | |
/** | |
* Add data to the permalinks | |
* @param string $permalink | |
* @param WP_Post $post | |
* @param bool | |
* @return string | |
*/ | |
function resume_permalinks( $permalink, $post, $leavename ) { | |
// Only affect resumes | |
if ( $post->post_type !== 'resume' || empty( $permalink ) || in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) { | |
return $permalink; | |
} | |
// Get the meta data to add to the permalink | |
$title = get_post_meta( $post->ID, '_candidate_title', true ); | |
// We need data, so fallback to this if its empty | |
if ( empty( $title ) ) { | |
$title = 'candidate'; | |
} | |
// Do the replacement | |
$permalink = str_replace( '%candidate_title%', sanitize_title( $title ), $permalink ); | |
return $permalink; | |
} | |
add_filter( 'post_type_link', 'resume_permalinks', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment