Check if home.php and set home.php as page_on_front

I want to include a custom inline js on home page only.
I havent set home page from reading section.
I am using home.php file.

function print_my_inline_script() {    
   if (is_home() && wp_script_is( 'jquery', 'done' ) ) {
      ?>
      <script type="text/javascript">
        jQuery(document).ready(function($){ 
            $('.carousel').carousel();
            $('.item').hover(function(e){
                $(this).find('.summary').stop().fadeTo('fast',1); 
            },function(){
                $(this).find('.summary').stop().fadeOut('fast'); 
            });
        });
      </script>
      <?php
  }
}
add_action( 'wp_footer', 'print_my_inline_script' ); 

This doesnt work. is_front_page() doesnt work either.

Read More

I have already done <?php wp_reset_query(); ?> after loop.

I have one more question. I know home.page overrides index.php and works as home page.
But I dont want my users to get confused when they change options in reading section.

I have found this,

update_option( 'page_on_front', $post->ID );
update_option( 'show_on_front', 'page' );

but it needs an ID and I dont have any page, so I dont have any id.

So, I need a way to set check if user is in home.php (home page) and since using home.php overrides the reading section options, any workarounds for that ?

Related posts

Leave a Reply

3 comments

  1. First issue:

    I want to include a custom inline js on home page only. I havent set home page from reading section. I am using home.php file.

    I will assume that by home page, you actually mean site front page. In that case, you don’t really care about is_home() or home.php, as these apply to the blog posts index, regardless of whether you are on the site front page or some other page.

    See also:

    I have one more question. I know home.page overrides index.php and
    works as home page. But I dont want my users to get confused when they
    change options in reading section.

    Again, the confusion here regards home page as site front page versus blog posts index. Refer to the Template Hierarchy for site front page display for further explanation.

    User reading settings will have no impact, if you choose the correct conditional with which to enqueue your script.

    Second issue:

    This conditional in your callback:

    if ( is_home() && wp_script_is( 'jquery', 'done' ) )
    
    1. is_home(): You need to use is_front_page(), if you intend to target the site front page
    2. wp_script_is( 'jquery', 'done' ): this seems to be unnecessary; you merely need to register your script with jquery as a dependency

    Third issue:

    I have found this,

    update_option( 'page_on_front', $post->ID );
    update_option( 'show_on_front', 'page' );
    

    but it needs an ID and I dont have any page, so I dont have any id.

    So, I need a way to set check if user is in home.php (home page) and since using home.php overrides the reading section options, any workarounds for that ?

    This is, essentially, _doing_it_wrong(). You should have no need to override user settings. Just pick the right conditionals:

    1. To target the site front page, use is_front_page()
    2. To ensure that the site front page is displaying a static page, use 'page' == get_option( 'show_on_front' ).
    3. If the front page is set to display a static page, then the user will have created and assigned a static page to use. To determine the ID for that page, use get_option( 'page_on_front' ).

    Putting it all together

    1. Add your script to a custom.js file in the Theme directory somewhere.
    2. Add a callback hooked into wp_enqueue_scripts():

      add_action( 'wp_enqueue_scripts', 'wpse58196_enqueue_custom_script' );`
      
    3. Inside the callback, enqueue your custom script based on the correct conditionals:

      function wpse58196_enqueue_custom_script() {
          // If we're on the site front page, and the front page displays a static page
          if ( is_front_page() && 'page' == get_option( 'show_on_front' ) ) {
              // Enqueue custom script, with jQuery as a dependency, and output in the footer
              wp_enqueue_script(
                  // handle
                  'custom-carousel',
                  // path
                  get_template_directory() . '/custom.js',
                  // dependency array
                  array( 'jquery' ),
                  // version
                  '1.0',
                  // in footer?
                  true
              );
          }
      }
      
  2. Your issue is more likely related to wp_script_is().

    Try this instead

    function aa_home_scripts() {
        if(is_home()){
            wp_enqueue_script(
                'app-home',
                get_template_directory_uri() . '/js/app.home.js',
                array('jquery'), // declare jQuery as dependancy
               '1',
               true // flag to add to footer
           );
        }
    }
    add_action('wp_enqueue_scripts', 'aa_home_scripts');