Help solve "wp_enqueue_script was called **incorrectly**"

Please I was having error on my site until I activated wordpress debugger only to find this notice. I have been checking my function to solve this but have been unable to do that. please see the debugger notice: Notice : wp_enqueue_script was called incorrectly . Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts , admin_enqueue_scripts , or login_enqueue_scripts hooks.
I have this on my function.php :

<?php

// This file is part of the Carrington Mobile Theme for WordPress
// http://carringtontheme.com
//
// Copyright (c) 2008-2009 Crowd Favorite, Ltd. All rights reserved.
// http://crowdfavorite.com
//
// Released under the GPL license
// http://www.opensource.org/licenses/gpl-license.php
//
// **********************************************************************
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// **********************************************************************

if (__FILE__ == $_SERVER['SCRIPT_FILENAME']) { die(); }

load_theme_textdomain('carrington-mobile');

define('CFCT_DEBUG', false);
define('CFCT_PATH', trailingslashit(TEMPLATEPATH));
define('CFCT_HOME_LIST_LENGTH', 5);
define('CFCT_HOME_LATEST_LENGTH', 250);

$cfct_options = array(
'cfct_about_text'
, 'cfct_credit'
, 'cfct_posts_per_archive_page'
, 'cfct_wp_footer'
);


// Changing excerpt length
function new_excerpt_length($length) {
return 25;
}
add_filter('excerpt_length', 'new_excerpt_length');

// Changing excerpt more
function new_excerpt_more($more) {
return '....';
}
add_filter('excerpt_more', 'new_excerpt_more');








function getPostViews($postID){
   $count_key = 'post_views_count';
   $count = get_post_meta($postID, $count_key, true);
   if($count==''){
       delete_post_meta($postID, $count_key);
       add_post_meta($postID, $count_key, '0');
       return "0 View";
   }
   return $count.' Views';
}
function setPostViews($postID) {
   $count_key = 'post_views_count';
   $count = get_post_meta($postID, $count_key, true);
   if($count==''){
       $count = 0;
       delete_post_meta($postID, $count_key);
       add_post_meta($postID, $count_key, '0');
   }else{
       $count++;
       update_post_meta($postID, $count_key, $count);
   }
}
// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);





function time_ago( $type = 'post' ) {
   $d = 'comment' == $type ? 'get_comment_time' : 'get_post_time';

   return human_time_diff($d('U'), current_time('timestamp')) . " " . __('ago');

}

function pagination($pages = '', $range = 4)
{  
    $showitems = ($range * 2)+1;  

    global $paged;
    if(empty($paged)) $paged = 1;

    if($pages == '')
    {
        global $wp_query;
        $pages = $wp_query->max_num_pages;
        if(!$pages)
        {
            $pages = 1;
        }
    }   

    if(1 != $pages)
    {
        echo "<div class=\"pagination\"><span>Page ".$paged." / ".$pages."</span>";
        if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
        if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";

        for ($i=1; $i <= $pages; $i++)
        {
            if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
            {
                echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
            }
        }

        if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";  
        if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
        echo "</div>\n";
    }
}

function cfct_blog_init() {
if (cfct_get_option('cfct_ajax_load') == 'yes') {
cfct_ajax_load();
}
}
add_action('init', 'cfct_blog_init');

function cfct_archive_title() {
if(is_author()) {
$output = __('Posts by:');
} elseif(is_category()) {
$output = __('Category Archives:');
} elseif(is_tag()) {
$output = __('Tag Archives:');
} elseif(is_archive()) {
$output = __('Archives:');
}
$output .= ' ';
echo $output;
}

function cfct_mobile_post_gallery_columns($columns) {
return 1;
}
add_filter('cfct_post_gallery_columns', 'cfct_mobile_post_gallery_columns');

if (!is_admin()) {
wp_enqueue_script('jquery');
wp_enqueue_script('carrington-mobile', get_bloginfo('template_directory').'/js/mobile.js', array('jquery'), '1.0');
}

include_once(CFCT_PATH.'carrington-core/carrington.php');
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];

if(empty($first_img)){
$first_img = "https://dailygisters.com/wp-content/uploads/2018/12/images-1-1.jpeg";
}
return $first_img;
}
?>
<?php
function string_limit_words($string, $word_limit)
{
 $words = explode(' ', $string, ($word_limit + 1));
 if(count($words) > $word_limit)
 array_pop($words);
 return implode(' ', $words);
}
?>

<?php
function wpse52737_enqueue_comment_reply_script() {
   if ( get_option( 'thread_comments' ) ) {
       wp_enqueue_script( 'comment_reply' );
   }
}
add_action( 'comment_form_before', 'wpse52737_enqueue_comment_reply_script' );



function wpguy_linked_image($content){

   $searchfor = '/(<img[^>]*\/>)/';
   $replacewith = '<a href="'.get_permalink().'">$1</a>';

   if (is_single() === FALSE){
       $content = preg_replace($searchfor, $replacewith, $content, 1);
   }
   return $content;

}

add_filter('the_content', 'wpguy_linked_image');
function isa_add_img_title( $attr, $attachment = null ) {

   $img_title = trim( strip_tags( $attachment->post_title ) );

$attr['title'] = the_title_attribute( 'echo=0' );    
$attr['alt'] = $img_title;

   return $attr;
}
add_filter( 'wp_get_attachment_image_attributes','isa_add_img_title', 10, 2 );
// remove width & height attributes from images
//
function remove_img_attr ($html)
{
   return preg_replace('/(width|height)="\d+"\s/', "", $html);
}

add_filter( 'post_thumbnail_html', 'remove_img_attr' );







?>



Please can I get any help on this?

Friends I was able to solve this by moving below line to the top

function my_init_method() {
   if (!is_admin()) {
wp_enqueue_script('jquery');
wp_enqueue_script('carrington-mobile', get_bloginfo('template_directory').'/js/mobile.js', array('jquery'), '1.0');

}
}    
add_action('wp_enqueue_scripts', 'my_init_method');



1 Like

I’m not for sure this will help but you can try moving this code to the bottom of the functions.php

if (!is_admin()) {
wp_enqueue_script('jquery');
wp_enqueue_script('carrington-mobile', get_bloginfo('template_directory').'/js/mobile.js', array('jquery'), '1.0');
}

That would be incorrect way to do it. It would basically run as soon as the PHP parser reaches that point which in most cases won’t be the correct time to make those calls.

Note the warning issued by the debugger as mentioned in OP

Notice : wp_enqueue_script was called incorrectly . Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts , admin_enqueue_scripts , or login_enqueue_scripts hooks.

This explains what to do - wp_enqueue_script() should never be called until wp_enqueue_scripts , admin_enqueue_scripts or login_enqueue_scripts hooks have run. Which is why that call should be made inside a function and that function should be set as callback on one or all 3 of these hooks - depending on where you want the JS to be available.

Thanks guys for your help on that…I later switched to Generatepress framework and everything works fine for me. I get above 90 on both mobile and desktop pagespeed score and on chrome lighthouse audits I get 67 100 100 100. This is very good an what I have been trying to achieve. Thanks all. You can take a look at my site tophits .ng (remove space before the dot.)