Click counter

Support General Click counter

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #7031
    james@fosker.co.za[email protected]
    Participant

    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' );
    #7034
    Anh TranAnh Tran
    Keymaster

    I think you write the update_post_meta incorrectly, it should be:

    update_post_meta( get_the_ID(), 'link_counter', $count );

    #7059
    james@fosker.co.za[email protected]
    Participant

    That works thanks 🙂

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Click counter’ is closed to new replies.