Functions.php file breaking site every time I edit

I tried to add the following code to the bottom of the functions.php file for my Child Theme and then the site broke. Now I get a 500 Error when I try and access the live site or wp-admin.

add_filter('add_to_cart_redirect', 'themeprefix_add_to_cart_redirect');
function themeprefix_add_to_cart_redirect() {
 global $woocommerce;
 $checkout_url = $woocommerce->cart->get_checkout_url();
 return $checkout_url;
}

I tried copying and pasting the default template for the functions.php Child Theme file and that still did not fix the site. I also tried clearing my browser cache.

I am going to restore my site to a previous back up but how do I prevent this from happening again in the future.

Thanks in advance!

Hi,

I tried adding that code in my test site and I didn’t get a 500 Error.

There could be a syntax error in your child theme’s functions.php

You need to add the code after this line

// Additional Functions
// =============================================================================

Here is the code in my functions.php file for Child Theme:

<?php

// =============================================================================
// FUNCTIONS.PHP
// -----------------------------------------------------------------------------
// Overwrite or add your own custom functions to Pro in this file.
// =============================================================================

// =============================================================================
// TABLE OF CONTENTS
// -----------------------------------------------------------------------------
//   01. Enqueue Parent Stylesheet
//   02. Additional Functions
// =============================================================================

// Enqueue Parent Stylesheet
// =============================================================================

add_filter( 'x_enqueue_parent_stylesheet', '__return_true' );



// Additional Functions
// =============================================================================

function add_google_code(){ ?>
   
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
  (adsbygoogle = window.adsbygoogle || []).push({
    google_ad_client: "ca-pub-9139397624332531",
    enable_page_level_ads: true
  });
</script>  

<?php }
add_action('wp_head', 'add_google_code');`Preformatted text`

Hi,

You can add the code like this.

<?php

// =============================================================================
// FUNCTIONS.PHP
// -----------------------------------------------------------------------------
// Overwrite or add your own custom functions to Pro in this file.
// =============================================================================

// =============================================================================
// TABLE OF CONTENTS
// -----------------------------------------------------------------------------
//   01. Enqueue Parent Stylesheet
//   02. Additional Functions
// =============================================================================

// Enqueue Parent Stylesheet
// =============================================================================

add_filter( 'x_enqueue_parent_stylesheet', '__return_true' );



// Additional Functions
// =============================================================================

function add_google_code(){ ?>
   
    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({
    google_ad_client: "ca-pub-9139397624332531",
    enable_page_level_ads: true
    });
    </script>  

<?php }
add_action('wp_head', 'add_google_code');


add_filter('add_to_cart_redirect', 'themeprefix_add_to_cart_redirect');
function themeprefix_add_to_cart_redirect() {
    global $woocommerce;
    $checkout_url = $woocommerce->cart->get_checkout_url();
    return $checkout_url;
}

Hope that helps.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.