0

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( 'aprilia@example.net', 'Alert', 'This Site was Visited!' );
    }

  if(is_article(1234)){
    }
    add_action( 'wp', 'email_alert' );

please help/advice, a big thank you!

2
  • In general when you get a critical error from WordPress there will be more details in the email it sends you or in the server error log. – Rup Feb 22 at 10:32
  • is_article() isn't a function. WordPress has no idea what to with it. – Tony Djukic Feb 22 at 18:01
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( 'aprilia@example.net', 'Alert', 'This Site was Visited!' );
    }
}
add_action( 'wp', 'email_alert' );
1
  • thank you - site is no more stalling :-) but email is not being sent... – Aprilia P. Feb 22 at 11:58
1

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( 'aprilia@example.net', 'Alert', 'This Site was Visited!' );
    }
} 

add_action( 'wp', 'email_alert' );
0

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( 'aprilia@example.net', '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

3
  • The add_action needs to be outside the email_alert function's closing brace. – Rup Feb 23 at 10:52
  • Do you know that wp_mail works? Does your site send emails at all? – Rup Feb 23 at 10:52
  • @Rup you made my day! it is working now, thank you very much for being so patient! – Aprilia P. Feb 23 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.