In functions.php, I have added the following code

function my_custom_js() {
    echo '<script type="text/javascript" src="//platform-api.sharethis.com/js/sharethis.js#property=58ef5701485778001223c86c&product=inline-share-buttons"></script>';
add_action('wp_head', 'my_custom_js');

This gives an error:

undefined function add_action()

But this is already defined by WordPress. Am I missing something?

  • where is your functions.php file located? – codingpuss Apr 17 '17 at 9:24
  • @rajith is your function is inside a class? – Mukii kumar Apr 17 '17 at 9:34

If add_action is undefined, it means WP is not loaded in the regular way. Assuming we are talking about functions.php in the theme folder (and not the functions.php in the wp-includes folder, which you must not mess with) there should be no problem, unless the php-file itself is run outside WP.

This could happen, for instance, if you are including the php-file with a full url, in which case WP would see it as an external source, to be evaluated outside the WP-context. In that case add_action would be undefined. Normally, this would not happen, because WP itself looks for the functions.php file in the active (child) theme, but messing up the normal way of things is certainly possible.

There is not enough context in your question to pinpoint where this could happen, but I suggest you check all include and requirestatements in your theme. Also, this may be a useful resource to track where things go wrong.


@rajith try this. This might be helps you-

add_action('wp_head', array($this, 'my_custom_js'));
  • it working on class-based coding... his coding is not class based. :) – Faysal Mahamud Apr 17 '17 at 9:25

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.