Insert Javascript in WordPress

 javascript "src =" " width = "500" height = "375" srcset = " 500w, uploads / 2013/02 / javascript.jpg 900w "sizes =" (max-width: 500px) 100vw, 500px "/> </a></p>
<p> The other day was <span class= a forum user asked how to insert Javascript in WordPress and the truth is that it is not so obvious how it might seem, but it is easy to do it.

Without specifying if it was for a hosted on your own server or on, would initially have two possible answers namely …

  1. You can not insert Javascript into sites due to a default security limitation, so if it's your case forget it, do not you can insert Javascrip t in neither in entrances neither in widgets of text nor in any other part.
  2. If you have your own WordPress the thing changes, there you have total freedom to insert, to add, to install what you want

Another clarification that must be done is that in this article does not explain how to add a Javascript code in the code of a WordPress theme because it is something so simple how to copy and paste it. A typical example would be the Google Analytics code.

Nor is it about how to insert an external script such as the YouTube videos, a Facebook fan box and others, which already offer a code with the " call " to the external Javascript needed to show the video or whatever. Here you already know, you go to the Text tab (HTML) of the editor and paste the script that is offered to you.

What is involved here is how to insert your own Javascript in an entry or page in WordPress , in the content ok?

… How to insert Javascript in an entry or WordPress page …

For an occasional or one-time use of Javascript you have to put the script in a JavaScript file and then " call it "from within the entry or page. That's right, make sure that each script is defined by its function name for example:

To include the JavaScript within the entry or page you have, then, that combine the call to the script with the call to the same JavaScript . That is, something like this:

If the attribute src of your Javascript tag is cut or truncated you will have to disable the Visual WordPress editor completely or be careful not to switch to the visual editor tab, because Sometimes it happens and it will not work.

Another very interesting and simple option is to add Javascript using a shortcode, like the one offered by the embed javascript . Simply, when you want to insert a Javascript in an entry or page you do it like this:

… Insert JavaScript in widgets …

Here we are not going to complicate, apart from directly inserting the code provided by the service that offers the content, we have the wonderful plugin HTML Javascript adder that does all the work.

This little wonder allows to insert HTML, javascripts, shortcodes, ads, flash codes, plain text and almost anything you can think of ] in a sidebar using UNLIMITED widgets that you can display in entries or pages, to choose.

… Insert JavaScript in WordPress with shortcode …

Another way, that I personally liked it a lot, of insert JavaScript in WordPress altern first, create a shortcode using a custom field.

First you create a new custom field that you call, for example " js " , and in the field of " Value ", you paste the Javascript code you want, something like this:
 insert javascript wordpress with custom field

Once created you just have to " ] call it "from the editor like this:
 shortcode insert javascript in wordpress

The effect achieved will be the same as putting it" to capon "but with the advantage that you can also use it in the Visual tab of the WordPress editor something quite interesting by itself.

And that's it, if you know any other way you tell us in the comments. [19659005] References:

  1. Using JavaScript (WordPress Codex) [19659078] Javascript in WordPress (Labnol)

NOTICE : this publication is from two years ago or more. If it's a code or a plugin it might not work in the latest versions of WordPress, and if it's a news story it might be obsolete. Then do not say we have not warned you.

Loading …

That may also help you:

Use the latest version of jQuery automatically

If you use jQuery on your site created with WordPress and you always want to have the latest version and above that you do not have to host it on your server but load it directly from the high speed servers of Google you just have to copy this code in the file functions.php of your active topic:

What this code does is load the latest available version from the Google CDN servers so load the folder " / jquery / 1 / " instead of from the version folder available on your site. This code really "bypasses" the version you have installed and uses the latest version available in Google.

Now, use this with caution, checking that your scripts work correctly after saving the changes in the file functions.php . If something went wrong, simply delete the added lines and continue using the version hosted on your server.

Update your theme to WordPress 2.9

One of the novelties of WordPress 2.9 about to come to light, is the inclusion of image thumbnails in posts a functionality that will do more easy the development of magazine-type themes without the need to include additional scripts or plugins.

This new functionality, for your theme to support, requires a couple of changes in your current theme, namely …

1. In the file functions.php of your active topic:

2. In the files index.php and any other where you want the thumbnails to appear:

Insert text at the end of each post

This is one of the tricks I have been searching for the longest and fortunately I found it. What it offers is a simple way to insert any text at the end of our posts without the need for plugins complicated scripts or anything like that.

Just add the following code to file functions.php of your active theme and magic becomes reality:

Important news in WordPress 2.8 • WordPress Help

in the that " true " means that it is queued in the footer (" false " is the default option).

When you queue a script in the footer all the dependencies will be added (if they are not already active) and will be launched before the script. Some will be in the header, and others also in the footer. By default only jQuery is thrown in the header but when a script is queued in the header all the dependencais are also thrown in the header. Almost all external scripts will be executed after loading the page so there is no real need to queue the header.

Scripts queued for the cover footer depend on wp_footer () ; is present in the active theme. Unfortunately, this is not the case in all cases, so we will have to publish this novelty so that the developers of the themes as well as the plugins know and can include it.

To make it easier to queue the scripts, you can add two new actions :: "wp_enqueue_scripts" that runs in the header of the cover where the functions is_page (), is_home (), etc., and "admin_enqueue_scripts" are available that runs in the header of the administration layer and can be queued for specific pages.

Another important feature is that all management core scripts are concatenated and compressed before being sent to the browser. This functionality can be easily extended to include scripts added by plugins and use the server cache, although this would require some changes to the server configuration (.htaccess in Apache).

Although compression through PHP can be problematic in some hosts there are several "switches" (constants) that can manage it: define ('CONCATENATE_SCRIPTS', false); disables both concatenation and compression in all scripts. You can also use define ('COMPRESS_SCRIPTS', false); to disable JavaScript compression and define ('COMPRESS_CSS', false); for CSS files. The compression is configured by default to "deflate" since it is faster and uses less server resources. You can also force the gzip with the settings define ('ENFORCE_GZIP', true);

Also, all the main scripts are reduced. All custom scripts are included in two versions: .dev.js is the unreduced scripts, and .js is the reduced version. The constant defines ('SCRIPT_DEBUG', true); will load the .dev.js versions of the scripts making debugging easier.

There are some possible changes: removing the switch COMPRESS_CSS ] and using only COMPRESS_SCRIPTS use compression in "deflate" mode but adding the file header of gzip and use it as "encoded content gzip" since it seems to be more compatible with web servers and proxys (all current browsers have "deflate" support).

In short, a series of internal changes that will surely continue to make WordPress the most advanced CMS and, surely, the most effective.

NOTICE ]: This publication is from two years ago or more. If it's a code or a plugin it might not work in the latest versions of WordPress, and if it's a news story it might be obsolete. Then do not say we have not warned you.

Loading …

That may also help you: