php - Trying to publish a little bit of Javascript on the homepage AND at the bottom
<script type="text/javascript">
function toggleDiv() {
var triggeredDiv = document.querySelector('.triggeredDiv');
if (document.getElementById('myonoffswitch').checked) {
triggeredDiv.classList.remove('shown');
} else {
triggeredDiv.classList.add('shown');
}
}
document.getElementById('myonoffswitch').addEventListener("change", toggleDiv);
</script>
I need to print this little snippet of Javascript at the bottom of the homepage...
I know that I can do that using a functions.php
command like this:
function for_homepage() {
return 'JavaScript HERE';
}
add_shortcode('for_homepage', 'for_homepage');
And then my shortcode would be [for_homepage]
but I don't believe that this is the best approach - I think it is better to enque the script?
Also - should I print it directly on the page (at the bottom where it needs to be for this to work) for speed reasons?
Thanks!
BTW this is to make this switcher work: /
++++++
UPDATE
I believe that this might be the better approach:
function add_inline_script() {
echo "<script>/* do awesome things */</script>\n";
}
add_action( 'wp_head', 'add_inline_script', 0 );
However - I would change the wp_head
to the correct footer function...
Is this the best approach?
<script type="text/javascript">
function toggleDiv() {
var triggeredDiv = document.querySelector('.triggeredDiv');
if (document.getElementById('myonoffswitch').checked) {
triggeredDiv.classList.remove('shown');
} else {
triggeredDiv.classList.add('shown');
}
}
document.getElementById('myonoffswitch').addEventListener("change", toggleDiv);
</script>
I need to print this little snippet of Javascript at the bottom of the homepage...
I know that I can do that using a functions.php
command like this:
function for_homepage() {
return 'JavaScript HERE';
}
add_shortcode('for_homepage', 'for_homepage');
And then my shortcode would be [for_homepage]
but I don't believe that this is the best approach - I think it is better to enque the script?
Also - should I print it directly on the page (at the bottom where it needs to be for this to work) for speed reasons?
Thanks!
BTW this is to make this switcher work: https://proto.io/freebies/onoff/
++++++
UPDATE
I believe that this might be the better approach:
function add_inline_script() {
echo "<script>/* do awesome things */</script>\n";
}
add_action( 'wp_head', 'add_inline_script', 0 );
However - I would change the wp_head
to the correct footer function...
Is this the best approach?
Share Improve this question edited Mar 20, 2019 at 11:49 Qaisar Feroz 2,1471 gold badge9 silver badges20 bronze badges asked Mar 20, 2019 at 9:14 HenryHenry 9831 gold badge8 silver badges31 bronze badges 2 |1 Answer
Reset to default 0You can use the functions is_front_page() or is_home() depending on what your home page is. Also instead of wp_head you can use wp_enqueue_scripts Your code will look something like this
function add_inline_script() {
if(is_home() || is_front_page)
wp_enqueue_script('js-handler-name', 'js-url');
}
add_action( 'wp_enqueue_scripts', 'add_inline_script');
- 有点厉害 win10兼容安卓应用方式曝光
- 微软答疑Surface平板:32GB版可用空间20GB
- Intel毫不客气:ARM+Win8软硬件都不行
- Pre-increment and Post-increment in C - Stack Overflow
- reactjs - NPM SEMANTIC RELEASE | MAINTENANCE BRANCH - Stack Overflow
- c++ - Why is my OpenGL application rendering a 3D model with unexpected transparency? - Stack Overflow
- Unable to create a azure synapse workspace? - Stack Overflow
- node.js - Send GET request with pfx file using AXIOS failed - Stack Overflow
- unity game engine - After commit to git, all gameobjects loose their assets - Stack Overflow
- material ui - Using ShadowDOM to address MUI 5 vs MUI 6 Compatibility Issue? - Stack Overflow
- python - Sympy: Define custom derivative on symbol - Stack Overflow
- python - hydra submitit launcher plugin fails to import modules that can be imported normally when omitting the plugin - Stack O
- rust - Why is the compiler asking for Sized, when I already added it? - Stack Overflow
- javascript - Mapping through these JSON elements - Stack Overflow
- google sheets - shortest formula to get the last non zero value from column - Stack Overflow
- Dbeaver customizing column and table name colors - Stack Overflow
- reactjs - Google Books API setOnLoadCallback works only after page reload - Stack Overflow
wp_enqueue_script()
where you can set dependencies and whether to use the script in the footer. – Max Yudin Commented Mar 20, 2019 at 10:13wp_footer
action? – cybmeta Commented Mar 20, 2019 at 12:02