I have the following that i need to add to all drupal pages of a website:
<script type="text/javascript">
var __cs = __cs || [];
__cs.push(["setCsAccount", "code"]);
</script>
<script type="text/javascript" async src="https://...cs.min.js"></script>
I tried to add it to one of the themes i have as a library by using
(function ($) {
Drupal.behaviors.myModuleBehavior = {
attach: function (contect, settings) {
document.write('<script type="text/javascript">...</script>');
}
}
})
But nothing happened, there is another theme in the website with the following files
I'm not sure I'm going on at it in the correct way since i'm new to drupal but the most important thing to me is that i prefer not to add any modules or outside libraries to the website
Override the html.html.twig of whatever theme your theme is inheriting from and just put your javascript in the head section.
find what theme your theme is inheriting from, it will say in your themes .info.yml file, look for "base theme". For example HERE you can see that the classy theme is inheriting from the stable theme.
Copy the html.html.twig file from your base theme and put it in your theme, keeping the directory structure is probably a good idea, but not absolutely necessary. If you can't find the file in your base theme, or your theme does not have a base theme, you could just use the html.html.twig from one of the core themes, eg the stable themes html.html.twig file.
Add your javascript to the head section of your html.html.twig file. Add the first code that you posted, probably no need for drupal behaviors.
Related
I am using a custom theme in Drupal and the jquery and bootstrap version in pretty outdated. Old pages in are in that version but I want to design few new pages where I want to include the latest version of bootstrap and jquery. Now whenever I design a new page, Drupal itself adds certain wrappers to it and includes the previous bootstrap, jquery version in the aggregate js file. I just want it to be plain html and not include any other css/js files or make any other modifications.
I have added CSS, JS files in link and script tags. And few elements also have inline css. I just want it to render as a simple HTML page
Currently this is my theme.inc file
foreach ($stylesheets as $stylesheet) {
drupal_add_css($stylesheet, array('group' => CSS_THEME, 'every_page' => TRUE, 'media' => $media));
}
I want to restrict this for certain pages. Any leads?
You could try hook_css_alter() and hook_js_alter() like it is suggestsed here:
Drupal 7 override jquery js file in custom theme
In your node preprocess function in template.php in your theme. But I don't know if you could limit this to just some nodes, or pages.
There are many questions asking how to change the theme in Semantic-UI, but I have not been able to find even a question where it refers to changing the theme dynamically, i.e. after a webpack build.
I want to allow each user of a site to save their own preference for the theme. I have some users who prefer dark themes, and others who are color-blind, and others who have weak eyes and need larger fonts, or more contrast, etc.
I know it's possible to change the theme dynamically since the semantic-ui demos all do it. Unfortunately, the Theming page and all documentation I have seen describes how to change the site-wide theme, and apply that, in a new site-wide build. Or to customize that (still) site-wide build.
I think I'd be happy to just be able to add a class to the class list for an element (e.g. "github") and have it use that theme for that user (even if it was just for that element). But ideally, I'd like to have my page load an extra .less or .css file(s) with site-wide overrides for that user, for the user-selected theme.
I'm still pretty new semantic-ui and to applying dynamic changes to a webpack site. Any suggestions for how to apply additional less variable changes after build, or to reload entire Semantic-UI themes, like the demo does?
Note that demo site is not a link to GitHub; it's a look-alike with a paint can icon near the top-right which brings up a sidebar that allows you to change themes. Dynamically.
Update:
I need to test this now, but I may have an answer for my own question here.
It seems that the gulp build process typically compiles and combines all the less and other files into the dist folder as semantic.css, semantic.js, semantic.min.css and semantic.min.js. It also produces different individual component .css files in the dist/components subfolder, but (I think) if you're loading the full css file (e.g. semantic.min.css), that you don't really need the components subfolder. That this is present for those sites who want to optimize to the point of only including the .css files for the components they use?
So it's already processed and combined, and to swap themes, I think all that is necessary is to swap one semantic.min.css file with the output of the build for another theme. The .js files are the same, at least for default vs github themes.
If this is true, it's a matter of copying the semantic.min.css to an alternative file, for example, semantic.github.min.css and use that .css file instead. Or copy it to a theme subfolder like github/semantic.min.css. Then, in either case, update the DOM with a new href on the stylesheet originally referenced.
Summary: It looks like it's all in the semantic*.css file and swapping the output of different builds allows us to swap themes. I'll test this later tonight.
Update 2:
I updated the theme.config file with all github entries, then rebuilt the dist folder, copied the semantic.min.css as semantic-github.min.css to my static folder with the original, then just updated the href to select it:
// normally: <link rel="stylesheet" type="text/css" href="/static/semantic/semantic.min.css">
// non-default: <link rel="stylesheet" type="text/css" href="/static/semantic/semantic-theme.min.css">
function swapThemeCss (theme) {
console.log('Theme change to "' + theme + '".')
let sheet = document.querySelector('#theme')
if (!sheet) {
return
}
if (theme === 'default') {
sheet.setAttribute('href', '/static/semantic/semantic.min.css')
} else {
sheet.setAttribute('href', '/static/semantic/semantic-' + theme + '.min.css')
}
}
Oh also, in the example above, I gave the link an id of 'theme' to make it easier to find it and replace the href dynamically:
<link id="theme" rel="stylesheet" type="text/css" href="/static/semantic/semantic.min.css">
"So it's already processed and combined, and to swap themes, I think all that is necessary is to swap one semantic.min.css file with the output of the build for another theme."
Correct.
Depending on whether you're having per-user styles, or just multiple themes the user can pick from, you can simply have separate less files with per-theme overrides that can be compiled with webpack but perhaps not inserted into your index.html. Then you can dynamically add/remove the <link>s to depending on the user preference. The dynamic adding will cause a flicker from the default styles to the per-user theme styles if you're inserting the <link> tags via frontend javascript (because it must wait for the frontend JS to load, which will render the page/default styles in the meantime, then inject the new <link> and only show the new styles once those have been loaded). Add the per-user <link> tags serverside to make it seamless.
sorry if this is a basic question but I'm having trouble figuring out how to use a Javascript/jQuery script in my website. (If this is well documented and I just don't know the right terms, please share what to look for - I've had no luck yet online with what I know.)
I'd already designed a site with static pages, it just didn't have a blog. So I put Wordpress in for one of the pages, and figured out how to add in the HTML/CSS design for my existing site's menu using the header.php and footer.php files. However, my jQuery script that's supposed to load when the page is resized in order to change the menu design is not working.
I pasted the <script type="text/javascript" src="jquery.min.js"></script> code in the <head> part of the file, adjusted the body tag to <body onresize="myFunction()" <?php body_class(); ?>>, and added the "myFunction" script to the footer.php file, above the </body> tag. Above function myFunction() { I also have window.onload=myFunction(); which isn't working either - so no matter what size the page is loaded at or what size it's changed to the menu is unresponsive.
When I tried adding an enqueue code block to the functions.php file it broke the page entirely, giving a 500 error.
Any advice would be hugely appreciated!
Check that the path you specify to the jQuery library is correct. In your script tag src="jquery.min.js is a relative path. This means that the file jquery.min.js is in the same directory as your html/php/template according to your src path.
I would recommend using a CDN. You won't have a relative path, need to load your jquery files, and you'll benefit from browser caching if other pages/sites have loaded the same files.
3x
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
2x
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
When you add resources such as scrips and style sheets to WordPress you should enqueue them using the functions wp_enqueue_script() and wp_enqueue_style().
Doing this ensures that your scirpt/style is only included once, allows you to declare dependancies, and (in the case of scripts) allows you to add it to the footer should you require/desire.
You can find more information on these functions in the Codex -
wp_enqueue_script()
wp_enqueue_style()
In conjunction with these functions, you should use the wp_enqueue_scripts action, ensuring that your scripts styles get enqueued at the correct time.
So for example, you should place something similar to this in your functions.php file. Because jQuery already exists within WP, you don't even need to specify a location, you just tell WP that you want to enqueue it -
add_action( 'wp_enqueue_scripts', 'my_enqueue_jquery' );
function my_enqueue_jquery(){
wp_enqueue_script( 'jquery' );
}
After asking on the Prestashop forum and receiving no reply I wanted to ask you guys and hope for an answer.
I am trying to add an animated snow plugin to my shop but after looking at the header.tpl file which instructs you to not edit - how do I add my own Javascript to the head of my template?
I duplicated the default-theme and I am working from that.
If the theme is default-bootstrap, then yes, you probably shouldn't modify it, if you intend to upgrade it (can be automatically upgraded using autoupgrade module).
Same can be true for 3rd party themes which are actively updated. But usually 3rd party themes don't get upgraded at all, which means you can modify theme templates. Because the templates are sort-of too complex to be extended by a child theme, it is ok to edit them directly. PretaShop doesn't have child-parent theme system. Just edit the templates directly.
If you would like your changes to be portable accross themes, then you should probably make a module. Inside the module use special functions to add .js and .css files to header:
mymodule.php
...
public function install()
{
...
$this->registerHook('displayHeader');
...
}
public function hookDispayHeader()
{
$this->context->controller->addJS($this->_path.'js/script.js');
$this->context->controller->addCSS($this->_path.'css/style.css');
}
If you need a quick way to add, just edit theme's global.css and global.js
You may also add the stysheet and script to autload folder:
themes/theme1/css/autoload/ and
themes/theme1/js/autoload/. Files inside these folder will be loaded for all pages.
Add custom JS or CSS in theme, using this :
<link rel="stylesheet" href="{$css_dir}bootstrap/bootstrap.css" type="text/css" media="all" />
<script type="text/javascript" src="{$js_dir}bootstrap.js"></script>
$css_dir links to your theme css directory.
$js_dir links to your theme js directory.
Assuming you're working on your own theme (or duplicated the default-theme for custom use)
I am in need of help!
Can someone PLEASE walk me through the specifics, from start to finish, on how to add a jquery plugin script to my wordpress site?
I'm new to Jquery and I have searched the web for the last week. I've tried it myself several times and its just not working. I've asked for help on other forums and I haven't received any answers. Is this something fairly simple to do?
Just to let you know, I am in need step by step instructions. It's just that when I ask questions about this on forums, people are giving me answers and assuming that I already know certain things...I am a beginner with jquery and I need help with each step of the process of adding a jquery script, from start to finish.
The script I want to add is here:
http://tympanus.net/codrops/2010/03/09/a-fresh-bottom-slide-out-menu-with-jquery/
I want to add it on my homepage in the theme I'm using.
1) How do I call the jquery (wp_enqueue_script)
2) Where do I place this (above)
3) CSS - do I simply append the css from the script to my style.css file?
4) How do I call the script to action on the wordpress page?
5) How do I incorporate the html?
These are some of the questions that I have. I've been searching all over the net and usually people assume that people know how to incorporate javascript libraries into their themes....well some of us beginners need help :-)
Any assistance is appreciated!
Thanks!!!
~T
The slide-out menu you linked to consists of images, a stylesheet, a minified version of the jQuery library and and index.html containing the javascript of the menu.
The jQuery included in the archive you downloaded is not neccessary, as jQuery is by default included in wordpress. If you want to add the newest minified jQuery to WP do it from the google libraries in your functions.php like so:
function jq_load_from_google() {
if (!is_admin()) {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery',
'https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js');
wp_enqueue_script( 'jquery' );
}
}
add_action('init', 'jq_load_from_google');
You could append the css to your existing stylesheet or (as in the following example) add it to the theme separately. Rename the stylesheet to menu.css (or the like), place the images and stylesheet in your themes /css and /images folders and add the following to your theme's header.php before wp_head() and after your main stylesheet:
<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/css/menu.css" type="text/css" />
Add the javascript (the second script that contains code, not the jQuery line) included in the downloaded index.html to your theme's header.php after wp_head().
Change the all occurences #menu in the js to your wordpress menu's ul's ID.
See the WP docs for details and examples: http://codex.wordpress.org/Function_Reference/wp_enqueue_script
Use Firebug to look for errors and conflicts: http://getfirebug.com/