I am desperately attempting to find a way to get an alert when a specific article on my WordPress is visited (and no, I will not be flooded by emails, the code will be used temporarely) Being new to php, I used this code, but the site gets a critical error if I put it into the functions.php?
function email_alert() {
wp_mail( '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="61001113080d0800210419000c110d044f0f0415">[email protected]</a>', 'Alert', 'This Site was Visited!' );
}
if(is_article(1234)){
}
add_action( 'wp', 'email_alert' );
please help/advice, a big thank you!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
There’s no is_article. I assume you guessed there would be analogously to is_page, but I don’t think article is a built-in WordPress post type. I think you want is_page instead, which looks like it covers all post types.
You should also move the page check into the action handler, to be sure that WordPress has enough state loaded to know if it’s on that page or not at the point it does the check, which it won’t when it’s loading the plugin or theme you’ve added this code to, e.g.
function email_alert() {
if ( is_page( 1234 ) ) {
wp_mail( '[email protected]', 'Alert', 'This Site was Visited!' );
}
}
add_action( 'wp', 'email_alert' );
Method 2
here we go, this is the code that works, triggering an email-alert when a specific article is viewed
function email_alert() {
global $post;
if( $post->ID == 1234) {
wp_mail( '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7819080a11141119381d00191508141d56161d0c">[email protected]</a>', 'Alert', 'This Site was Visited!' );
}
}
add_action( 'wp', 'email_alert' );
Method 3
according to this article, I think a specific post could be adressed like this
function email_alert() {
global $post;
if( $post->ID == 1234) {
wp_mail( '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="30514042595c5951705548515d405c551e5e5544">[email protected]</a>', 'Alert', 'This Site was Visited!' );
}
add_action( 'wp', 'email_alert' );
}
however, emails are still not being sent for some reasons. happy for all inputs and help
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0