我想知道我应该从WordPress返回什么值save_post
动作功能。
此示例来自save_post documentation 显式和隐式返回均不带值:
function my_project_updated_send_email( $post_id ) {
// If this is just a revision, don\'t send the email.
if ( wp_is_post_revision( $post_id ) )
return;
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
$subject = \'A post has been updated\';
$message = "A post has been updated on your website:\\n\\n";
$message .= $post_title . ": " . $post_url;
// Send email to admin.
wp_mail( \'[email protected]\', $subject, $message );
}
add_action( \'save_post\', \'my_project_updated_send_email\' );
但是,此示例来自
add_meta_box 文档非常明确地返回了传入的$post\\u id(如果提前返回),但如果函数执行到最后,则不返回任何内容。
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
function myplugin_save_postdata( $post_id ) {
/*
* We need to verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST[\'myplugin_inner_custom_box_nonce\'] ) )
return $post_id;
$nonce = $_POST[\'myplugin_inner_custom_box_nonce\'];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce, \'myplugin_inner_custom_box\' ) )
return $post_id;
// If this is an autosave, our form has not been submitted, so we don\'t want to do anything.
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user\'s permissions.
if ( \'page\' == $_POST[\'post_type\'] ) {
if ( ! current_user_can( \'edit_page\', $post_id ) )
return $post_id;
} else {
if ( ! current_user_can( \'edit_post\', $post_id ) )
return $post_id;
}
/* OK, its safe for us to save the data now. */
// Sanitize user input.
$mydata = sanitize_text_field( $_POST[\'myplugin_new_field\'] );
// Update the meta field in the database.
update_post_meta( $post_id, \'_my_meta_value_key\', $mydata );
}
add_action( \'save_post\', \'myplugin_save_postdata\' );
那页上没有显示它为什么会回来
$post_id
如果它早点回来。
似乎没有任何关于save_post 抄本页的一种或另一种方式。我在网上也看到了相互矛盾的建议。
在中创建动作函数的示例Plugin API documentation 看起来它也是save_post
操作,并返回$post_id
虽然没有说明原因,但还是谈到了成功:
function email_friends($post_ID) {
$friends = \'[email protected],[email protected]\';
mail($friends, "sally\'s blog updated",
\'I just put something on my blog: http://blog.example.com\');
return $post_ID;
}
有谁能告诉我,如果有什么事的话,我应该从
save_post
行动,以及为什么,最好有某种官方参考?