How do i call a javascript function from wordpress?

Can I call a JavaScript function in Wordpress like this?


Where should I put my JavaScript? at footer.php? or what would be the proper way to achieve something like this?

I put my script in footer.php, then I called the function like as I mentioned but no use?

How do i call a javascript function from wordpress?

asked May 28, 2015 at 6:06

How do i call a javascript function from wordpress?

1

Yes you can use onclick="callGetDomain();" and for the function definition, use a separate js file and include that file using wp_enqueue_script(), which is the proper way to call js files in Wordpress. Check this

answered May 28, 2015 at 6:10

How do i call a javascript function from wordpress?

Vidya LVidya L

2,2514 gold badges26 silver badges41 bronze badges

Contents

  • 1 JavaScript in Template Files
  • 2 JavaScript in Posts
  • 3 Creating a Multiple Script File
  • 4 Troubleshooting JavaScript
  • 5 Resources

JavaScript will work within WordPress. If used within the template files, most JavaScript will work fine. Here are a few tips to make your JavaScript work in WordPress.

JavaScript in Template Files

To use JavaScript repeatedly within your site, you can either set the call for the JavaScript, or the script itself, in the head of your header.php template file, between the meta tags and the style sheet link, no differently than you would if you were using JavaScript in any HTML page. To "load" the JavaScript file into your site, in the head, add something like this:


Be sure that you define the type correctly, as your site will not validate without it.

In the spot where you wish to use the JavaScript, set the call for the JavaScript. For example, you are using a JavaScript that sets a link for users to "email this page" to a friend and you want it to be under the post title. It might look like this:

JavaScript in Posts

To use JavaScript inside of posts in WordPress, you need to take a few more steps. Odds are that this usage is for one or only a few instances, so adding the script to the header would only add code you don't need to your header.php template file.

For the occassional or one time use of JavaScript, you need to put the script into a JavaScript file and then call it out from within the post. Make sure that each script is defined by its function name such as:

function updatepage(){var m="Page updated "+document.lastMo.......}

To include a JavaScript inside a post, you need to combine both the call to the script file with the call to the JavaScript itself.


Creating a Multiple Script File

You might have a collection of scripts that you call from time to time, like a scripts which calculate time zones or distance, or maybe scripts that create some effect or accent on your page. For reoccurring JavaScripts, consider grouping them together into one file.

Let's call our group JavaScripts file scriptfile.js (choose whatever you want) and say it contains the updatepage, emailpage, and caltimezone scripts. As you copy each JavaScript into the file, make sure it has a unique function name such as with this condensed version:

function updatepage() {var m="Page updated "+document.lastMo.......}
function emailpage() {mail_str = "mailto:?subject=....}
function caltimezone() {var timerID ; function tzone(tz, os, ds, cl) {this.ct =......} 

Place the script file of all the JavaScripts in the head of the header.php template file between the meta tags and the style sheet link. It will just sit there, loaded into the browser's memory, waiting for one of the scripts inside to be called.


In the spot in your post where you would like to use the JavaScript, call it as follows:


Troubleshooting JavaScript

If you are having trouble with including JavaScripts inside a post, use the Text Control Plugin which allows you to control on a global or per post basis the ability to turn off WordPress's automatic formatting features which can quickly turn a code into something readable instead of executable. Set the options on the post that you will be using the JavaScript on to have No Formatting or Markup or nl2br and No Character Formatting. You may have to experiment to get it to work. As a reminder, when using the Text Control Plugin, you must first Save and Continue Editing the post in order to see the Text Control Plugin options.

If you choose No Formatting, your post's text will all run together, so you will have to add paragraph tags and other HTML tags in order to format your page as WordPress normally does that for you.

If your JavaScript doesn't work, triple check that you haven't made any errors during the cut and paste into a group or single file. Be sure you used a text editor and not a word processing program to create the JavaScript file. Check the name of the function in the script file as well as on your site. Not all JavaScripts may work, and could possibly conflict with your PHP commands, but this is very rare.

If you are really having trouble with this, the WordPress Support Forum may be able to help.

Resources

  • Script Downloads
  • Hotscripts
  • PHP Resource Index
  • Only PHP

How do you call JavaScript in WordPress?

How to Add Custom JavaScript to WordPress (3 Methods).
Use the Head & Footer Code plugin (the simplest option for non-technical users).
Add code to functions. php file..
Add code to header (using the wp_enqueue_script function or the wp_head hook).

How do I call a JavaScript function call?

The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.

Can you call a JS function in PHP?

You can't call a JavaScript function from PHP, but you can output a call for your JavaScript function from your PHP source code. Then, the user's browser will take care of the rest.

What are 2 ways to call a JavaScript function?

JavaScript functions can be called:.
As a function..
As a method..
As a constructor..
via call and apply..