WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 implemented in my worpdress theme ios button switch and when someone click the button the script load a php file with the text: on or off

Here is the code:

  <script type="text/javascript">

$('#1').iphoneSwitch("<?php echo get_user_meta( $current_user->ID, '_fbpost_status', true); ?>", 
 function() {
   $('#ajax').load('<?php bloginfo( 'template_url' ); ?>/includes/ajaxswitch/on.php');
  function() {
   $('#ajax').load('<?php bloginfo( 'template_url' ); ?>/includes/ajaxswitch/off.php');
    switch_on_container_path: '<?php bloginfo( 'template_url' ); ?>/includes/ajaxswitch/iphone_switch_container_off.png'

So on.php have only the "Activated" text and of.php "Stoped" text. The on and off files have not included the wordpress variables. How can I include them? To make a update in db. Or do you know any better method for doing the update?

share|improve this question
Your question is not very clear but you should probably be using the AJAX API if you need WordPress variable and functions in those files. – s_ha_dum Apr 14 '13 at 21:43
Sorry for misunderstanding, so in my question I have the script from single.php file, so yesterday I found out that I can define javascript variables via wp functions and tried this function: add_action('wp_enqueue_scripts', 'my_frontend_data'); function my_frontend_data() { global $current_user; wp_localize_script('data', 'Data', array( 'userMeta' => get_user_meta($current_user->ID, '_fbpost_status', true), 'templateUrl' => get_template_directory_uri() )); } But I get error, Data is not defined in javascript. – Sebastian Corneliu Vîrlan Apr 15 '13 at 7:26
Edit your question with that additional information please. Formatting in the comments in very minimal making code hard to read. – s_ha_dum Apr 15 '13 at 13:19

Have a look at wp_localize_script(), which is exactly the right core function for your needs. A short explanation on the usage can be found in this answer to the question "Pass PHP variable to javascript".

share|improve this answer

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.