I created a short JavaScript file and placed it in my theme's JS folder, the contents of the file are:

 * Text2Give Functions File
 * Mobile Landing Page Functions File

jQuery( function( $ ) {
  "use strict";

  // bind change event to select
  $('#text2give').on('change', function () {
      var url = $(this).val();
      if (url) {
          window.location = url; // redirect
      return false;
  } )
} );

I've added it to be enqueued along with all the other JS scripts in my functions.php file like so:

    // Add script for landing page for text2give
if ( is_page( 'text2give') ) {
    wp_enqueue_script( 'text2give', get_template_directory_uri() . '/js/text2give.js', array( 'jquery' ), '20170614', true  );

It doesn't work when I load the above referenced page and when I view source I see the following:

<link rel="alternate" type="application/json+oembed" href="https://liquidchurch.com/wp-json/oembed/1.0/embed?url=https%3A%2F%2Fliquidchurch.com%2Ftext2give%2F"/>
<link rel="alternate" type="text/xml+oembed" href="https://liquidchurch.com/wp-json/oembed/1.0/embed?url=https%3A%2F%2Fliquidchurch.com%2Ftext2give%2F&#038;format=xml"/>

All the other scripts look like this:

<script type='text/javascript' src='https://liquidchurch.com/wp-includes/js/jquery/jquery.js?ver=1.12.4'></script>

Any ideas on why this is happening? Thanks!

  • 1
    not totally sure but one thing to check is the last parameter set to true ($in_footer) which means the script will not end up in <head>? try changing it to false. – majick Jun 14 '17 at 19:04
  • 1
    Can you try it without the if ( is_page( 'text2give') ) {} and see if it loads? – czerspalace Jun 14 '17 at 19:29
  • 1
    It's not! Those oEmbed link tags are not related to your script being enqueued or not. This is added by core. – birgire Jun 14 '17 at 19:36
  • 1
    ... and make sure to have wp_footer() in your page template if you're want to load scripts in footer. – birgire Jun 14 '17 at 19:53
  • 1
    I renamed the file text2give-js.js to make it easier for me to distinguish between the page (text2give) and the JS file - this revealed that @birgire was absolutely right - I was mistaking JSON and XML versions of the text2give page for being the JS script (gahh!). I also changed the footer value to false. I can see that text2give-js.js is being loaded in the header via view source, but the code still doesn't work. – davemackey Jun 14 '17 at 19:57

The oEmbed discovery link tags are added by core in wp_oembed_add_discovery_links() since version 4.4. See the changeset here.

When we enqueue scripts in the footer, we have to make sure the wp_footer() is not missing.

There was a typo in the enqueued script here, since the HTML select tag had id="txt2give" but not id="text2give".

  • Thanks, appreciate the help! I may have been staring at this code for a long time before noticing those issues, great to have a second pair of eyes! – davemackey Jun 14 '17 at 20:22
  • 2
    Glad to hear it helped, yes sometimes it helps to sleep on it or get some fresh eyes to look at it ;-) – birgire Jun 14 '17 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.