Skip to content

Instantly share code, notes, and snippets.

@freakdesign
Last active October 15, 2021 23:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save freakdesign/95b6d4f02bd7b4f4a1ba to your computer and use it in GitHub Desktop.
Save freakdesign/95b6d4f02bd7b4f4a1ba to your computer and use it in GitHub Desktop.
Detect if an admin is logged into Shopify using liquid.
<!--
Simple method for detecting an Admin in Shopify using liquid.
Quick code by jason at freakdesign.com.au
Related Blog post: http://freakdesign.com.au/blogs/news/34154561
-->
{% capture CFH %}{{ content_for_header }}{% endcapture %}{{ CFH }}
{% if CFH contains 'admin_bar_iframe' %}
{% assign isAdmin = true %}
{% endif %}
@asalisbury
Copy link

Thanks for the gist! Just thought I'd mention {% capture CFH }} should be {% capture CFH %}.

@asalisbury
Copy link

Also, is it necessary to have {{ CFH }} at the end of line 8?

@freakdesign
Copy link
Author

Finally got round to updating the typo mentioned above.
Slow, be I got there in the end...

@sketchybear
Copy link

Really useful, thanks for this!

@annezao
Copy link

annezao commented Aug 13, 2020

doesnt works anymore

@rolis19
Copy link

rolis19 commented Feb 3, 2021

doesnt works anymore

Change 'admin_bar_iframe' to 'adminBarInjector'

@incredimike
Copy link

I just used preview_bar_injector and it's working correctly for me.

@eballeste
Copy link

eballeste commented Oct 15, 2021

  if CFH contains 'adminBar' or CFH contains 'previewBar'
    assign isAdmin = true
  endif

shopify uses:
adminBarInjector when you are viewing the live theme
previewBarInjector when you are previewing a non-live theme

this whole snippet will trigger a Shopify theme-check linter ContentForHeaderModification error but I'm choosing to ignore it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment