Adding a javascript to all pages in OpenCart - php

I'm new to OpenCart and don't have any experience with PHP, so I have a question. I want to add the following JavaScript to hide the url bar on mobile browsers
// When ready...
window.addEventListener("load",function() {
// Set a timeout...
setTimeout(function(){
// Hide the address bar!
window.scrollTo(0, 1);
}, 0);
});
However, I can't find a way to insert this so this code will be executed on all pages in OpenCart. Where should I put this code?

save your script to a file, say 'catalog/view/javascript/myscript.js'
Then add
$this->document->addScript('catalog/view/javascript/myscript.js');
to the catalog/controller/common/header.php some place before this line:
$this->data['scripts'] = $this->document->getScripts();
You could also just place your script inline into catalog/view/theme/{theme name}/template/common/header.tpl using normal html markup.

Looking at the theme documentation, I believe you want to edit the following file:
catalog/view/theme/{your-theme}/template/common/header.tpl
These templates (header, footer, etc) should appear on all pages.

You don't need to go through this trouble especially if you don't have access to FTP. All you need to do is just go to admin panel > design > theme editor > and choose the respective parts to insert the codes. If is footer then just choose footer.twig.
After adding the codes, click Save and you will see the changes immediately. If add the codes directly to the file on FTP, it won't work.
Tried and tested on OpenCart 3

To add a script like that, just go to admin panel > design > theme editor > select "common" > footer and in the end of the file (after </html>) add the script.

If you want script to all your pages of the OC just add it before footer tag in footer.tpl or footer.twig

Related

Wordpress Adding Custom HTTP Header To One Template File

I need to add a header Access-Control-Allow-Origin: * into one of my template files. How would I do this I would like it to be for just this specific template so that its not site wide,
I think you're looking for is_page_template();
<?php
if(is_page_template('your-page-template.php')) { //Change this to the path to your wordpress template
//Add your code here...
} ?>
you can create a custom template page and call the custom Header on this template.
To create a page and apply a page template
use this code to create page template
First you need to have a single page. There are several ways to do it, one is to create a page in wordpress, then use the ID and create a file page-ID.php in the theme folder. Other way is to create a template file which you can select from the side menu in the wordpress editor.
In that page, you can add the php header and the code you want to show.

Sticky navigation script wont attach class to div

I am currently editting a wordpress theme and creating a navigation bar half way the page that sticks to the top of the screen once the user scrolls past a certain point. Ive done this before and it worked fine. I copied that exact code and changed the #divs and classes to the correct ones. This is the code i use:
$(window).scroll(function () {
console.log($(window).scrollTop())
if ($(window).scrollTop() > 10) {
$("#stickbar-steps").addClass("after-scroll-wrap");
}
if ($(window).scrollTop() < 9) {
$("#stickbar-steps").removeClass("after-scroll-wrap");
}
});
However it doesn't seem to add the class to the #div.When i manually add the class to the #div it works fine but it just wont load the script or wont attach the class. Usually when i reach that point in my previous working one, i could see it getting attached using inspect element, but im not seeing it in this one.
The wordpress theme has a seperate page in the admin section which allows me to paste in JS code. I've tried to add it there and i've tried to paste a copy among the other jquery files in the functions.php. But neither of them seems to work.
Any ideas where to look for? Theme uses jquery ver=1.12.4.
Thanks

How to style author id in WordPress?

How to add css to specific wordpress user to hide plugin fiction over front end? I installed the wp-about-author plugin but I need to set as hidden for admin user. So when the admin make any blog post then the wp-about-author wont be displayed. Unfortunately no feedback from the plugin developer. Thanks for any suggestions.
CSS way is not recommended since the user can check out actual DOM by view the source.
Instead, You can prevent from even showing up in the DOM Tree by modifying a plugin php file.
$level = get_the_author_meta('user_level');
if ($level == 10) {
return;
}
Add this line to wp-about-author.php on line #31.
This works, I tested.
Try to update plugin better then hack css :) You can add to wp-about-author.php on line #102 something like:
if (wp_admin()) $return_content = '';
I didn't check it, but might work for you.

Wordpress calling an HTML file from PHP

I am trying to modify a theme on wordpress. The theme shows a slider on the front page, which reads from the featured.php file.
I have gone into the featured.php and removed all the PHP code. I can inject my custom HTML into the featured.php and the pages display properly, however I want the page to display from a wordpress page.
So i tried to:
<?php
$html = file_get_contents('http://www.mydomain.com/homepage-featured');
?>
The above link corresponds to a page i created in wordpress. So i want to be able to inject my HTML into that page and then when you load my homepage, the PHP tells the browser to display the contents of this URL.
The above code doesn't work.
Thanks for the help.
file_get_contents() - as its name suggests - reads a file in but does not print it. You need to echo your $html variable. A better way is to use require() or include() but if I were you I would put my custom file on the same server so that way you don't have to use the file from a remote location thus sparing network traffic.
I think you have better to use the include function.
The file_get_contents you are using would generate an HTTP request, so it would make your script slower. I think it would be a good idea to put the HTML file on the same server if possible.
Have you tried
<?php
require('http://www.mydomain.com/homepage-featured');
?>
If my understanding is correct, you are trying to use a template (featured.php) for a different page other than the front page.
To do so, just change the Page Template of the page(the separate page which is # www.url.com/myhomepage). You can change this # Dashboard > Pages (Click Edit link in the required page) > Edit Page > Page Attributes meta box (available in the rightside below Publish) > Template. Change the template of this page to "Featured".
(I assume that your code in file feature.php has Template Name: Featured at the top)

add code snippet in magento core

I'm running magento 1.4 and I'm trying to display an overlayer banner on all the pages in my magento store. In which file should I add the code snippet for the banner so that the it gets displayed on all pages?
BTW: the code snippet is actually some a short php if function + an OpenX Javascript Tag
From this search:
Go to /admin/cms_block/ and add a block. Remember the identifier.
In your code add <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('identifier')->toHtml() ?>
There you go…
For javascript to be included on every page have a look at page.xml layout <default> section. For PHP code see which blocks are included on the page and create a block after (or before) one of them

Categories