{"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

The problem with is_admin()<\/code><\/h2>\n\n\n\n

The 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\n

But 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\n

The 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

<?php<\/span>\nfunction<\/span> is_admin_request<\/span>()<\/span> <\/span>{\n\tif<\/span> ( function_exists( 'wp_doing_ajax'<\/span> ) ) {\n\t\treturn<\/span> is_admin() && ! wp_doing_ajax();\n\t} else<\/span> {\n\t\treturn<\/span> is_admin() && ! ( defined( 'DOING_AJAX'<\/span> ) && DOING_AJAX );\n\t}\n}<\/span><\/code><\/div>Code language:<\/span> HTML, XML<\/span> (<\/span>xml<\/span>)<\/span><\/small><\/pre>\n\n\n

That worked for the front end, but the same user later found a new issue with that: the plugin now lazy loads the post thumbnail feature in the backend. With that, a newly chosen featured image did not show up in the meta box directly, but only after saving the post.<\/p>\n\n\n\n

This is the current solution:<\/p>\n\n\n

<?php<\/span>\n\/**\n * Check if this is a request at the backend.\n *\n * @return<\/span> bool true if is admin request, otherwise false.\n *\/<\/span>\nfunction<\/span> is_admin_request<\/span>()<\/span> <\/span>{\n\t\/**\n\t * Get current URL.\n\t *\n\t * @link<\/span> https:\/\/wordpress.stackexchange.com\/a\/126534\n\t *\/<\/span>\n\t$current_url = home_url( add_query_arg( null<\/span>, null<\/span> ) );\n\n\t\/**\n\t * Get admin URL and referrer.\n\t *\n\t * @link<\/span> https:\/\/core.trac.wordpress.org\/browser\/tags\/4.8\/src\/wp-includes\/pluggable.php#L1076\n\t *\/<\/span>\n\t$admin_url = strtolower( admin_url() );\n\t$referrer = strtolower( wp_get_referer() );\n\n\t\/**\n\t * Check if this is a admin request. If true, it\n\t * could also be a AJAX request from the frontend.\n\t *\/<\/span>\n\tif<\/span> ( 0<\/span> === strpos( $current_url, $admin_url ) ) {\n\t\t\/**\n\t\t * Check if the user comes from a admin page.\n\t\t *\/<\/span>\n\t\tif<\/span> ( 0<\/span> === strpos( $referrer, $admin_url ) ) {\n\t\t\treturn<\/span> true<\/span>;\n\t\t} else<\/span> {\n\t\t\t\/**\n\t\t\t * Check for AJAX requests.\n\t\t\t *\n\t\t\t * @link<\/span> https:\/\/gist.github.com\/zitrusblau\/58124d4b2c56d06b070573a99f33b9ed#file-lazy-load-responsive-images-php-L193\n\t\t\t *\/<\/span>\n\t\t\tif<\/span> ( function_exists( 'wp_doing_ajax'<\/span> ) ) {\n\t\t\t\treturn<\/span> ! wp_doing_ajax();\n\t\t\t} else<\/span> {\n\t\t\t\treturn<\/span> ! ( defined( 'DOING_AJAX'<\/span> ) && DOING_AJAX );\n\t\t\t}\n\t\t}\n\t} else<\/span> {\n\t\treturn<\/span> false<\/span>;\n\t}\n}<\/span><\/code><\/div>Code language:<\/span> HTML, XML<\/span> (<\/span>xml<\/span>)<\/span><\/small><\/pre>\n\n\n

If you know a better solution, please share it with me \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"

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","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","lazy_load_responsive_images_disabled":false},"categories":[115],"tags":[],"wp-worthy-pixel":{"ignored":false,"public":"094d689e53674c518a1a04283b7a60b0","server":"vg07.met.vgwort.de","url":"https:\/\/vg07.met.vgwort.de\/na\/094d689e53674c518a1a04283b7a60b0"},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3815"}],"collection":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/comments?post=3815"}],"version-history":[{"count":2,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3815\/revisions"}],"predecessor-version":[{"id":5869,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3815\/revisions\/5869"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=3815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=3815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=3815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}