#The Problem
I need to create custom links based on the current page URL within WordPress. The system needs to work with both pretty and default permalinks. I have a custom rewrite endpoint defined that needs to be added.
So if the URL of the page is http://site.url/?p=25
I need to create http://site.url/?p=25&custom=1
If the URL is http://site.url/2012/post-slug
I need to create http://site.url/2012/post-slug/custom
I can't find a built-in function to do this, so I'm thinking I'll need to build my own to do this. Some pseudo-code:
function create_link() {
$current = get_current_page_url(); // Not a real fn, just get the current url
$structure = get_option( 'permalink_structure' );
if ( ! empty( $structure ) ) {
$new = add_query_arg( 'custom', 1, $current );
} else {
$new = trailingslashit( $current ) . 'custom';
}
return $new;
}
Does this logic make sense? Is there an easier/better way to do it? What's the best way to get the current page URL?
#The Solution
OK, after some feedback on Twitter, here's the function I ended up with:
public function get_custom_link() {
$permalink = get_permalink();
$structure = get_option( 'permalink_structure' );
if ( empty( $structure ) ) {
$new = add_query_arg( 'custom', 1, $permalink );
} else {
$new = trailingslashit( $permalink ) . 'custom/1';
}
return $new;
}
Is there a reason
get_permalink()
wouldn't work?