Title: Undefined Function in JavaScript Plugin
Last modified: August 20, 2016

---

# Undefined Function in JavaScript Plugin

 *  [smpayne2](https://wordpress.org/support/users/smpayne2/)
 * (@smpayne2)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/undefined-function-in-javascript-plugin/)
 * Getting an error:
 * Fatal error: Call to undefined function wp_get_current_user() in /homepages/46/
   d175187141/htdocs/The Little Shops Boutique/wordpress/wp-content/plugins/ext-
   links-params/ext-links-params.php on line 11
 * This is plugin opening:
 *     ```
       <?php defined( 'ABSPATH' ) OR die( 'No direct access.' );
       /*
       Plugin Name: External Link Params
       Description: Add extra querystring params to external links (JavaScript)
       Author: Victor Villaverde Laan
       Version: 1.00
       License: Dual licensed under the MIT and GPL licenses
       */
           wp_get_current_user();
       ```
   
 * How in the world is this function undefined?!
 * (I am a noncoder)

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Thread Starter [smpayne2](https://wordpress.org/support/users/smpayne2/)
 * (@smpayne2)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/undefined-function-in-javascript-plugin/#post-2339168)
 * I thought this function was built in to WordPress.
 *  [bcwp](https://wordpress.org/support/users/bcwp/)
 * (@bcwp)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/undefined-function-in-javascript-plugin/#post-2339169)
 * Yes it is. But that looks like some interesting code… As far as I was taught,
   you’re not supposed to put anything before the /* */ plugin declaration, other
   than “<?php”. However, the problem may have something to do with this statement
   in the WordPress codex:
 * [http://codex.wordpress.org/Function_Reference/wp_get_current_user](http://codex.wordpress.org/Function_Reference/wp_get_current_user)
 *     ```
       Usage
   
       <?php wp_get_current_user(); ?>
   
       Use the init or any subsequent action to call this function. Calling it outside of an action can lead to troubles. See #14024 for details.
       ```
   
 * I would contact the plugin developer directly, and ask them what they intended
   the code to look like because that code doesn’t make any sense. I don’t think
   you’d want to call wp_get_current_user() without assigning the return value to
   a variable, like this:
 * `$current_user = wp_get_current_user();`
 * and you would want to call it inside of an action or filter hook.
 *  Thread Starter [smpayne2](https://wordpress.org/support/users/smpayne2/)
 * (@smpayne2)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/undefined-function-in-javascript-plugin/#post-2339170)
 * Well, this is what follows:
 *     ```
       wp_get_current_user();
           $user_id = $current_user->ID;
           echo 'User ID: '.$user_id.'<br />';
                   $ext_link_param = 'ul='.$user_id;
   
       define( 'EXT_LINK_PARAMS', $ext_link_param );
       ```
   
 * Should they be in opposite order or something?
 *  [bcwp](https://wordpress.org/support/users/bcwp/)
 * (@bcwp)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/undefined-function-in-javascript-plugin/#post-2339171)
 * I think there’s some code missing. I would contact the developer.
 *  Thread Starter [smpayne2](https://wordpress.org/support/users/smpayne2/)
 * (@smpayne2)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/undefined-function-in-javascript-plugin/#post-2339173)
 * Okay, this is an orphan plugin, due to it being a favor I was given by someone
   who did not have time to do it. Original plugin:
 * [http://pastebin.com/8Ry0ZFGn](http://pastebin.com/8Ry0ZFGn)
 * I was told to edit the line:
 * `define( 'EXT_LINK_PARAMS', 'ul=user_id' );`
 * I asked for help on another thread for how to get the user_id for current user
   and was given:
 *     ```
       <?php
           wp_get_current_user();
           $user_id = $current_user->ID;
           echo 'User ID: '.$user_id.'<br />';
                   $ext_link_param = 'ul='.$user_id;
   
       define( 'EXT_LINK_PARAMS', $ext_link_param );
       ?>
       ```
   
 * That thread is [http://wordpress.org/support/topic/calling-the-user-id-for-javascript?replies=3#post-2381341](http://wordpress.org/support/topic/calling-the-user-id-for-javascript?replies=3#post-2381341).
   I marked it resolved at the time because I thought it was.
 * At this time, all I have IS orphan code. I’m trying to attach the current user
   id to external links, which the plugin does fine if it can call that user id 
   as the param.
 *  [bcwp](https://wordpress.org/support/users/bcwp/)
 * (@bcwp)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/undefined-function-in-javascript-plugin/#post-2339195)
 * OK, try this: Start with a fresh copy of the script you got from the pastebin.
   com link. Then add the following code right under “function ext_link_params_wp_head(){”
   and before the “?>”, so you’d have:
 *     ```
       function ext_link_params_wp_head() {
       $current_user = wp_get_current_user();
       $ext_link_param = 'ul='.$current_user->ID;
       ?>
       ...
       ```
   
 * Then change the
    `<?php echo EXT_LINK_PARAMS ?>` snippet to say `<?php echo $
   ext_link_param; ?>`
 * Now, change
    `add_action( 'wp_head', 'ext_link_params_wp', 10 );` to say `add_action('
   wp_print_scripts', 'ext_link_params_wp' );` and just to be on the safe side, 
   change `add_action( 'wp_head', 'ext_link_params_wp_head', 10 );` to ‘add_action(‘
   wp_footer’, ‘ext_link_params_wp_head’, 10 );’
 * For consistency, you can rename the ‘ext_link_params_wp_head’ function to ‘ext_link_params_wp_footer’
   if you like, just be sure to change it in both the add_action and the function
   declaration.
 * And that _should_ work, although I don’t have a test environment set up right
   now to test it.
 * It’s not a pretty solution, but unfortunately the vast number of WordPress plugins
   aren’t.
 *  Thread Starter [smpayne2](https://wordpress.org/support/users/smpayne2/)
 * (@smpayne2)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/undefined-function-in-javascript-plugin/#post-2339328)
 * Do I need to change this line at all?
 * `define( 'EXT_LINK_PARAMS', 'ul=user_id' );`
 * I did the other changes and it stopped breaking wordpress, but it doesn’t actually
   add the parameter when I click the links.
 *  [bcwp](https://wordpress.org/support/users/bcwp/)
 * (@bcwp)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/undefined-function-in-javascript-plugin/#post-2339350)
 * My suggestion was to delete that line of code entirely. There’s no reason to 
   define a constant if you’re only going to use the value once, and the constant
   is computed. That’s just making messy code messier. But you’ll have to change
   the
    `<?php echo EXT_LINK_PARAMS ?>` section to say `<?php echo $ext_link_param;?
   >`
 * Otherwise, I’m not sure why it’s not working. I usually add echo or var_dump()
   statements throughout my code when I’m debugging to test if various parts are
   being executed. For example, right under
 *     ```
       function ext_link_params_wp_head() {
       $current_user = wp_get_current_user();
       ```
   
 * I’d probably do something like:
    `echo 'And the current user ID is: '.$current_user-
   >ID.'!';` If nothing gets printed on the screen, then I know my function isn’t
   getting called. If “And the current user ID is: !” gets printed to the screen,
   then I know there’s something up with the wp_current_user() call. Actually, replacing
   the echo statement with `var_dump($current_user);` would be a better test.
 * It also helps to use your browser’s built-in script debugging capabilities. If
   the JavaScript is crashing, you need to know about it, and on what line it’s 
   erroring out on. In FireFox, you can use the wonderful FireBug plugin. In Chrome,
   you can click the wrench on the top-right, then Tools->Developer Tools. Even 
   IE has pretty good script debugging capabilities, but you’ll have to turn them
   on in the menu.
 * In fact, right before the last
    `});` in your jQuery section, add this: `alert('
   Hello World!');` and then refresh the page. If you don’t see an alert box pop
   up with “Hello World”, then either your script isn’t getting called or you have
   an error somewhere in your JavaScript.
 * Also try changing
    `add_action( 'wp_footer', 'ext_link_params_wp_head', 10 );`
   back to `add_action( 'wp_head', 'ext_link_params_wp_head', 10 );`
 * If you still can’t find the error, paste your entire plugin code here, and I’ll
   see if I can spot any obvious errors.
 *  [bcwp](https://wordpress.org/support/users/bcwp/)
 * (@bcwp)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/undefined-function-in-javascript-plugin/#post-2339357)
 * On second thought, what you’re trying to accomplish is trivial enough that there’s
   little point in spending this much time debugging the logic. Probably easier 
   just to write it from scratch:
 * 1) go to your plugins folder and create a new folder called “smpayne2”
    2) inside
   that folder, create a file called “smpayne2.php” and paste the following code
   into it:
 *     ```
       <?php
       /*
       Plugin Name: smpayne2
       Description: Appends current user ID to mailto: anchor tags.
       */
       function smp2_enqueue_script() {
       	wp_enqueue_script( 'smpayne2', plugin_dir_url( __FILE__ ) . 'smpayne2.js', array( 'jquery' ), '1.0' );
       	$current_user = wp_get_current_user();
       	wp_localize_script( 'smpayne2', 'smp2Params', array(
       		'userID' => $current_user->ID
       	) );
       } // smp2_enqueue_script
       add_action( 'wp_print_scripts', 'smp2_enqueue_script' );
       ?>
       ```
   
 * 3) in the same folder, create a file called “smpayne2.js” and paste the following
   code into it:
 *     ```
       jQuery(document).ready(function($) {
       	$('a[href^="mailto:"]').each(function() {
       		var href = $(this).attr('href');
       		href += ( -1 < href.indexOf('?') ? '&' : '?' ) + 'ul=' + smp2Params.userID;
       		$(this).attr('href', href);
       	});
       });
       ```
   
 * 4) activate the “smpayne2” plugin in WordPress
 * This should accomplish what you wanted. There’s less code, and it’s cleaner.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Undefined Function in JavaScript Plugin’ is closed to new replies.

## Tags

 * [javascript](https://wordpress.org/support/topic-tag/javascript/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [undefined function](https://wordpress.org/support/topic-tag/undefined-function/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 9 replies
 * 2 participants
 * Last reply from: [bcwp](https://wordpress.org/support/users/bcwp/)
 * Last activity: [14 years, 5 months ago](https://wordpress.org/support/topic/undefined-function-in-javascript-plugin/#post-2339357)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
