Jump to content

Welcome, Guest!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

  • 0
rubben1985

Head code in specific page/directory

Question

Hi everyone,

I need to put a code (script) in the head but only in specific pages or directories. Panda theme includes an option to inject it in the head but I need to do it only in specific directories (i will use a code per language because it is a formulary).

Anyone knows how to do it? or is there any other way to do it that it is not using the head section?

 

Code is similar to this:

<script src="https://XXXXXXXXXXXXXXXXXXXd=37" type="text/javascript" charset="utf-8"></script>
 

Share this post


Link to post
Share on other sites

9 answers to this question

Recommended Posts

  • 0
33 minutes ago, rubben1985 said:

Hi everyone,

I need to put a code (script) in the head but only in specific pages or directories. Panda theme includes an option to inject it in the head but I need to do it only in specific directories (i will use a code per language because it is a formulary).

Anyone knows how to do it? or is there any other way to do it that it is not using the head section?

 

Code is similar to this:

<script src="https://XXXXXXXXXXXXXXXXXXXd=37" type="text/javascript" charset="utf-8"></script>
 

Create a simple module that will hook into `displayHeader`. The hook can conditionally emit any html code based on your condition. 

You can use $this->context->controller to determine controller that's currently handling page render, $this->context->shop to see what shop in multistore is used, $this->context->language to base your decision on selected language,...

  • Like 1

Share this post


Link to post
Share on other sites
  • 0

You can try edit header.tpl file and add:

 

{if $lang_iso == en } 
script src="https://XXXXXXXXXXXXXXXXXXXd=37" type="text/javascript" charset="utf-8"></script>
{elseif $lang_iso == de}
script src="https://XXXXXXXXXXXXXXXXXXXd=40" type="text/javascript" charset="utf-8"></script>
{/if}

 

  • Like 1

Share this post


Link to post
Share on other sites
  • 0
24 minutes ago, datakick said:

Create a simple module that will hook into `displayHeader`. The hook can conditionally emit any html code based on your condition. 

You can use $this->context->controller to determine controller that's currently handling page render, $this->context->shop to see what shop in multistore is used, $this->context->language to base your decision on selected language,...

Thanks, I can imagine is not too complex but not being a developer, creating a simple module is quite far from my knowledge :S

23 minutes ago, Adik said:

You can try edit header.tpl file and add:

 


{if $lang_iso == en } 
script src="https://XXXXXXXXXXXXXXXXXXXd=37" type="text/javascript" charset="utf-8"></script>
{elseif $lang_iso == de}
script src="https://XXXXXXXXXXXXXXXXXXXd=40" type="text/javascript" charset="utf-8"></script>
{/if}

 

That could work to start but very soon I will need to apply code to specific pages (for example blog directory .....com/en/blog/* o a specific blog page with a specific formulary). Is there any piece of code for that? (apply to a directory and apply to 1 page)

Thanks

Share this post


Link to post
Share on other sites
  • 0

Try this:

{if $lang_iso == en && if $category->name == 'Category 1'}
<script src="https://XXXXXXXXXXXXXXXXXXXd=37" type="text/javascript" charset="utf-8"></script>
{elseif $lang_iso == de && if $category->name == 'Category 2'}
<script src="https://XXXXXXXXXXXXXXXXXXXd=40" type="text/javascript" charset="utf-8"></script>
{else $lang_iso == fr && if $category->name == 'Category 3'}
<script src="https://XXXXXXXXXXXXXXXXXXXd=42" type="text/javascript" charset="utf-8"></script>
{/if}

 

  • Like 1

Share this post


Link to post
Share on other sites
  • 0

@Adik any idea how to apply to:

1- specific directories like: */blog/*

2- Specific page: like https://www.lesielle.com/es/blog/el-mejor-activo-antiedad-el-retinol-59

3- URLS containing specific names. For example: when a url include the word "retinol". This could be very helpful to create personalized formularies with my marketing tool.

 

@Jonny Any specific way to put script code in specific blog articles/blog categories? Using Panda theme and your blog module?

 

PS: In case anyone has it, I have discovered I can use VC to include it in specific CMS or product pages.

Share this post


Link to post
Share on other sites
  • 0

What @datakick suggested was the best way, because in that way you don't need to worry about losing your customizations after every theme update.
You actually don't need to create one, you can find several free modules on our website, you can use the "Free custom css and js code" module to do modifications to its displayHeader function to display that script in some specific pages, because you never need to upgrade the "Free custom css and js code" module so your changes will be safe.

$page = Context::getContext()->smarty->getTemplateVars('page');
if ( ($page['page_name']=='module-stblog-article' && in_array(Tools::getValue('id_st_blog'),array(1,2,3)))
|| ($page['page_name']=='module-stblog-category' && in_array(Tools::getValue('id_st_blog_category'),array(1,2,3)))
){
  $this->context->controller->addJS('https://XXXXXXXXXXXXXXXXXXXd=37');
}

 

  • Like 1

Share this post


Link to post
Share on other sites
  • 0

@Jonny No problems with updates because I can put it in Panda theme editor (head/javascript).

The problem is that I am just no developer so no idea what specific code to add to the cases I exposed:

1- specific directories like: */blog/*

2- Specific page: like https://www.lesielle.com/es/blog/el-mejor-activo-antiedad-el-retinol-59

3- URLS containing specific names. For example: when a url includes the word "retinol". This could be very helpful to create personalized formularies with my marketing tool.

4- one of the 3 previous, depending also a language. Ex: directory /blog/ when the language is English

Datakick, adik and you Jonny, you have given me all pieces of code that seems combining them together could allow me to do almost everything I need but I do not know how to put them together :S I just know to replicate, not to construct them

I would just need one specific real example of each, and I suppose I can replicate them changing the "words/url" indicating the page.

 

Share this post


Link to post
Share on other sites
  • 0

Then you can just edit the \themes\panda\header.tpl file, use a piece of code like this

{if $smarty.server.REQUEST_URI|strpos:"/blog/" !== false || $smarty.server.REQUEST_URI|strpos:"retinol" !== false}
        <script src="https://XXXXXXXXXXXXXXXXXXXd=37" type="text/javascript" charset="utf-8"></script>
{/if}

 

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×