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

Use Post ID in functions.php

programmeradmin2浏览0评论

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!

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论