2017-05-29 104 views
2

我想替换核心WP函数wp_delete附件。在我的functions.php文件中,我添加了以下代码:替换核心WordPress功能

add_action('init', 'remove_my_action'); 

function remove_my_action(){ 
    remove_action('delete_attachment', 'wp_delete_attachment'); 
    add_filter('delete_attachment','wp_delete_attachment',10,2); 
} 

And then the copy of the replaced function goes here with edited code. 

这不起作用。我收到错误:无法重新声明wp_delete_attachment()。我已经尝试了很多其他方法,但无法使其正常工作。

底线是我需要添加一些代码到wp_delete_attachment函数的中间。如果我可以以某种方式用我的版本替换该函数,或者将代码添加到现有函数中而不编辑wp-includes/post.php文件中的实际代码(以便版本更新不会覆盖我的代码),我会感到满意与任一。如何才能做到这一点?我通过问题找到的所有选项都没有解决问题。谢谢!!

回答

1

您需要将您的副本wp_delete_attachment命名为唯一的名称。也许命名空间与您的网站名称,如function my_site_wp_delete_attachment()

此外,我相信你需要使用add_action而不是add_filter

remove_action('delete_attachment', 'wp_delete_attachment'); 
add_action('delete_attachment', 'my_site_wp_delete_attachment'); 
+0

谢谢!!这让我对我的解决方案! –

+0

我接受了这个答案,因为它解决了'redeclare'问题,但似乎要替换的原始函数仍在运行。 –