Last active
December 12, 2018 14:37
-
-
Save ryu-compin/f2eea606f6a3db7fe9681cf7695e3c08 to your computer and use it in GitHub Desktop.
PWA4WP install button sample - html/css/js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pwainstall { | |
display: none; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="pwainstall"> | |
<button id="pwainstall_button" class="square_btn">Add to Home Screen</button> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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