{"id":3815,"date":"2017-07-21T14:29:48","date_gmt":"2017-07-21T12:29:48","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=3815"},"modified":"2020-02-09T10:59:48","modified_gmt":"2020-02-09T09:59:48","slug":"wordpress-backend-request","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/wordpress-backend-request-3815\/","title":{"rendered":"Check if request is made from WordPress backend"},"content":{"rendered":"\n
It is not so easy to check if a request was made from the backend or not. This post shows my solution for that, which I build recently.<\/p>\n\n\n\n\n\n\n\n
is_admin()<\/code><\/h2>\n\n\n\nThe plugin Lazy Loading Responsive Images<\/em><\/a> needs to check if a request was made in the backend, to modify only image markup at the front end. The function is_admin()<\/code> checks if a backend page was called \u2013 this was the first solution used in the plugin.<\/p>\n\n\n\nBut then a user came across a problem with that: the function returns true<\/code> for AJAX requests because they use the wp-admin\/admin-ajax.php<\/code>. That means the lazy loading plugin did not work for front end content which is added via AJAX because is_admin()<\/code> returns true<\/code>.<\/p>\n\n\n\nThe solution<\/h2>\n\n\n\n
With that in mind, the solution has to check for AJAX requests somewhere. The first try of a function to replace the is_admin()<\/code> calls looked like the following (kindly directly supplied by the user zitrusblau<\/em> with the issue report<\/a>):<\/p>\n\n\n