Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Resume Manager - Add meta data to the permalinks
<?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
You can’t perform that action at this time.