[wp-hackers] Page Redirect from Shortcode

Christopher Ross cross at thisismyurl.com
Thu Oct 6 13:17:42 UTC 2011


Claude, I have a code snippet at http://thisismyurl.com/blog/2011/01/19/redirecting-a-wordpress-post-with-custom-fields/ that will do it, although looking at it ... i should really update that so it's better.

Here's a better function for your functions.php file that will fire before WordPress generates the page.

> function thisismyurl_redirect() {
> 	
> 	if ( is_single() ) {
> 		global $post;
> 	
> 		$post_id = $post->ID;
> 		if ( !empty( $post_id ) ) $redirect = get_post_meta ( $post_id, 'redirect', true);
> 		
> 		if ( ! empty( $redirect ) ) {
> 			wp_redirect( $redirect, 301 );
> 			exit();
> 		}
> 	}
> 
> }
> add_action('template_redirect','thisismyurl_redirect');


Now, add a custom field to your Post named "redirect" and if it's present, it'll cause a redirect.

Chris


On 2011-10-06, at 9:56 AM, Claude Needham wrote:

> On Wed, Oct 5, 2011 at 10:44 PM, Dion Hulse (dd32) <wordpress at dd32.id.au> wrote:
>> Shortcodes are parsed/called at display time, by the time the shortcode
>> function is called, the header and some posts may/will have printed.
>> 
>> Your only real option is to hook in early on template_redirect (or similar)
>> and scan through $wp_query->posts manually looking for the shortcode, and
>> handling it there..
>> 
>> A shortcode might not be the best option for what you're trying to achieve?
>> what are the reasons you've gone with a shortcode?
>> 
>> On 6 October 2011 16:41, Claude Needham <gxxaxx at gmail.com> wrote:
> 
> I'm afraid the reason for using shortcode was not the best reason but
> it was a compelling reason.
> The owner of the website is technically challenged. They are
> comfortable with shortcodes and little else.
> I was attempting to use a mechanism they were familiar with.
> 
> But, it looks like the shortcode mechanism just plain will not work.
> 
> In light of this, what mechanism would be best for this situation?
> Post meta? Custom field?
> 
> Thanks, this is a new area for me.
> 
> Claude
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers




--
Christopher Ross
christopher.ross at rodonic.com

http://rodonic.com

Toll Free (855) RODONIC
Toronto (416) 900-3731
San Diego (858) 201-4912

We're more than just great sites.



More information about the wp-hackers mailing list