4

I am working on a WordPress theme, and I would like an author to send an email to someone when a Protected Custom Post is published.

How to get the password of a Protected Post in functions.php?

I already created a metabox field (for the e-mail) and I am using PHPMailer() in my functions.php file. The function works to get the name and the URL of the post, but I now need to send the password by email...

4

2 回答 2

5

The password is stored unencrypted in the column post_password of wp_posts table.

It's just a matter of:

$the_post = get_post( PUT_YOUR_POST_ID_NUMBER );
echo $the_post->post_password;
于 2013-09-24T13:36:54.687 回答
1

If you have set up a custom meta field for the value you wish to send you can just grab it with get_post_custom():

$custom = get_post_custom( get_the_ID() );

Then you can access the meta value by its key from $custom like so:

$custom['keyofmetafield']

EDIT: Additionally I believe there is a core function the_post_password() if you're trying to get the password used to protect the page - but you mentioned saving something in a meta field so I wasn't sure.

https://developer.wordpress.org/reference/functions/the_post_password/#source

于 2013-09-24T12:29:56.893 回答