javascript, jQuery

SquareSpace Scroll + YouTube PopUP

<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
<script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>

<script type="text/javascript">
	$(document).ready(function() { 
    
    $('a[href*="https://www.youtube.com/watch?"]').each(function() {
  $(this).attr('data-fancybox','');
 });
      
   $('a[href*="https://youtu.be/"]').each(function() {
  $(this).attr('data-fancybox','');
 });    
	
	
	});
</script>

<script>
$(document).ready(function(){
  // Add smooth scrolling to all links
  $("a").on('click', function(event) {

    // Make sure this.hash has a value before overriding default behavior
    if (this.hash !== "") {
      // Prevent default anchor click behavior
      event.preventDefault();

      // Store hash
      var hash = this.hash;

      // Using jQuery's animate() method to add smooth page scroll
      // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
      $('html, body').animate({
        scrollTop: $(hash).offset().top
      }, 800, function(){
   
        // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = hash;
      });
    } // End if
  });
});
</script>

Aggiungendo il codice in tutte le pagine non ci sono problemi

il clic si attiva su ID dell’elemento sia un <p> o un <div>

 

javascript, jQuery, PHP, WordPress

Woocommerce Checkout – Configurazione campi

	function newFieldCheck(){
		
		//controlliamo di essere nella pagina giusta

		if (is_checkout()) {
	
		?>

			<script type="text/javascript">
				
			jQuery( document ).ready(function( $ ) {  
				
			// nascondo i campi di fatturazione
			
			$('#ragione_sociale_field').hide();
			$('#partita_iva_field').hide();
			$('#codice_destinatario_field').hide();
			$('#pec_field').hide();

				
			 // imposto un trigger sul campo di richiesta di fatturazione
			 	
			  $('#fattura_ricevuta').on('change', function (e) {
				  
// 				    var optionSelected = $("option:selected", this);
				    var valueSelected = this.value;
				    
				    if (valueSelected == 'SI'){
					    
					    $('.optional').hide();  //rimuovo la scritta opzionale sui campi di fatturazione
					    
					    	$('#ragione_sociale_field').show();
							$('#partita_iva_field').show();
							$('#codice_destinatario_field').show();
							$('#pec_field').show();		    			 
				    } 
				     
				     if (valueSelected == 'NO') {
					    
					    	$('#ragione_sociale_field').hide();
							$('#partita_iva_field').hide();
							$('#codice_destinatario_field').hide();
							$('#pec_field').hide();		    		    
				    }

				  
			  });				
				
			});  //end ready


			</script>
	
	
		<?php
			
		}
		
		
	} //end cpCheck



add_action('woocommerce_before_checkout_form', 'newFieldCheck');

 

 

 function validazioneFattura() {

		    $fattura = filter_input(INPUT_POST, 'fattura_ricevuta');
		    $ragione_sociale = filter_input(INPUT_POST, 'ragione_sociale');
   		    $piva = filter_input(INPUT_POST, 'partita_iva');
   		    $sdi = filter_input(INPUT_POST, 'codice_destinatario');
   		    $pec = filter_input(INPUT_POST, 'pec');
   		    $codice_fiscale = filter_input(INPUT_POST, 'codice_fiscale');

   			
   			if (strlen($codice_fiscale) != 16) {
		        wc_add_notice(__('<strong>Codice Fiscale:</strong> Errore, controlla i dati inseriti.'), 'error');
		    }

		
		    if ($fattura == 'SI' AND $ragione_sociale == '') {
		        wc_add_notice(__('<strong>Errore</strong>, devi inserire una Ragione Sociale'), 'error');
		    }
		    
		    // check partita iva  30/04/2019 - 16:30
		    
			if (($fattura == 'SI' AND strlen($piva) != 11) or ($fattura == 'Fattura' AND $piva == ''))  {
		        wc_add_notice(__('<strong>Partita IVA</strong>, Errore, controlla i dati inseriti'), 'error');
		    }
		    
		    // controllo fatturazione elettronica 24/02/2019 - 13:32
		    
		    if ($fattura == 'SI' AND $pec == '' AND $sdi == ''){
		        wc_add_notice(__('<strong>Errore</strong>, devi inserire un Codice Destinatario o un indirizzo PEC'), 'error');
		    }
			   
		    
		}
		
		add_action('woocommerce_checkout_process', 'validazioneFattura');

 

jQuery, WordPress

Ordinamento output plugin in post

Ecco uno snippet davvero utile per ordinare l’output dei plugin in un post di WordPress.
Dopo svariate ricerche, l’unico modo davvero valido rimane quello di modificare il DOM così

<script type="text/javascript">
  jQuery(document).ready(function($) { 
    
      $('#wpdevar_comment_1').insertAfter('.entry-author');
      $('.et_bloom_below_post').insertBefore('.entry-meta text-center');

   });
</script>

grazie alle direttive insertAfter() e insertBefore() di jQuery (grazie <3 jQuery), questa impostazione richiede 5 minuti.

javascript, jQuery

Smooth Scroll

<script type="text/javascript">
 jQuery(document).ready(function($) { 
 
 //EFFETTO MORBIDOSO
 
 $(function() {
 $('a[href*="#"]:not([href="#"])').click(function() {
 if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
 var target = $(this.hash);
 target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
 if (target.length) {
 $('html, body').animate({
 scrollTop: target.offset().top-100
 }, 1000);
 return false;
 }
}
 });
});
 
 });
</script>
javascript, jQuery

url in javascript

http://www.test.com:8082/index.php#tab2?foo=123

window.location.host                   www.test.com:8082
window.location.hostname               www.test.com
window.location.port                   8082
window.location.protocol               http
window.location.pathname               index.php
window.location.href                   http://www.test.com:8082/index.php#tab2
window.location.hash                   #tab2
window.location.search                 ?foo=123

jQuery

$(location).attr('host');                        www.test.com:8082
$(location).attr('hostname');                    www.test.com
$(location).attr('port');                        8082
$(location).attr('protocol');                    http
$(location).attr('pathname');                    index.php
$(location).attr('href');                        http://www.test.com:8082/index.php#tab2
$(location).attr('hash');                       #tab2
$(location).attr('search');                     ?foo=123

 

jQuery

jQuery :: ajax submit

 

jQuery(document).ready(function($) { 
	
		$("#idForm").submit(function() {


var url = "http://www.dominio.com/script.php"; // the script where you handle the form input.


    $.ajax({
           method: "POST",
           url: url,
           data: $("#idForm").serialize(), // serializza tutti gli elementi.
            beforeSend: function() {
		      console.log('invio dei dati in corso...');
		   }
         })
           .done(function(data)
           {
               console.log(data); // show response from the php script.
             //  inviaForm();
			 inviaForm();
           }) 
           
           .fail(function(xhr) { // if error occured
		        console.log("Error occured.please try again");
		        console.log(xhr.statusText + xhr.responseText);
    	   });

     return false; // avoid to execute the actual submit of the form.

		});

	});

			function inviaForm(){
					$("#idForm").submit();
			}


Tutti i parametri vengono passati in POST, basterà dunque che il controller PHP che riceve i dati utilizzi il classico $_POST[‘reference’];