{"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<p>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<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">The problem with <code class=\"lang-php\">is_admin()<\/code><\/h2>\n\n\n\n<p>The plugin <a href=\"https:\/\/de.wordpress.org\/plugins\/lazy-loading-responsive-images\/\"><em>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 <code class=\"lang-php\">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<p>But then a user came across a problem with that: the function returns <code class=\"lang-php\">true<\/code> for AJAX requests because they use the <code class=\"lang-markup\">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 <code class=\"lang-php\">is_admin()<\/code> returns <code class=\"lang-php\">true<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The solution<\/h2>\n\n\n\n<p>With that in mind, the solution has to check for AJAX requests somewhere. The first try of a function to replace the <code class=\"lang-php\">is_admin()<\/code> calls looked like the following (kindly directly supplied by the user <em>zitrusblau<\/em> <a href=\"https:\/\/wordpress.org\/support\/topic\/make-it-work-with-ajax-requests\/\">with the issue report<\/a>):<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">is_admin_request<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n\t<span class=\"hljs-keyword\">if<\/span> ( function_exists( <span class=\"hljs-string\">'wp_doing_ajax'<\/span> ) ) {\n\t\t<span class=\"hljs-keyword\">return<\/span> is_admin() &amp;&amp; ! wp_doing_ajax();\n\t} <span class=\"hljs-keyword\">else<\/span> {\n\t\t<span class=\"hljs-keyword\">return<\/span> is_admin() &amp;&amp; ! ( defined( <span class=\"hljs-string\">'DOING_AJAX'<\/span> ) &amp;&amp; DOING_AJAX );\n\t}\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>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<p>This is the current solution:<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-comment\">\/**\n * Check if this is a request at the backend.\n *\n * <span class=\"hljs-doctag\">@return<\/span> bool true if is admin request, otherwise false.\n *\/<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">is_admin_request<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n\t<span class=\"hljs-comment\">\/**\n\t * Get current URL.\n\t *\n\t * <span class=\"hljs-doctag\">@link<\/span> https:\/\/wordpress.stackexchange.com\/a\/126534\n\t *\/<\/span>\n\t$current_url = home_url( add_query_arg( <span class=\"hljs-keyword\">null<\/span>, <span class=\"hljs-keyword\">null<\/span> ) );\n\n\t<span class=\"hljs-comment\">\/**\n\t * Get admin URL and referrer.\n\t *\n\t * <span class=\"hljs-doctag\">@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<span class=\"hljs-comment\">\/**\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\t<span class=\"hljs-keyword\">if<\/span> ( <span class=\"hljs-number\">0<\/span> === strpos( $current_url, $admin_url ) ) {\n\t\t<span class=\"hljs-comment\">\/**\n\t\t * Check if the user comes from a admin page.\n\t\t *\/<\/span>\n\t\t<span class=\"hljs-keyword\">if<\/span> ( <span class=\"hljs-number\">0<\/span> === strpos( $referrer, $admin_url ) ) {\n\t\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">true<\/span>;\n\t\t} <span class=\"hljs-keyword\">else<\/span> {\n\t\t\t<span class=\"hljs-comment\">\/**\n\t\t\t * Check for AJAX requests.\n\t\t\t *\n\t\t\t * <span class=\"hljs-doctag\">@link<\/span> https:\/\/gist.github.com\/zitrusblau\/58124d4b2c56d06b070573a99f33b9ed#file-lazy-load-responsive-images-php-L193\n\t\t\t *\/<\/span>\n\t\t\t<span class=\"hljs-keyword\">if<\/span> ( function_exists( <span class=\"hljs-string\">'wp_doing_ajax'<\/span> ) ) {\n\t\t\t\t<span class=\"hljs-keyword\">return<\/span> ! wp_doing_ajax();\n\t\t\t} <span class=\"hljs-keyword\">else<\/span> {\n\t\t\t\t<span class=\"hljs-keyword\">return<\/span> ! ( defined( <span class=\"hljs-string\">'DOING_AJAX'<\/span> ) &amp;&amp; DOING_AJAX );\n\t\t\t}\n\t\t}\n\t} <span class=\"hljs-keyword\">else<\/span> {\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">false<\/span>;\n\t}\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you know a better solution, please share it with me \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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":{"wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","webmentions_disabled_pings":false,"webmentions_disabled":false,"lazy_load_responsive_images_disabled":false,"footnotes":""},"categories":[115],"tags":[],"class_list":["post-3815","post","type-post","status-publish","format-standard","hentry","category-wordpress-snippets"],"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","targetHints":{"allow":["GET"]}}],"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}]}}