Home > functions > In jquery use php variable to execute an enqueued jquery file

In jquery use php variable to execute an enqueued jquery file

August 2Hits:0
Advertisement

I am loading some jquery on a specific Wordpress admin page, the jquery loads and works fine but I need have this jquery only trigger if a php variable exists.

In php I would just write a function and hook it into the admin page:

I am enqueuing the jquery file, i.e., foo.js that contains this following jquery:

jQuery(document).ready(function($){     $('#someID.some-class > a').append('<div class="caption"></div>');     $(".caption").text("Add this text"); }); 

So the file foo.js file loads whenever the themes.php file is loaded but I do not want the jquery to run unless a variable is set by the themes.php page.

I am confused as to where and how I do check the variable and trigger the jquery.

Do I just add the jquery by echoing out the the jquery from within a function?

Do I somehow add the php variable check into the foo.js file?

Answers

Do I just add the jquery by echoing out the the jquery from within a function?

Do I somehow add the php variable check into the foo.js file?

No, off course no.

Use wp_localize_script. The original aim of this function was to translate the js scripts, but it allows to pass any kind of variables to js.

function register_foo() {
  wp_register_script('foo-js', plugins_url('js/foo.js', __FILE__), array('jquery'), null);
  $mydata = array(
    'foo' => 'bar', 'awesome' => 'yes'
  );
  wp_localize_script('foo-js', 'mydata', $mydata); // this one do the magic
  wp_enqueue_script('foo-js');
}
add_action('admin_enqueue_scripts', 'register_foo');

With this snippet the file 'foo.js' is inserted in admin pages, and a variable, that is a json object, is passed to the script and you can use it as you want.

Now, as example, in your 'foo.js' you can have;

jQuery(document).ready(function($){
    var foo = mydata.foo;
    var awesome = mydata.awesome;
    if ( foo == 'bar' ) {
      $('#someID.some-class > a').append('<div class="caption"></div>');
      $(".caption").text("Is this script awesome? " + awesome + '!');
    }
});

