In My Custom Functions plugin I have
function dropin_location() {
if (is_page ('7')) {
echo '<script>';
echo "alert('got here')";
echo '</script>';
}
}
add_action('init', 'dropin_location');
On saving I get 403 error 'Access to this resource on the server is denied!' What am I doing wrong please.
In My Custom Functions plugin I have
function dropin_location() {
if (is_page ('7')) {
echo '<script>';
echo "alert('got here')";
echo '</script>';
}
}
add_action('init', 'dropin_location');
On saving I get 403 error 'Access to this resource on the server is denied!' What am I doing wrong please.
Share Improve this question edited Nov 30, 2019 at 22:07 alexwc_ 3265 silver badges18 bronze badges asked Nov 30, 2019 at 16:39 user2005143user2005143 132 bronze badges1 Answer
Reset to default 1You can use wp_head
. This will insert the script in the head.
function dropin_location() {
if ( is_page( 7 ) ) {
?>
<script>alert('got here')</script>'
<?php
}
}
add_action('wp_head', 'dropin_location');
However, there are other ways to add scripts. You can have it as a script file in your theme and enqueue it so that WordPress "knows" about it. This way it can have dependencies, load at the correct time, etc. Here is an explanation.
So if you have my-killer-script.js
in your theme, you can do the following:
wp_enqueue_script(
'some-script-handle', // some name to give it
get_theme_file_uri( '/path/to/my-killer-script.js' ), // file path to script
array( 'some-dependency-handle' ), // does this script need any dependencies? If so, add their handles, or leave the array empty.
false, // do you need a version?
true // load in footer? true/false
);
Read the WP docs on wp_enqueue_script here.