ThemeShaper Forums » Thematic

[closed]

How to create global javascript variable in php file

(4 posts)
  • Started 7 years ago by Civil_777
  • Latest reply from Civil_777
  • This topic is not resolved
  1. Civil_777
    Member

    Hi guys. Newbie here. I am editing a theme, and I want to create a global Javascript variable. I would like to construct the global variable using the php function bloginfo(), as follows.

    <script type="text/javascript">
    contact_form = <?php echo bloginfo('stylesheet_directory') . '/contact_form.php' ?>;
    </script>

    Question is: Which php file should I use to define this global variable? I want to use header.php, but that file is not defined in my child theme. It is only defined in the parent theme. I do not want to edit my parent theme in case it needs to be updated.

    Posted 7 years ago #
  2. You want to use the childtheme/functions.php file.

    To insert things in the header, use.

    // add to wp_head
    function snix_addto_header() { ?>
    <script>contact_form = <?php echo bloginfo('stylesheet_directory') . '/contact_form.php' ?>;</script>
    <?php }
    add_action('wp_head', 'snix_addto_header');

    Although it is considered best practice to insert anything that isn't "required to be in the header" into the footer just above the body tag. Doing so won't necessarily speed up your site, but it allows the layout to render before the script loads.

    // add to wp_footer
    function snix_addto_footer() { ?>
    <script>contact_form = <?php echo bloginfo('stylesheet_directory') . '/contact_form.php' ?>;</script>
    <?php }
    add_action('wp_footer', 'snix_addto_footer');

    Not sure what the contact_form = is, but this method of loading things isn't always the best way, make sure not to use this for everything, the wp_enqueue is a much better method.

    Posted 7 years ago #
  3. Here is an example of using wp_enqueue.

    // script manager template for deregestering and registering files.
    function snix_script_manager() {
    
    // wp_enqueue_script template ( $handle, $src, $deps, $ver, $in_footer );
    
    // deregister standard jquery
        wp_deregister_script('jquery');
    // loads modernizr-js script, local path, no dependency, no version, loads in header
    	wp_enqueue_script('modernizr-js', get_bloginfo('stylesheet_directory') . '/js/modernizr.js');
    // loads jqery script, from google cdn url, no dependency, yes version, loads in footer
        wp_enqueue_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"), false, '1.6.4', true);
    // loads custom-js script, local path, dependency is jquery, no version, loads in footer
    	wp_enqueue_script('custom-js', get_bloginfo('stylesheet_directory') . '/js/custom.js', array('jquery'), false, true);
    
    // page specific loading, no need to load everything on every page
    // if it is (! - not) contact page, do not load contact-form-7 scripts
    	if ( !is_page('contact') ) {
        	wp_deregister_script('contact-form-7');
         }
    
    }
    add_action('wp_enqueue_scripts', 'snix_script_manager');

    At the bottom I am loading only my contact form on my contact page, no need to pull in a script on every page if it is unneeded, check Devin's site for more info on optimizing WP scripts.

    Posted 7 years ago #
  4. Civil_777
    Member

    Inserting it into the header worked! Thanks very much for your help. I will try some of the optimizations you suggested.

    Posted 7 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.