0

Is it possible to override a function in a theme, from a plugin? I've been struggling to find an example of how I would achieve the above.

If anyone could shed any light it would be really helpful.

edit: Adding some code

So this is my function which allows the cart in woocommerce to update using ajax:

add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cartplus_fragment', '1');

function woocommerce_header_add_to_cartplus_fragment( $fragments ) {
    global $woocommerce;
    $basket_icon = esc_attr($instance['basket_icon']);

    ob_start();

    ?>
    <a class="cartplus-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" onclick="javscript: return false;" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
    <?php

    $fragments['a.cartplus-contents'] = ob_get_clean();

    return $fragments;

}

However, some themes also have a similar function built in - in these themes my code stops working which seems strange as I use the above code twice in my plugin (one is a variation of the above) and they work fine together. This is the code built in to the theme:

add_filter('add_to_cart_fragments', 'woocommerce_cart_link');

function woocommerce_cart_link() {
    global $woocommerce;

    ob_start();

    ?>
    <a href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> <?php _e('in your shopping cart', 'woothemes'); ?>" class="cart-button ">
    <span class="label"><?php _e('My Basket:', 'woothemes'); ?></span>
    <?php echo $woocommerce->cart->get_cart_total();  ?>
    <span class="items"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count); ?></span>
    </a>
    <?php

    $fragments['a.cart-button'] = ob_get_clean();

    return $fragments;

}
user319940
  • 3,267
  • 8
  • 38
  • 53

1 Answers1

1

Unless the function is meant to be overridden, no. This is basic PHP. You can't redefine a function. If you try you will get a fatal error.

Parts of WordPress are written to be overwritten. Look at /wp-includes/pluggable.php. Every function in there is wrapped in a if( !function_exists(...) ) conditional. Unless your theme did the same, and some do for some functions, you can't overwrite.

Look around for filters that might help you instead.

Looking at your code, you should be able to unhook that. Just make sure to hook the unhook late enough. That is not a good solution, though since you are breaking theme functionality and also must know the know the names of all the hooked functions that themes are using.

Is there something in $fragments, or in $_POST or $_GET or anything else, that you can use to conditionally run your code, leaving the rest alone.

s_ha_dum
  • 2,840
  • 2
  • 18
  • 23
  • This is as I thought - I've tried removing the offending filter but it seems to have no affect :S. – user319940 Dec 23 '12 at 15:58
  • Edit your question with the relevant code and I might be able to help more. – s_ha_dum Dec 23 '12 at 16:03
  • Do you mean like: remove_filter('add_to_cart_fragments', 'woocommerce_cart_link'); remove_action('add_to_cart_fragments','woocommerce_cart_link'); - doesn't seem to work, it's a bit of a workaround for now but at the moment it's ok if it breaks that functionality, as what I am building replaces it. What would be better is if I could work out why it breaks my code, I can't see a logical reason for it. – user319940 Dec 23 '12 at 16:39
  • `remove_filter('add_to_cart_fragments', 'woocommerce_cart_link');` will work if you run it in the right place. If it isn't working I'd guess that you are unhooking it ***before*** the function is hooked in the first place, which won't work. The `remove_filter` docs say that the `$priority` parameter has to match for the filter you are removing. Or what you think is the problem isn't. – s_ha_dum Dec 23 '12 at 16:48
  • There is no priority param so I think my code is correct - I've tried unhooking from wp_footer and it still doesn't work. The WooCommerce docs really seem to suck - lots of holes. – user319940 Dec 23 '12 at 17:23