• I’ve created a plugin that has a shortcode which uses wp_mail() to send the user an email when they land on a quiz Thank You page. The email address is retrieved using the quiz provider’s API. It works well, but sends the user 4 emails instead of 1. I’m guessing it has to do with my SEO plugin loading pages multiple times—people in other threads have had luck disabling it. I’m working on a large and popular site, so I don’t want to disable the plugin and mess up the SEO. I’ve also tried putting my PHP code in a page template instead of a shortcode, the same behavior occurs.

    If I call wp_die() after the wp_mail() line, only 1 email is sent, but then the page displays an error. I want to show my Thank You page and send the email.

    Suggestions would be very appreciated! Thanks in advance.

    • This topic was modified 2 years, 9 months ago by earnesthood.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hey there,

    Perhaps test it on a staging or local server? Then you can check if the issue remains after deactivating the SEO plugin. If it is gone, then you can address it with the plugin support.

    Moderator bcworkz

    (@bcworkz)

    I cannot replicate such behavior on a plain vanilla site, so it’s likely due to something your theme or a plugin is doing.

    While the behavior is unusual for shortcodes, similar behavior is not unusual for filter and action hooks. In those cases, one way to avoid the behavior is to have the callback remove itself after it had first run. You could try a similar approach by doing a custom action hook of your own making. Have your action’s callback send the email, then remove itself from the action stack. Assuming the undesired behavior is occurring during one single request, this will ensure only one email is sent.

    If the behavior is due to multiple requests, like reloading the page, that approach will not help any. If that’s the case, perhaps you could set a session variable with a current timestamp. Before sending email, check the session variable and only send email if the variable either does not exist or the timestamp is so old that it must be an unrelated request.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Shortcode being called multiple times on page load’ is closed to new replies.