Is it bad practice to use PHP Print to show jQuery Function?

Need to show a certain piece of jQuery in the header of a wordpress site for settings of a plugin. Is it bad practice to use this the php print method to accomplish it? I’m using the following code:

<?php is_page( 'front-page' ); print 
"<script>
jQuery(function() {

        jQuery('#bannerscollection_kenburns_majestic').bannerscollection_kenburns({
            skin: 'majestic',
            width: 2530,
            height: 1200,
            autoHideNavArrows:true,
            numberOfThumbsPerScreen:16,
            thumbsOnMarginTop:23,
            thumbsWrapperMarginTop: 0,
            initialZoom: 1,
            finalZoom: 1,       
        });     


    });
</script>"
?>

EDIT

Read More

I ended up using this, which works great.

<?php is_page( 'front-page' ); wp_enqueue_script( 'kbsettings' ); ?>

Related posts

Leave a Reply

3 comments

  1. YOu can try like this

    <?php if(is_page( 'front-page' )){ ?>
    <script>
    jQuery(function() {
    
            jQuery('#bannerscollection_kenburns_majestic').bannerscollection_kenburns({
                skin: 'majestic',
                width: 2530,
                height: 1200,
                autoHideNavArrows:true,
                numberOfThumbsPerScreen:16,
                thumbsOnMarginTop:23,
                thumbsWrapperMarginTop: 0,
                initialZoom: 1,
                finalZoom: 1,       
            });     
    
    
        });
    </script>
    
    
    
    <?php } ?>
    
  2. Generally yes, it is bad practice to generate static content in dynamic script, it affects service performance (standalone js cached in user’s browser and php interpreter have to process your lines again and again)

    use something like that :

    <?php
    echo is_page( 'front-page' ) 
            ? "<script type='text/javascript' src='js/myplugin.js'></script>'" 
            : ""; 
    ?>
    

    Also, may be you install your plugin in wordpress template of front page ?

  3. You shouldn’t really be printing that many lines. As mentioned, you could use a HEREDOC? Usually, JS in PHP is really only if you need PHP variables in your JS. I don’t see that here?

    Why can’t you just load that script instead of printing it?

    <?php if is_front_page(){ //the general best practice way of getting the front page
        include(YOURPATH.'js/yourfile.js');
    ?>