Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Syntax error in functions.php

  • 18-12-2021 9:19am
    #1
    Registered Users, Registered Users 2 Posts: 662 ✭✭✭


    Hi all,

    Looking for some advice or where I can find an answer to a problem.

    I've tried stackoverflow but keep getting told to ask a new question.

    I need to show reviews on products on my website. Due to a theme issue I need to add php code to my functions.php to achieve this. The problem is the file will not save and returns an error. However the reviews will show. The error is "Your PHP code changes were rolled back due to an error on line 120 of file wp-content/themes/oceanwp-child-theme-master/functions.php. Please fix and try saving again.

    syntax error, unexpected '<', expecting end of file"

    When I fix it by removing "<?php" the file saves but the reviews stop showing. Any ideas on what to do? Please treat me as a total noob!!



Comments

  • Registered Users, Registered Users 2 Posts: 6,286 ✭✭✭Talisman


    What is line 120 of the file? Can you post the lines before and after it also.



  • Registered Users, Registered Users 2 Posts: 662 ✭✭✭jamesbil


    Just went to get the code and it turns out that i must have left the page without saving and the entire file is now empty!

    Just contacted my hosting provider to roll back the website for me.



  • Registered Users, Registered Users 2 Posts: 662 ✭✭✭jamesbil


    This is the code beginning 120

    <?php 
    /**
     * Enable reviews for all WC Products.
     */
    add_action('admin_init', function() {
        $updated = 0;
        $query = new \WP_Query([
            'post_type' => 'product',
            'posts_per_page' => -1,
            'comment_status' => 'closed',
        ]);
        if($query->have_posts()) {
            while($query->have_posts()) {
                $query->the_post();
                if(wp_update_post([
                    'ID' => get_the_ID(),
                    'comment_status' => 'open',
                ])) {
                    $updated++;
                }
            }
            wp_reset_postdata();
        }
        add_action('admin_notices', function() use ($updated) {
            printf(
                '<div class="notice notice-info is-dismissible"><p>Enabled reviews for %d products.</p></div>',
                (int)$updated
            );
        });
    });
    

    this is line 113-118

    function add_contact_form() {
        global $product;
        if(!$product->is_in_stock( )) {
           echo do_shortcode('[wpforms id="3244"]');
        }
    }
    




  • Registered Users, Registered Users 2 Posts: 662 ✭✭✭jamesbil


    I know it looks like the issue is <?php but if i remove it the function does not work as per my initial post



  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    did you close the php with ?>


    can you post the entire file



  • Advertisement
  • Registered Users, Registered Users 2 Posts: 662 ✭✭✭jamesbil


    this is the entire file, the issue is from line 119 down


    <?php

    /**

     * Child theme functions

     *

     * When using a child theme (see http://codex.wordpress.org/Theme_Development

     * and http://codex.wordpress.org/Child_Themes), you can override certain

     * functions (those wrapped in a function_exists() call) by defining them first

     * in your child theme's functions.php file. The child theme's functions.php

     * file is included before the parent theme's file, so the child theme

     * functions would be used.

     *

     * Text Domain: oceanwp

     * @link http://codex.wordpress.org/Plugin_API

     *

     */


    /**

     * Load the parent style.css file

     *

     * @link http://codex.wordpress.org/Child_Themes

     */

    function oceanwp_child_enqueue_parent_style() {

    // Dynamically get version number of the parent stylesheet (lets browsers re-cache your stylesheet when you update your theme)

    $theme  = wp_get_theme( 'OceanWP' );

    $version = $theme->get( 'Version' );

    // Load the stylesheet

    wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array( 'oceanwp-style' ), $version );

    }


    function remove_image_zoom_support() {

      remove_theme_support( 'wc-product-gallery-zoom' );

    }

    add_action( 'wp', 'remove_image_zoom_support', 100 );


    add_filter( 'woocommerce_product_tabs', 'delete_tab', 98 );


    function delete_tab( $tabs ) {


      if ( ! have_comments() ) {

        unset( $tabs['reviews'] );

      }

      if ( isset( $tabs['description'] ) ) {      

        unset( $tabs['description'] );

      }


      return $tabs;

    }


    add_filter( 'ocean_woo_summary_elements_positioning', 'remove_ocean_woo_excerpt_position' );

    function remove_ocean_woo_excerpt_position($sections){

      $sections = array_diff($sections, array('excerpt'));

      return $sections;

    }

     add_action( 'woocommerce_before_main_content', 'show_woocommerce_breadcrumb', 1, 0 );

     function show_woocommerce_breadcrumb(){

       if( is_product_category() ) {

        add_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );

       }

     }


    remove_action('woocommerce_after_single_product_summary','woocommerce_output_product_data_tabs',10);

     

    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );


    add_filter( 'woocommerce_product_tabs', 'add_custom_product_tab', 10, 1 );

    function add_custom_product_tab( $tabs ) {

    /* adding Short description under Description tab */

      $custom_tab = array(

        'short_desription' => array(

          'title' => __( "Description", "woocommerce" ),

          'priority' => 12,

          'callback' => 'short_description_tab_content'

        )

      );

      return array_merge( $custom_tab, $tabs );

    }


    function short_description_tab_content() {

      global $post, $product;


      $short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt );


      if ( ! $short_description ) {

        return;

      }


      echo '<div class="woocommerce-product-details__short-description">' . $short_description . '</div>'; // WPCS: XSS ok.

    }


    add_action( 'ocean_after_single_product_price', 'woocommerce_output_product_data_tabs' );


    add_filter( 'woocommerce_show_page_title', 'display_ocean_woo_archive_title', 100 );

    function display_ocean_woo_archive_title(){

      if( is_product_category() ) {

        return true;

      }

    }

    add_filter('wf_pklist_add_custom_css','wt_pklist_add_custom_css_shippinglabel',10,2);

    function wt_pklist_add_custom_css_shippinglabel($custom_css,$template_type)

    {

    if($template_type=='shippinglabel')

    {

      

      $custom_css.='.wfte_weight{display:none !important;}.wtfe_barcode clearfix{display:none !important;}.wfte_invoice_number{display:none !important;}';

    }

    return $custom_css;

    }

    // Remove the additional information title

    add_filter( 'woocommerce_product_additional_information_heading', '__return_null' );

    add_action('woocommerce_single_product_summary', 'add_contact_form', 20);


    function add_contact_form() {

      global $product;

      if(!$product->is_in_stock( )) {

        echo do_shortcode('[wpforms id="3244"]');

      }

    }

    (this is line 119) <?php

    /**

     * Enable reviews for all WC Products.

     */

    add_action('admin_init', function() {

    $updated = 0;

    $query = new \WP_Query([

    'post_type' => 'product',

    'posts_per_page' => -1,

    'comment_status' => 'closed',

    ]);

    if($query->have_posts()) {

    while($query->have_posts()) {

    $query->the_post();

    if(wp_update_post([

    'ID' => get_the_ID(),

    'comment_status' => 'open',

    ])) {

    $updated++;

    }

    }

    wp_reset_postdata();

    }

    add_action('admin_notices', function() use ($updated) {

    printf(

    '<div class="notice notice-info is-dismissible"><p>Enabled reviews for %d products.</p></div>',

    (int)$updated

    );

    });

    }); 



  • Registered Users, Registered Users 2 Posts: 23,074 ✭✭✭✭Esel
    Not Your Ornery Onager


    See the above question, it might be important.

    Not your ornery onager



  • Posts: 25,611 ✭✭✭✭ [Deleted User]


    Looks like the PHP tag opened on line 119 is never closed.



  • Registered Users, Registered Users 2 Posts: 10,900 ✭✭✭✭28064212


    There should be no PHP tag on line 119, the tag from line 1 is still active. No close tag is required at the end of the file, and it's actually recommended not to have it.

    @jamesbil, where did you get the code? Was it from this site: https://chap.website/woocommerce-reviews-tab-not-showing/? Note the paragraph after the code

    After adding this code to your child theme’s functions.php you should visit your admin dashboard and see a notice informing you how many products were successfully updated. You can reload the page and the number should now be 0. After that you should remove or comment out the code, since it will keep outputting a notice and making a query on every admin page load.

    You're supposed to add the code (without the opening <?php tag), visit the admin dashboard, then remove the code. That code block is a run-once function that will enable the reviews on all old products. You should read (or re-read) the full page from that link

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users, Registered Users 2 Posts: 662 ✭✭✭jamesbil


    Yes thats where i got the code.

    Problem is the reviews don't display.

    The only way they will is if I do as per my first post

    "I need to add php code to my functions.php to achieve this. The problem is the file will not save and returns an error. However the reviews will show. The error is "Your PHP code changes were rolled back due to an error on line 120 of file wp-content/themes/oceanwp-child-theme-master/functions.php. Please fix and try saving again.

    syntax error, unexpected '<', expecting end of file"

    When I fix it by removing "<?php" the file saves but the reviews stop showing.

    When I visit the dashboard, I can see the products have been updated so I remove the code. Reviews still dont display on the product.

    eg here: https://jamesbillingsfurniture.com/shop/airer/



  • Advertisement
  • Registered Users, Registered Users 2 Posts: 23,074 ✭✭✭✭Esel
    Not Your Ornery Onager


    ^ Your site does not have an option to refuse non-essential cookies - just wondering if this is legal?

    Not your ornery onager



  • Registered Users, Registered Users 2 Posts: 662 ✭✭✭jamesbil




  • Registered Users, Registered Users 2 Posts: 662 ✭✭✭jamesbil


    Cookie policy up and running again



  • Registered Users, Registered Users 2 Posts: 10,900 ✭✭✭✭28064212


    The code from line 119 down isn't doing anything. Once the individual products have their reviews enabled, that code is completely unnecessary. You can confirm that any given product has their reviews enabled by going to the product admin page, as detailed in the section "Product metabox options" on the page you got the code from. The only purpose of the code is so that you don't have to go in and do that manually for every product. Your problem lies elsewhere.

    Also, I'm not really sure what your problem is - you keep saying that you can get the reviews to show if you do a particular thing. So do that. What's the issue?

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users, Registered Users 2 Posts: 662 ✭✭✭jamesbil


    It's a strange one.

    Reviews currently aren't showing on my site, there are reviews but they just don't display.

    If I add that full code, including the <?php at the beginning and click save file I get an error, "syntax error, unexpected '<', expecting end of file"

    But, reviews now show on the website. But there is still an error with the functions.php file.

    If I fix the error by removing the <?php, the file will save but the reviews stop showing.

    When I remove the code as per the instructions, reviews still do not show or display.

    Can't work it out at all..



  • Registered Users, Registered Users 2 Posts: 611 ✭✭✭brianwalshcork


    >If I add that full code, including the <?php at the beginning and click save file I get an error, "syntax error, unexpected '<', expecting end of file"But, reviews now show on the website. But there is still an error with the functions.php file.


    Your functions.php contains this function:

    function delete_tab( $tabs ) {

     if ( ! have_comments() ) {

      unset( $tabs['reviews'] );

     }

     if ( isset( $tabs['description'] ) ) {    

      unset( $tabs['description'] );

     }

     return $tabs;

    }



    so the reviews show when you have a syntax error in the php file since this function doesn't run.

    So to make your reviews show, either figure out where the value for have_comments() is coming from, or just comment out the line:

     unset( $tabs['reviews'] );


    (and remove the code that you added - it's intended to only run once as mentioned above)

    Post edited by brianwalshcork on


  • Registered Users, Registered Users 2 Posts: 662 ✭✭✭jamesbil


    Brilliant thanks so much!!

    That code was added to hide the review tab if there are no reviews. I figured it would look better if there was no review tab rather than it showing "Reviews (0)"

    But its hiding the tab when there are reviews too.



  • Registered Users, Registered Users 2 Posts: 662 ✭✭✭jamesbil


    Sorted it with this change

    add_filter( 'woocommerce_product_tabs', 'delete_tab', 98 );
    function delete_tab( $tabs ) {
    
        global $product;
        $id = $product->id;
    
        $args = array ('post_type' => 'product', 'post_id' => $id);    
        $comments = get_comments( $args );
    
        if(empty($comments)) {
            unset( $tabs['reviews'] );
    




Advertisement