WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] JQuery not working in Theme (4 posts)

  1. TomReich
    Member
    Posted 1 year ago #

    Had a theme custom-built for us by an undergraduate student (who no longer works for us) and we are trying to implement a plugin that requires jQuery. Unfortunately, it seems the theme does not correctly implement jQuery. Here is the code from our functions.php and header.php.

    functions.php

    <?php
    
    	// Add RSS links to <head> section
    	automatic_feed_links();
    
    	// Load jQuery
    	 if ( !is_admin() ) {
    	   wp_deregister_script('jquery');
    	   wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"), false);
    	   wp_enqueue_script('jquery');
    	} 
    
    	// Clean up the <head>
    	function removeHeadLinks() {
        	remove_action('wp_head', 'rsd_link');
        	remove_action('wp_head', 'wlwmanifest_link');
        }
        add_action('init', 'removeHeadLinks');
        remove_action('wp_head', 'wp_generator');
    
    	// Declare sidebar widget zone
        if (function_exists('register_sidebar')) {
        	register_sidebar(array(
        		'name' => 'Search',
        		'id'   => 'search',
        		'description'   => 'Search widget',
        		'before_widget' => '<div id="%1$s" class="widget %2$s">',
        		'after_widget'  => '</div>',
        	));
    		register_sidebar(array(
    			'name'=> 'Slideshow',
    			'id' => 'slideshow',
    			'before_widget' => '<li id="%1$s" class="widget %2$s">',
    			'after_widget' => '</li>',
    		));
    		register_sidebar(array(
    			'name'=> 'Events',
    			'id' => 'events',
    			'before_widget' => '<li id="%1$s" class="widget %2$s">',
    			'after_widget' => '</li>',
    		));
    		register_sidebar(array(
    			'name'=> 'News',
    			'id' => 'news',
    			'before_widget' => '<li id="%1$s" class="widget %2$s">',
    			'after_widget' => '</li>',
    		));
    		register_sidebar(array(
    			'name'=> 'About Us',
    			'id' => 'about_us',
    			'before_widget' => '<li id="%1$s" class="widget %2$s">',
    			'after_widget' => '</li>',
    		));
    	}
    
    	add_action( 'init', 'register_my_menus' );
    	function register_my_menus() {
    	register_nav_menus(
    
    	array(
    
    	'menu-1' => __( 'Menu 1' )
    		)
    	);
    }
    ?>
    <?php
    /** Tell WordPress to run yourtheme_setup() when the 'after_setup_theme' hook is run. */
    add_action( 'after_setup_theme', 'yourtheme_setup' );
    if ( ! function_exists('yourtheme_setup') ):
    /**
    * @uses add_custom_image_header() To add support for a custom header.
    * @uses register_default_headers() To register the default custom header images provided with the theme.
    *
    * @since 3.0.0
    */
    function yourtheme_setup() {
    // This theme uses post thumbnails
    add_theme_support( 'post-thumbnails' );
    // Your changeable header business starts here
    define( 'HEADER_TEXTCOLOR', '' );
    // No CSS, just IMG call. The %s is a placeholder for the theme template directory URI.
    define( 'HEADER_IMAGE', '%s/images/headers/forestfloor.jpg' );
    // The height and width of your custom header. You can hook into the theme's own filters to change these values.
    // Add a filter to yourtheme_header_image_width and yourtheme_header_image_height to change these values.
    define( 'HEADER_IMAGE_WIDTH', apply_filters( 'yourtheme_header_image_width', 972 ) );
    define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'yourtheme_header_image_height',	109 ) );
    // We'll be using post thumbnails for custom header images on posts and pages.
    // We want them to be 940 pixels wide by 198 pixels tall (larger images will be auto-cropped to fit).
    set_post_thumbnail_size( HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true );
    // Don't support text inside the header image.
    define( 'NO_HEADER_TEXT', true );
    // Add a way for the custom header to be styled in the admin panel that controls
    // custom headers. See yourtheme_admin_header_style(), below.
    add_custom_image_header( '', 'yourtheme_admin_header_style' );
    // … and thus ends the changeable header business.
    // Default custom headers packaged with the theme. %s is a placeholder for the theme template directory URI.
    register_default_headers( array (
    'green' => array (
    'url' => '%s/images/headers/header-1.png',
    'thumbnail_url' => '%s/images/headers/header-1-thumb.png',
    'description' => __( 'Green', 'yourtheme' )
    ),
    'blurryplant' => array (
    'url' => '%s/images/headers/header-2.png',
    'thumbnail_url' => '%s/images/headers/header-2-thumb.png',
    'description' => __( 'Blurry Plant', 'yourtheme' )
    ),
    'sharpplant' => array (
    'url' => '%s/images/headers/header-3.png',
    'thumbnail_url' => '%s/images/headers/header-3-thumb.png',
    'description' => __( 'Sharp Plant', 'yourtheme' )
    ),
    'green2' => array (
    'url' => '%s/images/headers/header-4.png',
    'thumbnail_url' => '%s/images/headers/header-4-thumb.png',
    'description' => __( 'Green2', 'yourtheme' )
    ),
    'white' => array (
    'url' => '%s/images/headers/header-5.png',
    'thumbnail_url' => '%s/images/headers/header-5-thumb.png',
    'description' => __( 'White', 'yourtheme' )
    ),
    'grey' => array (
    'url' => '%s/images/headers/header-6.png',
    'thumbnail_url' => '%s/images/headers/header-6-thumb.png',
    'description' => __( 'Grey', 'yourtheme' )
    )
    ) );
    }
    endif;
    if ( ! function_exists( 'yourtheme_admin_header_style' ) ) :
    /**
    * Styles the header image displayed on the Appearance > Header admin panel.
    *
    * Referenced via add_custom_image_header() in yourtheme_setup().
    *
    * @since 3.0.0
    */
    function yourtheme_admin_header_style() {
    ?>
    <style type="text/css">
    #headimg {
    height: <?php echo HEADER_IMAGE_HEIGHT; ?>px;
    width: <?php echo HEADER_IMAGE_WIDTH; ?>px;
    }
    #headimg h1, #headimg #desc {
    display: none;
    }
    </style>
    <?php
    }
    endif;
    ?>

    and header.php

    <!DOCTYPE html>
    <html <?php language_attributes(); ?>>
    <LINK REL="SHORTCUT ICON" HREF="Assets/sustainability.ico" />
    <head>
    	<meta charset="<?php bloginfo('charset'); ?>" />
    
    		<?php if (is_search()) { ?>
    	   <meta name="robots" content="noindex, nofollow" />
    	<?php } ?>
    
    		<title>
    		   <?php
    		      if (function_exists('is_tag') && is_tag()) {
    		         single_tag_title("Tag Archive for &quot;"); echo '&quot; - '; }
    		      elseif (is_archive()) {
    		         wp_title(''); echo ' Archive - '; }
    		      elseif (is_search()) {
    		         echo 'Search for &quot;'.wp_specialchars($s).'&quot; - '; }
    		      elseif (!(is_404()) && (is_single()) || (is_page())) {
    		         wp_title(''); echo ' - '; }
    		      elseif (is_404()) {
    		         echo 'Not Found - '; }
    		      if (is_home()) {
    		         bloginfo('name'); bloginfo('description'); }
    		      else {
    		          bloginfo('name'); }
    		      if ($paged>1) {
    		         echo ' - page '. $paged; }
    		   ?>
    	</title>
    
    	<meta http-equiv="content-type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
         <link rel="stylesheet" type="text/css" href="<?php bloginfo('style.css'); ?>" />
    
        <?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?>
    
        <script>
      for(var e,l='article aside footer header nav section time'.split(' ');e=l.pop();document.createElement(e))
    </script>
    
    	<link rel='stylesheet' href="<?php bloginfo('stylesheet_url'); ?>">
    	<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>">
    
    	<!--[if IE]>
    	<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    	<![endif]-->
    
    	<script type="text/javascript" src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js'></script>
    		<script type="text/javascript"></script>
    <?php wp_head(); ?>
    </head>
    
    <body <?php body_class(); ?>>
    
    <header><?php
    // Check if this is a post or page, if it has a thumbnail, and if it's a big one
    if ( is_singular() &&
    has_post_thumbnail( $post->ID ) &&
    ( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'post-thumbnail') ) &&
    $image[1] >= HEADER_IMAGE_WIDTH ) :
    // We have a new header image!
    echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
    else : ?>
    <img src="<?php header_image(); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" alt="" />
    <?php endif; ?></header>
    
    <nav>
    <div id="nav-bar" class="nav">
    <?php wp_nav_menu( array( 'theme_location' => 'menu-1' ) ); ?>
    </div>
    </nav>
    
    <div id="page-wrap">
    
    <div id="container">

    I have very little experience with coding to know how to troubleshoot this issue. If anyone can spot the problem, it would be a huge help. If any more information is needed, let me know. Thanks in advance!

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    I'm sorry but jQuery has been implemented very badly in this theme. For example, this line;

    wp_deregister_script('jquery');

    is an absolute no-no. You should NEVER deregister core jQuery. The level of coding needed to sort this out is really beyond the scope of the support we are able to provide in the forum. I can only suggest that you consider hiring someone to sort this them out for you.

  3. TomReich
    Member
    Posted 1 year ago #

    Actually, your answer seemed to be enough.

    I removed this entire section:

    // Load jQuery
    	 if ( !is_admin() ) {
    	   wp_deregister_script('jquery');
    	   wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"), false);
    	   wp_enqueue_script('jquery');
    	}

    and it seems to have done the trick. The plugin with issue is working like a charm. Thanks for pointing out that "absolute no-no"

    Best!

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    Glad I could help :-)

Topic Closed

This topic has been closed to new replies.

About this Topic