外部添加一个函数到WordPress插件's PHP文件(允许简单的版本更新)


Externally add a function to a WordPress Plugin's PHP file (Allowing for easy version update)

我脑子里有一件很棘手的事,我已经纠结了好几天了。

我正在为某人创建一个WordPress网站(在http://www.lydiala.com/),并使用简单WordPress Paypal购物车插件

我想在我的header.php文件的右上角显示购物车中的商品总数。

我已经找出了一个函数,成功返回这个数字,并已将其添加到插件的主PHP文件,在/wp-content/plugins/wordpress-simple-paypal-shopping-cart/wp_shopping_cart.php(与所有其他函数一起)。

我在wp_shopping_cart.php:

中使用了以下EXISTING函数
function simple_cart_total() {  
    $grand_total = 0;   
    foreach ((array)$_SESSION['simpleCart'] as $item){      
        $total += $item['price'] * $item['quantity'];       
        $item_total_shipping += $item['shipping'] * $item['quantity'];  
    }   
    $grand_total = $total + $item_total_shipping;
    return number_format($grand_total,2); 
}

我复制了它,重新命名并更改如下:

function total_items() {    
    $grand_total = 0;   
    foreach ((array)$_SESSION['simpleCart'] as $item){      
         $total += $item['quantity'];   
    }
$grand_total = $total;  
    return number_format($grand_total); 
}

现在,在我的header.php我有:

echo total_items();

效果非常好!

但是,当将来插件更新时,当wp_shopping_cart.php被新版本覆盖时,此新添加的函数将丢失。

是否有办法将我的自定义函数附加到这个外部.php文件?我已经尝试了一些方法,但大多数都不起作用,因为它们试图加载wp_shopping_cart.php,其中已经被加载("不能重新声明"等)。

如果情况变得更糟,我总是可以告诉客户在这个插件可用的更新时打电话给我。

我建议把它添加到其他插件中。

paypall-total .php在您的wp-content/plugins(不要忘记激活在wp-admin)

/*
plugin-name: paypall totals
description: adds a function to show paypall totals
pluginURI: http://stackoverflow.com/q/17066291/933065
*/
function paypall_total_items() { 
  session_start();   
  $grand_total = 0;   
  foreach ((array)$_SESSION['simpleCart'] as $item){      
    $total += $item['quantity'];   
  }
  $grand_total = $total;  
  return number_format($grand_total); 
}

因为它只得到它的数据出$_SESSION它不重要你把这个函数放在哪里。它也会工作在你的主题functions.php

同时调整header.php: echo paypall_total_items();

中的代码

把paypal插件恢复到原来的代码。

对不起,我现在无法评论,应该分成一个临时回答。
我认为你不应该写一个插件,你应该把这些代码放在你的主题的function.php中。
并检查插件"WordPress Simple Paypal Shopping Cart plugin ."是否已安装以下代码:

if (function_exists ("simple_cart_total")) {...}