WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to add php code within jquery. I know that the file then cannot be saved as a js file but must be saved as a php file to execute the php code. If we save it as a php file then it can also not be enqueued within function.php.

jQuery(document).ready(function() {
jQuery('#a, #b').change(function () {
    var $this = jQuery(this), // Get a handle on the checkbox we just clicked.
        $a = jQuery('#a'),
        $b = jQuery('#b'),
        $c = jQuery('#c'),    // Get a handle on the textbox.
        $d = jQuery('#d'),    // Get a handle on the textbox.
        $e = jQuery('#e'),    // Get a handle on the textbox.
        $f = jQuery('#f'),    // Get a handle on one of our default values.
        $g = jQuery('#g');    // Get a handle on one of our default values.

if ($this.attr('id') == 'a' && $this.is(':checked')) {
   // Clicking checkbox a will add the content of c and f and place it in e
   // It will also uncheck checkbox b.

   $e.val( <?php echo stripslashes(get_option('m')); ?> );
} else if ($this.attr('id') == 'b' && $this.is(':checked')) {
   // Clicking checkbox b will add the content of d and g and place it in e
   // It will also uncheck checkbox a.

   $e.val( <?php echo stripslashes(get_option('m')); ?> );
} else {

So how can I add php within my jquery and integrate it within my function.php theme file. Also please be noted that the php echo will return a javascript code so will it be possible that this code will be just added as a text and not echo as an executed code. Please help.

share|improve this question

I see no reason for you to use PHP in your JavaScript file. Enqueue your script properly using the wp_enqueue_scripts hook and then use the wp_localize_script()function to pass your data to your JavaScript code.

function wpa67325_enqueue_scripts() {
    wp_enqueue_script( 'some_handle', 'filename.js', false );
    $translation_array = array( 'some_string' => __( 'Some string to translate' ), 'a_value' => '10' );
    wp_localize_script( 'some_handle', 'object_name', $translation_array );

add_action( 'wp_enqueue_scripts', 'wpa67325_enqueue_scripts', 10 );
share|improve this answer
Thanks Joseph for your help. Actually I have recently read an article about wp_localize_script() function and I didn't know anything about it before that. So I am completely novice to it. My actual requirement is this question stackoverflow.com/questions/12751766/… . Please read my comment below that question for clarification. That's what I really want to accomplish. Please if you can answer it, I'll be very thankful to you. – user1666698 Oct 6 '12 at 16:11

You can easily save your file as PHP file and enqueue it with wp_enqueue_script function without any problem.
There is nothing wrong in serving JS in a PHP file.

share|improve this answer
Thanks Hamed for your quick response. The point I am worry about is that the php code that is., <?php echo stripslashes(get_option('m')); ?> is going to give javascript code like that <div>My code</div> <script type="text/javascript"> ... some jquery code....</script> so will it be possible that it'll be just returned as a text and not an executed code when put into the textbox with the id e. – user1666698 Oct 6 '12 at 7:49
Ahhhh... I'm not sure I understand what you said. The whole point is that using php in javascript is a common practice. When you include javascript file using <script src=... the content is considered javascript code and therefor treated as such. – Hamed Momeni Oct 6 '12 at 7:54
So do you have any idea how I can accomplish this. Your help will be highly appreciated. – user1666698 Oct 6 '12 at 8:00
More explanation: What you need to do is just save your code as a php file and then use it as you would use a simple js file. I don't know how to say it more clearly. – Hamed Momeni Oct 6 '12 at 8:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.