Skip to content

Quickly and dirty add a JQuery script to the WordPress footer

A MacBook with lines of code on its screen on a busy desk

I often search for such a script so now I got this ready made and perfectly at hand if I need it in the future. If you like you can use it too, I jzst need to write some text here so that this post does not look too empty. Thanks.

add_filter( 'wpex_get_social_items', function ( $items ) {
	$items['print'] = array(
		'site'       => 'Print',
		'label'      => 'Print',
		'li_class'   => 'custom-print',
		'icon_class' => 'ticon ticon-print',
		'href'       => '#print',
	);
	return $items;
} );

add_action( 'wp_enqueue_scripts', function() {

	$script = '
	jQuery( document ).ready(function() {
		jQuery( ".wpex-social-share li.custom-print a" ).click( function() {
			window.print();
		} );
	} );';

	wp_add_inline_script( 'jquery', $script );

} );

Another example:

//TOGGLE CART AFTER ADD TO CART CLICK
add_action( 'wp_footer', 'trigger_for_ajax_add_to_cart' );
function trigger_for_ajax_add_to_cart() {
    /*
	if ( isset($_POST['add-to-cart']) && $_POST['add-to-cart'] > 0
    && isset($_POST['quantity']) && $_POST['quantity'] > 0 ) :
    ?>
    <script type="text/javascript">
        (function($){
            $( ".topbar-cart-link" ).trigger( "click" );
			//    setTimeout(function(){            $( ".open-side-cart" ).trigger( "click" );         }, 500);
            // Display an alert for testing purpose (To be removed)
            alert('".open-side-cart" Click!');

        })(jQuery);
    </script>
    <?php
    endif;
	*/
}

Dieser Beitrag hat 0 Kommentare

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

An den Anfang scrollen