Support » Plugin: Simple URLs » Display the click counts in frontend

Viewing 3 replies - 1 through 3 (of 3 total)
  • Just in case someone is interested: I integrated a shortcode to handle the frontend display:

    function surl_link_shortcode($atts) {
    	extract(shortcode_atts(array(
    	'id'  => '',		# ID of the url
    	'title' => '', 		# Title of the url
    	'clicks' => 0,		# display the counter
    	'link' => 1,		# do we need a link?
    	'linktext' => '',	# Which text should be displayed?
    	'target' => '',		# Which text should be displayed?
    	), $atts));
    
    	# default values
    	$url_id = ($id) ? intval($id)  : 0;
    	$url_title = ($title) ? $title : '';
    	$url_counter = ($clicks) ? true : false;
    	$url_link = ($link) ? true : false;
    	$url_linktext = ($linktext) ? $linktext : '';
    	$url_target = (in_array($target, array('_blank','_self','_top'))) ? $url_target : '_self';
    	$url_href = '';
    	$url_count_value = 0;
    
    	if(($url_id == 0) && ($url_title != '')) {
    	    # no id found, but there is a title
    	    $linkdata = get_page_by_title($url_title,OBJECT,'surl');
    	    $url_id = $linkdata->ID;
    	}
    
    	$url_meta = get_post_meta( $url_id ); // array of metadata
    	$url_href = get_permalink( $url_id ); // the permalink
    
    	if(isset($url_meta["_surl_count"][0])){
    		# is there a meta found?
    		$url_count_value = intval($url_meta["_surl_count"][0]);
    	}
    
    	# initialize the shortcode output
    	 $theShortcode = '';
    
    	# Build the a tag if needed
    	if($url_link && $url_href != ''){
    		$theShortcode .= '<a href="'.$url_href.'"';
    		$theShortcode .= ' target="'.$url_target.'"';
    		$theShortcode .= '>';
    	}
    
    	# output either linktext or permalink
    	if($url_linktext != ''){
    		$theShortcode .= $url_linktext;
    	} else {
    		$theShortcode .= $url_href;
    	}
    
    	# Close the a tag if needed
    	if($url_link && $url_href != ''){
    		$theShortcode .= '</a>';
    	}
    
    	# ouput the counter, if needed
    	if($url_counter){
    		$theShortcode .= ' '.sprintf(_n('(%s click so far)','(%s clicks so far)',$url_count_value),$url_count_value);
    	}
    
    	return $theShortcode;
    }

    Just add it to the __construct():

    add_shortcode('surl_link', array(&$this, 'surl_link_shortcode'));

    After that you can use a shortcode like this:

    [surl_link id=” title=” clicks=1 link=1 linktext=’Your linktext here’ target=’_self’]

    Hope that helps someone 😉

    Hi mate, I’m developing a site witch is somewhat where I’m supposed to use this snippet but seems that is not working.

    Hope that you will notice and replay in time.

    Could you please post the shortcode you used?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display the click counts in frontend’ is closed to new replies.