WordPress.org

Ready to get started?Download WordPress

Forums

Simple URLs
[resolved] Display the click counts in frontend (4 posts)

  1. sixtyseven
    Member
    Posted 6 months ago #

    Hello from Germany,

    I absolutely love the simplicity of this plugin. Although it would be very nice to have an option to display the clicks in the frontend. Could anybody give me a little guidance with that?

    Thank you.

    https://wordpress.org/plugins/simple-urls/

  2. sixtyseven
    Member
    Posted 6 months ago #

    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 ;-)

  3. spirkov
    Member
    Posted 4 months ago #

    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.

  4. sixtyseven
    Member
    Posted 4 months ago #

    Could you please post the shortcode you used?

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.