最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Easy Digital Downloads - How to use hooks correctly?

programmeradmin3浏览0评论

I’m using Easy Digital Downloads for my Wordpress webshop. After someone buy a item it needs to add data to the SQL database. I got this working by adding PHP code to shortcode-receipt.php. This is working correct but when I reload the receipt via browser or mail the PHP code will fire again.

I figured out that there was a hook in EDD, but i'm not sure how to use them. I figured out that the function needs to be added to functions.php.

file: /wp-content/themes/twentytwenty/functions.php

function pw_edd_on_complete_purchase( $payment_id ) {
## write data to the database ##
}
add_action( 'edd_complete_purchase', 'pw_edd_on_complete_purchase' );

But in what file do i place

do_action( 'edd_complete_purchase', $payment_id ); ?

I’m using Easy Digital Downloads for my Wordpress webshop. After someone buy a item it needs to add data to the SQL database. I got this working by adding PHP code to shortcode-receipt.php. This is working correct but when I reload the receipt via browser or mail the PHP code will fire again.

I figured out that there was a hook in EDD, but i'm not sure how to use them. I figured out that the function needs to be added to functions.php.

file: /wp-content/themes/twentytwenty/functions.php

function pw_edd_on_complete_purchase( $payment_id ) {
## write data to the database ##
}
add_action( 'edd_complete_purchase', 'pw_edd_on_complete_purchase' );

But in what file do i place

do_action( 'edd_complete_purchase', $payment_id ); ?

Share Improve this question asked Jul 17, 2020 at 8:20 JackJack 1
Add a comment  | 

2 Answers 2

Reset to default 0

You don't put do_action() anywhere. The plugin runs that when that action occurs. You just need to hook into it with add_action(). The way hooks work is documented here.

If you're not sure when or how a plugin's own hooks, such as this one, are fired, then you'll need to contact the plugin developer, or consult its developer documentation.

Just to add on top of what @Jacob Peattie said: the do_action is already in the EDD code somewhere, you don't have to add that part. That's the point that you your new function will get called from. Wordpress takes care of wiring up the two things for you, as per the docs link in Jacob's post.

As you wrote in your post, you need to add the add_action to your functions.php, or it can go in a plugin.

发布评论

评论列表(0)

  1. 暂无评论