0

There is a snippet of JavaScript code I would like to include in a WordPress page. What's happening is the code isn't loading properly and it's getting hung up somewhere in WordPress's native redirect commands.

This is the code I was supplied (and which works on their site and on other non-WordPress domains):

<link href="http://www.othersite.com/thepage.php?uid=1234" type="text/css" rel="stylesheet" /> 
<script type="text/javascript" src="http://www.othersite.com/thepage.php?uid=1234"></script>
<a id="OtherSite" target="_blank" href="http://www.othersite.com" title="The Other Site">Link to OtherSite</a>

If I look directly at the my page source with the code, it is pointing in the right direction. If I open my browser's javascript console for the exact same page, it shows the link has redirected itself back to my domain so instead of going to othersite.com/page.php it is actually trying to get to mysite.com/page.php.

I have tried

Redirect 301 mysite.com/thepage.php othersite.com/thepage.php

I have also tried adding the following to my functions.php (which broke everything)

<?php 
if (function_exists('myscript')) {
function myscript() {
     if (is_page('ThePage')) {
    wp_register_script('myscript', 'http://www.othersite.com/thepage.php?uid=1234'__FILE__); 
    wp_enqueue_script('myscript');
}
}
}
add_action('init', 'myscript');
?>

Does anyone know how I can get this to work?

EDIT @cole Honestly I have no idea why the __ FILE__ is there. I know enough about this stuff to be a menace to myself but not really enough to create my own work. The PHP stuff is what I put together based on the tutorial The Ins and Outs of The Enqueue Script For WordPress Themes and Plugins.

EDIT @morganestes I thought that was a little odd myself, but that's what they gave me. The CSS isn't intended to mess with anything in the othersite php, just the link-back to them at the bottom. I put your code into functions.php and while it didn't exactly break the site like my other attempts, it did have the unfortunate effect of being placed at the very top outside any of my theme elements. It also didn't fix the problem; wordpress is still changing the link from othersite.com to mysite.com.

EDIT @helga The fundamental problem is that Wordpress is taking it upon itself to magically rewrite my outbound link to refer to a non-existent internal location.

4
  • why the FILE variable at the end of your wp_register_script()? Looks like that would be invalid syntax
    – Cole
    Commented Feb 23, 2014 at 4:19
  • I don't understand. The code you have loads both a stylesheet and a JavaScript file from the SAME URL (thepage.php?uid=1234)? Also, are you just trying to load a script from an external source, as the code suggests? Commented Feb 23, 2014 at 5:42
  • I don't understand either. In making the code so generic, I can't really tell what you are trying to achieve. Plus the script looks like a PHP page (thepage.php?uid=1234) and not a script? morganestes' answer will get your script loaded properly. After that, it seems like a scripting issue. Commented Feb 23, 2014 at 8:57
  • Oh and check out wp_remote_get() and the iFrame plugin. Maybe either will be helpful. (or not) Commented Feb 23, 2014 at 9:16

1 Answer 1

2

If you're trying to load a script from an external source (as your code suggests), please don't use that article as a reference. It's littered with bad code, and the comments on the page state several times that it's outdated and points you to a new article.

Here's how you register and enqueue a script on a specific page (using a cleaned up version of your code):

if ( ! function_exists( 'myscript' ) ) {
    function myscript() {
        if ( is_page( 'ThePage' ) ) {
            wp_register_script( 'myscript', 'http://www.othersite.com/thepage.php?uid=1234' );
            wp_enqueue_script( 'myscript' );
        }
    }
}
add_action( 'wp_enqueue_scripts', 'myscript' );

Here's what was changed, and why:

if (function_exists('myscript')) {
function myscript() {

You're saying that if this function exists, then declare a function with the exact same name. This won't work since you can't reuse function names. You really wanted to check if the function doesn't exist.

     if (is_page('ThePage'))

is_page() checks several things to see what page you're on, specifically

@param mixed $page Page ID, title, slug, or array of such.

Your page better have a title of "ThePage", or the script won't load (slugs are all lowercase).

    wp_register_script('myscript', 'http://www.othersite.com/thepage.php?uid=1234'__FILE__); 

This is close to right, but that __FILE__ is just messing up the function call and needs to be removed here. Bad stuff.

add_action('init', 'myscript');

The proper hook for loading your own scripts on non-admin pages is wp_enqueue_scripts.

Now, assuming the script you registered actually returns some valid JavaScript, you'll see it loaded on the page titled "ThePage".

2
  • wp_enqueue_scripts is the proper hook to use when enqueuing items that are meant to appear on the front end, not wp_enqueue_script. Commented Apr 26, 2014 at 6:02
  • Pleasure @morganestes +1 Commented Apr 26, 2014 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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