0

wp_get_referer() from the wordpress codex:

Retrieve referer from '_wp_http_referer', HTTP referer, or current page respectively.

The current page url being visited is what I want. I want to add a record to mysql with that information. Instead, on column 3 below, you can see wp_get_referrer() repeatedly logs pages from wp-cron.php as I'm testing this in my browser. The page I navigate to is: http://localhost/wptest2/

|      12 |         1 | http://localhost/wptest2/wp-cron.php?doing_wp_cron=1565262572.3126919269561767578125?doing_wp_cron=1565262572.3126919269561767578125             | http://localhost/wptest2/wp-cron.php             | ?doing_wp_cron=1565262572.3126919269561767578125 |
|      13 |        35 | http://localhost/wptest2/wp-admin/plugins.php?plugin_status=all&paged=1&s?plugin_status=all&paged=1&s                                            | http://localhost/wptest2/wp-admin/plugins.php    | ?plugin_status=all&paged=1&s                     |
|      14 |         1 | http://localhost/wptest2/wp-cron.php?doing_wp_cron=1565263405.0508840084075927734375?doing_wp_cron=1565263405.0508840084075927734375             | http://localhost/wptest2/wp-cron.php             | ?doing_wp_cron=1565263405.0508840084075927734375 |
|      15 |        35 | http://localhost/wptest2/wp-admin/plugins.php?plugin_status=all&paged=1&s?plugin_status=all&paged=1&s                                            | http://localhost/wptest2/wp-admin/plugins.php    | ?plugin_status=all&paged=1&s                     |
|      16 |         1 | http://localhost/wptest2/wp-cron.php?doing_wp_cron=1565264568.4674808979034423828125?doing_wp_cron=1565264568.4674808979034423828125             | http://localhost/wptest2/wp-cron.php             | ?doing_wp_cron=1565264568.4674808979034423828125 |
|      17 |        35 | http://localhost/wptest2/wp-admin/plugins.php?plugin_status=all&paged=1&s?plugin_status=all&paged=1&s                                            | http://localhost/wptest2/wp-admin/plugins.php    | ?plugin_status=all&paged=1&s                     |
|      18 |         1 | http://localhost/wptest2/wp-cron.php?doing_wp_cron=1565265231.1210210323333740234375?doing_wp_cron=1565265231.1210210323333740234375             | http://localhost/wptest2/wp-cron.php             | ?doing_wp_cron=1565265231.1210210323333740234375 |
|      19 |        35 | http://localhost/wptest2/wp-admin/plugins.php?plugin_status=all&paged=1&s?plugin_status=all&paged=1&s                                            | http://localhost/wptest2/wp-admin/plugins.php    | ?plugin_status=all&paged=1&s                     |
|      20 |         1 | http://localhost/wptest2/wp-cron.php?doing_wp_cron=1565266149.3741869926452636718750?doing_wp_cron=1565266149.3741869926452636718750             | http://localhost/wptest2/wp-cron.php             | ?doing_wp_cron=1565266149.3741869926452636718750 |
|      21 |        35 | http://localhost/wptest2/wp-admin/plugins.php?plugin_status=all&paged=1&s?plugin_status=all&paged=1&s                                            | http://localhost/wptest2/wp-admin/plugins.php    | ?plugin_status=all&paged=1&s                     |
|      22 |         1 | http://localhost/wptest2/wp-cron.php?doing_wp_cron=1565266261.7427170276641845703125?doing_wp_cron=1565266261.7427170276641845703125             | http://localhost/wptest2/wp-cron.php             | ?doing_wp_cron=1565266261.7427170276641845703125 |
|      23 |        35 | http://localhost/wptest2/wp-admin/plugins.php?plugin_status=all&paged=1&s?plugin_status=all&paged=1&s                                            | http://localhost/wptest2/wp-admin/plugins.php    | ?plugin_status=all&paged=1&s                     |
|      24 |         1 | http://localhost/wptest2/wp-cron.php?doing_wp_cron=1565267458.3441669940948486328125?doing_wp_cron=1565267458.3441669940948486328125             | http://localhost/wptest2/wp-cron.php             | ?doing_wp_cron=1565267458.3441669940948486328125 |

How can I properly use wp_get_referrer() or another function to get the visitor's page visited and not these urls pointing to wp-cron.php?

  • 2
    I'm actually confused about the question... if you visited /wptest2 without coming from any referrers (site/page), then the referrer should be empty. And to get the current page URL, you can use home_url() . wp_unslash( $_SERVER['REQUEST_URI'] ) or home_url( $GLOBALS['wp']->request ) to exclude the query string part. – Sally CJ Aug 23 at 1:02

Your Answer

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

Browse other questions tagged or ask your own question.