PHP Snippet 1:
function get_user_specific_url() {
if ( is_user_logged_in() ) {
return sprintf(
'%s/custom-post-type/%s',
get_site_url(),
wp_get_current_user()->user_nicename
);
}
// Redirect home in case if not logged in
return get_site_url();
}
PHP Snippet 2:
add_action( 'init', function() {
add_rewrite_tag("%user_nicename%", '([^/]*)');
add_rewrite_rule(
'^custom-post-type/([^/]*)/?',
'index.php?page_id=123&user_nicename=$matches[1]',
'top'
);
} );
PHP Snippet 3:
add_shortcode( 'replace_link_w_user', 'rl_w_user' );
function rl_w_user($atts = array()) {
$details = shortcode_atts( array(
"link"=>'',
"button_text"=>'Search'
), $atts );
if ( is_user_logged_in() ) {
$button='
<form action="'.$details['link'].'/'.wp_get_current_user()->user_nicename.'">
<input type="submit" value="'.$details['button_text'].'" />
</form>';
return $button;
} else {
return;
}
}