Use Post ID in functions.php

I have the following code in my functions.php. It works great: when a booking is made, it emails me. Simple. However, I&

I have the following code in my functions.php. It works great: when a booking is made, it emails me. Simple.

However, I'd like it to send me the post ID for that booking rather than just a 'Hello World!' message. Help?

function add_to_system() {

  // get post meta
  // $email = get_post_meta('',$post_id)
  // $post_id = get_queried_object_id();
  mail('[email protected]', $post_id.' Hello', 'World!');

}

add_action('booked_new_appointment_created', 'add_to_system', 99);

Thanks for any guidance.

I have the following code in my functions.php. It works great: when a booking is made, it emails me. Simple.

However, I'd like it to send me the post ID for that booking rather than just a 'Hello World!' message. Help?

function add_to_system() {

  // get post meta
  // $email = get_post_meta('',$post_id)
  // $post_id = get_queried_object_id();
  mail('[email protected]', $post_id.' Hello', 'World!');

}

add_action('booked_new_appointment_created', 'add_to_system', 99);

Thanks for any guidance.

Share Improve this question asked Jul 2, 2019 at 7:10 michaelmcgurkmichaelmcgurk 1134 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

Only posting an alternative answer despite the question being about a 3rd-party plugin, whose source code isn't even freely available, because I don't believe the accepted answer is likely to be a good solution.

It's not documented on their website, but a comment from the developer states:

These would all be considered custom requests, which our free support doesn’t handle. What’d you want to do is probably hook into the “booked_new_appointment_created” action which comes with the Appointment ID (which you can use to pull down meta fields, etc.) — That’s all the direction I can really offer you regarding these custom coding questions.

Which implies that callback functions for the booked_new_appointment_created hook receive the appointment ID as an argument:

function add_to_system( $appointment_id ) {
    mail( '[email protected]', 'My subject', $appointment_id );
}
add_action( 'booked_new_appointment_created', 'add_to_system', 99, 1 );

Doing it this way is far more reliable, as you can be certain that the ID of the appointment you're using is the ID of the appointment that triggered the action, and not some other post or page that might be the 'current' post in the global state.

This is a good rule of thumb for all hooks: Use the arguments passed to callbacks wherever possible instead of global variables.

Use get_the_id rather than global $post;

get_the_ID()

Update Like this

function add_to_system() {
   mail("[email protected]","My subject",get_the_ID());
}
add_action('booked_new_appointment_created', 'add_to_system', 99);

try with this :

function add_to_system($appointment_id) {
   mail("[email protected]","My subject",$appointment_id);
}
add_action('booked_new_appointment_created', 'add_to_system', 99,1);

let me know if it works for you!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745345924a4623541.html

相关推荐

  • Use Post ID in functions.php

    I have the following code in my functions.php. It works great: when a booking is made, it emails me. Simple. However, I&

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信