• Resolved thepoorswiss

    (@thepoorswiss)


    Hi,

    Thanks for this great plugin!

    I am trying to track how many clicks I get on my sharing buttons to see if it is worth keeping it for my blog.

    Looking at the source code, it seems to be possible with a filter and adding something to each button ‘data’, but it does not seem to work.

    Here is what I have done:

    function tps_scriptlesssocialsharing_buttons($buttons) {
    	$buttons['twitter']['data'] = ' onclick="handleSocialClick(this);" ';
    	$buttons['facebook']['data'] = ' onclick="handleSocialClick(this);" ';
    	$buttons['pinterest']['data'] = ' onclick="handleSocialClick(this);" ';
    	$buttons['linkedin']['data'] = ' onclick="handleSocialClick(this);" ';
    	return $buttons;
    }
    add_filter( 'scriptlesssocialsharing_buttons', 'tps_scriptlesssocialsharing_buttons' );

    How can I achieve this?

    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Robin Cornett

    (@littlerchicken)

    Because the button output is all passed through the wp_kses filter, you need to add onclick to the array of attributes allowed on a link. Your code should work if you also add this filter:

    
    add_filter( 'wp_kses_allowed_html', 'prefix_filter_allowed_html', 10, 2 );
    /**
    	 * Add "onclick" to allowed KSES output.
    	 *
    	 * @param $allowed
    	 * @param $context
    	 * @return mixed
    	 */
    function prefix_filter_allowed_html( $allowed, $context ) {
    	if ( 'post' === $context ) {
    		$allowed['a']['onclick'] = true;
    	}
    
    	return $allowed;
    }
    

    I’ve checked this locally and the onclick attribute is added to the link, although I can’t test it beyond that. You can add this to the same file where you are adding the scriptless filter–just please practice safe coding and make sure your files are backed up. Hope that helps.

    Thread Starter thepoorswiss

    (@thepoorswiss)

    Thanks Robin!

    You’re awesome!

    It works like a charm now

    Thank you very much!

    Where are the results for this function shown?

    Plugin Author Robin Cornett

    (@littlerchicken)

    The results of this function will show on the Scriptless links which have the onclick attribute added. Without the wp_kses_allowed_html filter applied to allow it, that would be stripped from the output, even with the original filter applied.

    Hi Robin. Does this do the same as this one? Coz what I am looking for is a way to track the clicks and view them somewhere (and I don’t understand how to with this method).

    Plugin Author Robin Cornett

    (@littlerchicken)

    Hi @callaloo, I have not looked into the JavaScript code at all, so I’m not sure what’s involved there–if you want to add a script to the plugin behavior, the first answer will help you add a listener to the buttons. The link you share may be a good starting point for the script side.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Add onclick to the links’ is closed to new replies.