1

I have this script in my footer.php and I like to add it to the footer.php by using wp_enqueue_scripts.

<script type="text/javascript">
jQuery(window).load(function(){

<?php
if ( is_home() ) {

    if ( option::get('featured_posts_show') == 'on' ) {

        ?>if ( jQuery('#slider li').length > 0 ) {
            jQuery('#slider').flexslider({
                controlNav: false,
                directionNav: false,
                animationLoop: true,
                animation: '<?php echo option::get('slideshow_effect') == 'Slide' ? 'slide' : 'fade'; ?>',
                useCSS: true,
                smoothHeight: false,
                touch: false,
                slideshow: <?php echo option::get('slideshow_auto') == 'on' ? 'true' : 'false'; ?>,
                <?php if ( option::get('slideshow_auto') == 'on' ) echo 'slideshowSpeed: ' . option::get('slideshow_speed') . ','; ?>
                pauseOnAction: true,
                animationSpeed: 600,
                start: function(slider){
                    jQuery('#slider_nav .item').click(function(){
                        var id = getPostIdClass(this);
                        if ( id <= 0 ) return;

                        var index = slider.slides.index( slider.slides.filter('.' + id) );

                        slider.direction = (index > slider.currentSlide) ? 'next' : 'prev';
                        slider.flexAnimate(index, slider.pauseOnAction);
                    });
                },
                before: function(slider){
                    var id = getPostIdClass( slider.slides.eq(slider.animatingTo) );
                    if ( id <= 0 ) return;

                    jQuery('#slider_nav .item').removeClass('current');
                    jQuery('#slider_nav .item.' + id).addClass('current');

                    if ( jQuery('#slider_nav .row').length > 1 ) {
                        var navSlider = jQuery('#slider_nav').data('flexslider'),
                            currPage = navSlider.slides.index( navSlider.slides.find('.item.' + id).parent('.row') );
                        navSlider.direction = (currPage > navSlider.currentSlide) ? 'next' : 'prev';
                        navSlider.flexAnimate(currPage, navSlider.pauseOnAction);
                    }
                }
            });

            jQuery('#slider_nav .item').wrapInChunks('<div class="row" />', 4);

            jQuery('#slider_nav').flexslider({
                selector: '.tiles > .row',
                direction: 'vertical',
                controlNav: true,
                directionNav: false,
                animationLoop: false,
                animation: 'slide',
                useCSS: true,
                smoothHeight: false,
                touch: false,
                slideshow: false,
                pauseOnAction: true,
                animationSpeed: 600
            });
        }<?php

    }

    if ( option::get('carousel_posts_show') == 'on' ) {

        ?>if ( jQuery('#carousel .item').length > 0 ) {
            jQuery('#carousel .item').wrapInChunks('<div class="row" />', 3);
            jQuery('#carousel .row').append('<div class="clear" />');

            if ( jQuery('#carousel .item').length > 3 ) {
                jQuery('#carousel_wrap').append('<div id="carousel_nav"><div class="prev">Prev</div><div class="next">Next</div></div>');

                jQuery('#carousel .row').each(function(){
                    var max = Math.max.apply(null, jQuery('h4.title',this).map(function(){return jQuery(this).height()}).get());
                    jQuery('h4.title', this).css('min-height', max);
                });
                jQuery('#carousel').height(jQuery('#carousel .row').height());

                jQuery('#carousel').serialScroll({
                    axis: 'y',
                    cycle: false,
                    constant: false,
                    force: true,
                    items: '.row',
                    prev: '#carousel_nav .prev',
                    next: '#carousel_nav .next',
                    onBefore: function(e,elem,$pane,$items,pos){
                        $pane.height(jQuery(elem).height());
                    }
                });
            }
        }<?php

    }

}
?>

});
</script>

I haven't managed yet to add it, because of the php part inside the script. I don't know how to separate the javascript from the php and add it the right way to the footer.php.

Anyone?

2 Answers 2

2

Remove your php code from script and put you script in one file suppose name custom-script.js

then add this js with wp_enqueue_script in your theme functions.php file

function my_scripts_method() {
    if ( is_home() ) {

       if ( option::get('featured_posts_show') == 'on' ) {
        wp_enqueue_script('custom-script',get_stylesheet_directory_uri() . '/js/custom_script.js',  array( 'jquery' ), '', true );
        }
    }
}

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
Sign up to request clarification or add additional context in comments.

3 Comments

I'm afraid that's not going to work. There are several php options in the code as well, like this: animation: '<?php echo option::get('slideshow_effect') == 'Slide' ? 'slide' : 'fade'; ?>', How to deal with those, because I need those options?
As per the mark told in his answer you can use wp_localize_script() like as follow $slider_option = array('slideshow_effect'=>$slide) // $slide is your option value for slider wp_localize_script( 'custom-script', 'slider_option', $slider_option ); after that you can use this varible in js as follows animation:slider_option.slideshow_effect
Thanks, I'm going to try this, but it make sense. I'll come back when I'm stuck again.
1

Further to Datta Parad's answer, use wp_localize_script() to get your dynamic values.

See the Codex (link above) and Pippin's article for guidance on use.

Comments

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.