I am trying a link management plugin, I've got my redirect working but I am trying to have another metabox as a click counter but I can't seem to make it count, here my function and metabox code.
/*Metabox*/
<?php
add_filter( 'rwmb_meta_boxes', 'amp_link_register_meta_boxes' );
function amp_link_register_meta_boxes( $meta_boxes ) {
$meta_boxes[] = array (
'id' => 'link-box',
'title' => 'link box',
'pages' => array (
'link',
),
'context' => 'normal',
'priority' => 'high',
'autosave' => true,
'fields' => array (
array (
'id' => 'link_url',
'type' => 'url',
'name' => 'Link Url',
'placeholder' => 'Enter destination url',
),
array (
'id' => 'link_counter',
'type' => 'text',
'name' => 'Counter',
),
),
);
return $meta_boxes;
}
/* redirect and count function */
function links_amp_redirect() {
if( is_singular( 'link' ) ) {
$count = rwmb_meta('link_counter', get_the_ID() );
$count++;
update_post_meta( 'link_counter', $count, get_the_ID() );
wp_redirect( rwmb_meta( 'link_url', get_the_ID() ) );
exit();
}
}
add_action( 'template_redirect', 'links_amp_redirect' );