Related Articles

  • In jquery use php variable to execute an enqueued jquery fileAugust 2

    I am loading some jquery on a specific Wordpress admin page, the jquery loads and works fine but I need have this jquery only trigger if a php variable exists. In php I would just write a function and hook it into the admin page: I am enqueuing the j

  • Exec location path is added to session "path" variable then executing command in .wixproj file - is not found (exiting with code 9009)January 14

    I have wixproj file like this: <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> ... <Imp

  • Refused to execute script from jquery-1.7.2.min.js because MIME type('text/html') is not executableAugust 11

    I am using jquery-1.7.2.min.js as my jQuery library. This is a file that I have saved in my Style Library and I am referencing it in my master page. For some users it is failing and the error they are getting is the following: Refused to execute scri

  • How to get variable IDs from OOP in jQuery?November 21

    I created image widget and tried to use $variable ($this->get_field_id('widget_image') in jQuery. Unfortunately, I cannot get it in jQuery. It shows Uncaught ReferenceError: object_name is not defined Code // Creating the widget class Image_Widget ex

  • how to Create A SP with table variable and execute 3 SP statements in the same statement

    how to Create A SP with table variable and execute 3 SP statements in the same statementJanuary 25

    how to Create A SP with table variable and execute 3 SP statements in the same statement . exec [dbo].[sp_KPI_Churn_MTD_Current_Year] exec [dbo].[sp_KPI_Churn_MTD_Previous_Year] exec [dbo].[sp_KPI_Churn_MTD_Less_Than_Previous_Year] Results should be

  • Enqueuing jQuery in plug-insSeptember 2

    I want to use jQuery in a plugin. In functions.php of the theme I'm using, I found: if(!is_admin()) { wp_deregister_script('jquery'); wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js'); As I understand it,

  • bash execute read command from file

    bash execute read command from fileAugust 29

    After some manipulations, I got the file with commands that I would be glad to be able to execute. Input file (name of file is inp2.txt): "02 - Beautiful Emptiness.mp3" "02 - Come. mp3" "02 - Go For It.mp3" Code: #!/bin/bash

  • Add a js file and be sure it is executed as last js file?March 1

    I need to add a js file and make it to be executed as last js file (after all js that belong to modules) I create a module for this. But I can't manage the order of modules execution. Don't I? ┬┐should I add this file in my theme or through a function

  • couldn't execute "mysql": no such file or directory, using autoexpectSeptember 12

    I have the following file: exp.exp #!/usr/bin/expect db_host='localhost' db_name='webui_dev' db_user='root' db_pass='rootpass' new_db_name='db_2011' expect <<EOF log_user 0 spawn mysql -h $db_host -u $db_user -p $db_pass 'create database $new_db_nam

  • Accessing executables without going to file location on command lineDecember 20

    In my school Unix servers, I can run certain programs on the command line without going to the file where they are located. For example, shell> java filename would run the file with java. The home-computer I use has Windows on it, and I was wondering

  • JQuery UI not loading without explicit loading of jQueryJune 29

    I know there are a lot of questions regarding this issue, but I can't figure out mine here. I'm trying to load the whole jQuery UI ( Actually I need only the Slider ) and I successed. This is my enqueing which is working: wp_enqueue_script('jquery-go

  • Typescript and jQuery type. The right way to import jquery type?February 9

    Is it possible to include jquery.d.ts and use jQueryStatic type? I need something like ... protected el : jQueryStatic; ... No matter how I try I can not import jQueryStatic interface from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/maste

  • Is there a way to automatically set Execute permissions for new files?August 14

    I'm using OS X 10.6.4 and am trying to set a folder to automatically enable execute permissions on new script files copied or created in a directory. I have used Sandbox 2 to set every permission for the folder to enabled with sticky bits and the inh

  • Cannot execute binaries in Ubuntu : file not foundDecember 24

    When I try to run any executable file a file not found error occurs. Sometimes it says nothing but doesn't do anything. For example, when I execute ./blah-blah.bin it fails finding the file when it is clearly there. When I execute ./blah-blah-file-2.

  • How to enqueue jquery in admin and why is it not already there?February 9

    Why does the jquery below not fire? Its going to the "else" every time. if ( is_admin() && $pagenow == 'theme-install.php' && $_GET['tab']=="upload"){ add_action('admin_print_scripts', 'theme_upgrade_alert'); function t

  • Enqueue jQuery in WordPressJuly 29

    Building my first theme from scratch in Wordpress 3.4.1, I know Wordpress already has the latest version of the JQuery via Google. I have read about issue's if the script is not called properly, so want to try and keep everything as close to the reco

  • enqueue jQuery into the footerAugust 29

    I have looked but no where seems to cover how to place jQuery into the footer, at least not the way I have it in my theme, is there a way to place the following into the footer? // Load jQuery function my_register_jquery_function() { wp_deregister_sc

  • Cannot enqueue jQuery correctly using Google CDNSeptember 4

    The problem: I understand how to regularly enqueue jQuery UI in functions.php. My problem is that I'm using a selectmenu widget that doesn't come with WordPress by default: http://wiki.jqueryui.com/w/page/12138056/Selectmenu ...and it only works if I

  • make environmental variable equal the contents of a fileJuly 30

    I am working on linux and I want to set an environmental variable to the contents of a file: vi file: the env var should equal everything in here Now the file has a lot of " and other special characters and it will a take a lot of time to escape them

  • Unable to execute gcc: No such file or directoryAugust 5

    I am running Ubuntu LTS and I'm getting errors when trying to do some of my pip install stuff, like scrapy for example. This is the error I get: Unable to execute gcc: No such file or directory Error: command 'gcc' failed with exit status 1 I'm insta

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.732 s.