Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Integrating custom field w/ url cloaker WordPress

  • SOLVED

Hello all,
I'm in the final stages of wrapping up a big theme project for a client, and one of the things they've asked me to integrated is a link cloaker. I found the "Affiliate hide" plugin but I've been tweaking it so it pulls from a specific custom field.

The main function of the plugin is as follows:

function affiliate_hide() {
global $wp_query;

if( isset( $wp_query->query_vars['go'] )) {

$target = get_post_meta($wp_query->query_vars['go'], 'url', true);
if($target == '') {
wp_redirect(get_option('siteurl')); exit; // if no blog post redirects to index page.
}
wp_redirect($target, 301);
exit;
}
}


The 'url' is referring to the custom field in which the link to be cloaked is found. My problem is that I followed this [URL="http://www.farinspace.com/how-to-create-custom-wordpress-meta-box/"]add meta box[/URL] tutorial to utilize custom fields in a much nicer interface for my clients clients. This particular tutorials stores everything in an array, so I typically access values like so:

$my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
echo $my_meta['link'];


my problem is being able to grab the 'link' in place of the 'url' in the plugin function. I've tried a wide variety of things but just can't seem to figure it out.

Any help would be appreciated.
Thank you!

Answers (1)

2012-02-04

John Cotton answers:

Will this work?

function affiliate_hide() {
global $wp_query;

if( isset( $wp_query->query_vars['go'] )) {
if( $target = get_post_meta( $wp_query->query_vars['go'], '_my_meta', true ) ) {
wp_redirect( $target['url'], 301 );

} else {
wp_redirect(get_option('siteurl')); // if no blog post redirects to index page.
}

exit();
}
}


Andrew Clemente comments:

Unfortunately, it does not.

I've tried various ways of integrating _my_meta[link], but none seem to be successful


John Cotton comments:

Well, it would be a start to see how your actually storing things - do you know?

If not, put this somewhere you can see the output:

print_r(get_post_custom($post_id));

At least then you'll know where you data is and can adjust the $target appropriately...


Andrew Clemente comments:

Here's the output:

Array ( [_my_meta] => Array ( [0] => a:2:{s:4:"link";s:13:"reviewing.net";s:6:"rating";s:1:"1";} ) [_edit_last] => Array ( [0] => 1 ) [_edit_lock] => Array ( [0] => 1328379321:1 ) [_thumbnail_id] => Array ( [0] => 727 ) )

thanks!


John Cotton comments:

Have you tried

wp_redirect( $target['link'], 301 );

in my original code?


Andrew Clemente comments:

It redirects to http://www.domain.com/recommend/Array, so it appears it's still unable to extract the link bit from the array


John Cotton comments:

The array is serialized in your initial output.

What do you get with

$target = get_post_meta( $wp_query->query_vars['go'], '_my_meta', true );
print_r($target);



Andrew Clemente comments:

I ended up changing the value of the "jump link" and it worked. Seemed like all it needed for you original snippet to work was updating the option.

Thanks for much!