-1

I am developing a wordpress theme and now I am working on the theme options page. I added an option with "select" method to give the user the option to change the slider's fx. Now... this is the output of a text input that sets the fx duration <?php echo get_option('wis_fx_speed'); ?> and works fine! My problem is that <?php echo $fxSample ?> don't work. Tried everything I knew and imagined (syntax, order, put the switch inside the jquery script etc) , searched the web but found nothing... Could you help me? Thanks in advance!

<?php 

      switch (get_option('wis_fx_slider')) {
      case "opacity":
      $fxSample = "opacity : 'toggle',"; 
      break;
      case "width":
      $fxSample = "width : 'toggle',"; 
      break;
      case "opawidth":
      $fxSample = "opacity : 'toggle', width : 'toggle',"; 
      break;
      case "blink":
      $fxSample = "opacity : 'show',"; 
      break;
      }

?>


<script type="text/javascript">
    jQuery(document).ready(function($){
        $("#photo-rotator").tabs({fx:{<?php echo $fxSample ?>
        duration: <?php echo get_option('wis_fx_speed'); ?> }}).tabs("rotate", 2000);
    });
</script>

OUTPUT:

<

script type="text/javascript">
    jQuery(document).ready(function($){
        $("#photo-rotator").tabs({fx:{        duration: 3000 }}).tabs("rotate", 2000);
    });
</script>

EDIT Works if I make it with radio.

9
  • 1
    It looks like you are missing a comma after $fxSample ?> Also, make sure your duration is a number and not a string. Commented Jun 11, 2012 at 20:39
  • 1
    What is the resulting output? Any javascript errors showing in the browser error console? Commented Jun 11, 2012 at 20:40
  • 2
    Kevin I am not missing a comma after $fxSample ?>... the comma is in $fxSample = "opacity : 'toggle'**,**"; ! Commented Jun 11, 2012 at 20:41
  • 2
    Well it looks like it is empty. Set a default in your switch statement and see if that turns up. If your default value is printed I suggested checking the output of get_option('wis_fx_slider') to confirm it is as expected. Commented Jun 11, 2012 at 20:42
  • I believe you are missing a semi colon at the end of echo. Commented Jun 11, 2012 at 20:43

4 Answers 4

1

When I need to pass some data to JavaScript, I find it helpful to build a PHP array first, then use json_encode to convert it to JavaScript. This avoids a lot of potential issues:

<?php 
  $tab_options = array(
    'duration' => get_option('wis_fx_speed'),
    'fx' => array()
  );
  switch (get_option('wis_fx_slider')) {
    case 'opacity':
      $tab_options['fx']['opacity'] = 'toggle';
      break;
    case 'width':
      $tab_options['fx']['width'] = 'toggle';
      break;
    case 'opawidth':
      $tab_options['fx']['opacity'] = 'toggle';
      $tab_options['fx']['width'] = 'toggle';
      break;
    case 'blink':
      $tab_options['fx']['opacity'] = 'show';
      break;
  }
?>

<script type="text/javascript">
  var tabOptions = <?php echo json_encode($tab_options); ?>;
  jQuery(document).ready(function($) {
    $('#photo-rotator').tabs(tabOptions).tabs('rotate', 2000);
  });
</script>

Benefits:

  • Much cleaner code (easier to read).
  • You don't have to worry about getting the commas, quotes, and other syntax right for all the possible cases. For example, as other people have mentioned, you don't have a default case. This approach will at least generate valid JavaScript if an option value is invalid.
Sign up to request clarification or add additional context in comments.

2 Comments

You'll have to be more specific than "didn't work". Post the generated HTML and JavaScript. Find out whether your code is generating PHP errors. Try to actually figure out what's going on instead of expecting us to do all of your coding and debugging for you.
Updated the post - included javascript output
0

To safely output any PHP value into a JavaScript value, use json_encode.

...{fx:<?php echo json_encode($fxSample); ?>, duration: <?php echo json_encode(get_option("wis_fx_speed")); ?>}...

Note also that you are missing a , and have an extra } in your code. Always look at the generated result to look for syntax errors.

1 Comment

thanks for your answer but the piece of code you provided breaks the whole slider.
0

You need a semicolon after each php line

...
$("#photo-rotator").tabs({fx:{<?php echo $fxSample; ?>
...
  • What about a default case value?

  • Try to debug doing a

    var_dump(get_option('wis_fx_slider'));
    var_dump($fxSample);
    
  • Maybe is it a scope problem? Try defining

    $fxSample = "";
    

    at the beggining, before the switch

3 Comments

you are right, I was missing semicolon, added it now but nothing changed.
What do you mean "no results"? It has to be doing something or you are doing it really, really wrong.
relly wrong means? I show you my code... this code is for the select part tha I am trying to make it work
0

Is get_option('wis_fx_slider') returning one of the options in the switch case?

You haven't sepcified a "default" so $fxSample will go undefined.

<?php 

switch (get_option('wis_fx_slider')) {
    case "opacity":
        $fxSample = "opacity : 'toggle',"; 
        break;
    case "width":
        $fxSample = "width : 'toggle',"; 
        break;
    case "opawidth":
        $fxSample = "opacity : 'toggle', width : 'toggle',"; 
        break;
    default: // blink or anything else
        $fxSample = "opacity : 'show',";
}

?>

Is your duration a string or number? If it's a string you need quotes like

duration: '<?php echo get_option('wis_fx_speed'); ?>' }}).tabs("rotate", 2000);

What happens when you view source? Is the PHP laying out the dynamically generated javascript correctly? Can you post the output?

Also, formatting

<?php 

switch (get_option('wis_fx_slider')) {
    case "opacity":
        $fxSample = "opacity : 'toggle',"; 
        break;
    case "width":
        $fxSample = "width : 'toggle',"; 
        break;
    case "opawidth":
        $fxSample = "opacity : 'toggle', width : 'toggle',"; 
        break;
    case "blink":
        $fxSample = "opacity : 'show',"; 
        break;
}

?>


<script type="text/javascript">
    jQuery(document).ready(function($){
        $("#photo-rotator").tabs({
            fx: {
                <?php echo $fxSample ?>
                duration: <?php echo get_option('wis_fx_speed'); ?>
            }
        }).tabs("rotate", 2000);
    });
</script>

3 Comments

stevether wis_fx_speed works fine, $fxSample is the one that don't work
Post what you see when you view source
You need to post it so we can help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.