最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Custom function in My Custom Functions returning 403

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

You 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.

发布评论

评论列表(0)

  1. 暂无评论