Adding functions to Onepress child theme functions.php causes errors
-
I downloaded and installed Onepress child theme, activated it, and copied the settings to it via Recommended Actions. With default functions.php, it works. But now I want to add a function that modifies the behavior of woomcommerce plugin, so my functions.php looks like this:
<?php /** * OnePress Child Theme Functions * */ function woocommerce_after_shop_loop_item_title_short_description() { global $product; if ( ! $product->post->post_excerpt ) return; ?> <div itemprop="description"> <?php echo apply_filters( 'woocommerce_short_description', $product->post->post_excerpt ) ?> </div> <?php } add_action('woocommerce_after_shop_loop_item_title', 'woocommerce_after_shop_loop_item_title_short_description', 5); /** * Enqueue child theme style */ add_action( 'wp_enqueue_scripts', 'onepress_child_enqueue_styles', 15 ); function onepress_child_enqueue_styles() { wp_enqueue_style( 'onepress-child-style', get_stylesheet_directory_uri() . '/style.css' ); } /** * Hook to add custom section after about section * * @see wp-content/themes/onepress/template-frontpage.php */ /* function add_my_custom_section(){ ?> <section id="my_section" class="my_section section-padding onepage-section"> <div class="container"> <div class="section-title-area"> <h5 class="section-subtitle"> My section subtitle</h5> <h2 class="section-title"> My section title</h2> </div> <div class="row"> <!-- Your section content here, you can use bootstrap 4 elements :) --> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.</p> </div> </div> </section> <?php } add_action( 'onepress_after_section_about', 'add_my_custom_section' ); */
When I re-upload it to my server, and try to open my site, I get the error:
Parse error: syntax error, unexpected ‘function’ (T_FUNCTION) in /home/mySite/domains/mySite.lt/public_html/wp-content/themes/onepress-child-master/functions.php on line 1
Which is really weird, because line 1 contains no code, just comments. If I delete the comments, I get different error:
Parse error: syntax error, unexpected ‘woocommerce_after_shop_loop_it’ (T_STRING) in /home/mySite/domains/mySite.lt/public_html/wp-content/themes/onepress-child-master/functions.php on line 1
It looks almost random. Weird thing is, if I add the woocommerce modifying function to the Onepress parent theme functions.php, it doesn’t throw any errors, and works as expected. But adding the same code to child theme instead throws errors.
What is happening here?
EDIT: I tracked down that parser does not like this part from woocommerce_after_shop_loop_item_title_short_description() function:
?> <div itemprop="description"> <?php echo apply_filters( 'woocommerce_short_description', $product->post->post_excerpt ) ?> </div> <?php
It’s almost as if closing and reopening php tag messes up something – but this works perfectly fine in the parent theme functions.php. I have no idea why.
- The topic ‘Adding functions to Onepress child theme functions.php causes errors’ is closed to new replies.