A print button for Scriptless Social Sharing has been requested more than once, but since it requires JavaScript to implement, it will sadly not become part of the core plugin. However, it’s possible to add a print button yourself, with the judicious use of two filters, as of version 3.2.0.
This version introduces a new button generator class. Using a filter, you can register a button with one function and it will automatically be added to the settings screen and managed just like any other button on the site.
Note: for most buttons, it takes just one function. However, since the print button requires JavaScript, you’ll need a second one. Here’s the first one, which handles registering the button itself:
add_filter( 'scriptlesssocialsharing_register', 'prefix_scriptless_add_print_button' );
/**
* Adds a custom sharing button to Scriptless Social Sharing.
*
* @return void
*/
function prefix_scriptless_add_print_button( $buttons ) {
$buttons['print'] = array(
'label' => __( 'Print', 'scriptless-social-sharing' ),
'url_base' => esc_js( 'javascript:window.print();' ),
'args' => array( // extra parameters
'color' => '#0c5d19',
'target_self' => true,
),
);
return $buttons;
}
Code language: PHP (php)
Once the button is registered, it won’t work quite yet, because of the javascript
in the url_base
. That’s not allowed, because it’s JavaScript. However, if you tell WordPress that you do want to allow javascript
in your links, then the print button will work!
add_filter( 'kses_allowed_protocols', 'prefix_add_protocol' );
/**
* Allow Javascript to be used as a link protocol.
*
* @param array $protocols
* @return array
*/
function prefix_add_protocol( $protocols ) {
$protocols[] = 'javascript';
return $protocols;
}
Code language: PHP (php)
Return to Scriptless Social Sharing