Skip to content

Instantly share code, notes, and snippets.

@ryu-compin
Last active December 12, 2018 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryu-compin/f2eea606f6a3db7fe9681cf7695e3c08 to your computer and use it in GitHub Desktop.
Save ryu-compin/f2eea606f6a3db7fe9681cf7695e3c08 to your computer and use it in GitHub Desktop.
PWA4WP install button sample - html/css/js
// add this code to functions.php
function enque_pwa4wp_installaction(){
wp_enqueue_script('pwa4wp_pwa_installer',get_stylesheet_directory_uri() . '/js/pwainstall.js','','1.0',true);
}
add_action( 'wp_enqueue_scripts', 'enque_pwa4wp_installaction' );
#pwainstall {
display: none;
}
<div id="pwainstall">
<button id="pwainstall_button" class="square_btn">Add to Home Screen</button>
</div>
var pwa4wp_open_install = function(){
console.log("install button show");
jQuery('#pwainstall').css('display','block');
};
document.getElementById("pwainstall_button").addEventListener("click",function(){
if(window.pwa4wp_installevent !== undefined ){
window.pwa4wp_installevent.prompt();
window.pwa4wp_installevent.userChoice.then(function(choiceResult){
if(choiceResult === 'accepted'){
// insert actions for installation accepted here
console.log('pwa installation accepted.')
}else{
// insert actions for installation refused here
console.log('pwa installation refused.')
}
});
window.pwa4wp_installevent = null;
}else{
console.log("install event is undefined");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment