Im trying to get the path to the current theme (which is a sub theme) but I am having issues.
I have seen these 2 sites:
http://venutip.com/content/getting-path-your-subtheme
http://www.website-assistant.co.uk/web-developer/path-subtheme-drupal
Which has led me to this: (as an example)
template.php
function phptemplate_preprocess_node(&$vars) {
global $theme_key;
$path_to_theme = drupal_get_path('theme', $theme_key);
}
?>
page.tpl
<link rel="stylesheet" href="/<?php print $path_to_theme; ?>/css/print.css" media="print" type="text/css" />
But it doesnt work. What am I missing? I am using Drupal 6.19.
A.
In your phptemplate_preprocess_node function, you need to add the $path_to_theme variable to the $vars array:
function phptemplate_preprocess_node(&$vars) {
global $theme_key;
$vars['path_to_theme'] = drupal_get_path('theme', $theme_key);
}
You are also able to add print CSS via the theme's .info file by adding a line like this:
stylesheets[print][] = css/print.css
The advantage of this approach is that your CSS files will be aggregated properly when CSS aggregation is enabled.
Related
I'm trying to insert html into the head of my WordPress site, the plan is to insert a link tag to preload my main css file so that when it is loaded using wp_enqueue_style it is already preloaded. But I'm not entirely sure how to do this, I've seen a few similar issues and have tried a few approaches with no luck, currently I am trying:
function preload_styles() {
$href = 'styles/main.css';
echo `<link rel='preload' as='style' id='main-css' href='$href' type='text/css'/>`;
}
add_action( 'wp_head','preload_styles', 10);
But it seems to be doing nothing... Nothing is being inserted into the head. Any help on this would be greatly appreciated.
You are messing with a single quote and double quote. Try the below code.
function preload_styles() {
$href = 'styles/main.css';
echo '<link rel="preload" as="style" id="main-css" href="'.$href.'" type="text/css/">';
}
add_action( 'wp_head','preload_styles', 10);
Tested and works
My stylesheet is enqueued if i use
<link rel = "stylesheet" type = "text/css" href = "<?php echo get_template_directory_uri();?>/style.css">
but it's not included when i use
function include_css(){
wp_enqueue_style('main_css',get_stylesheet_uri());
}
add_action('wp_enqueue_scripts','include_css');
You may correct the code like this :
function include_css(){
wp_enqueue_style('main_css',get_template_directory_uri().'/style.css');
}
add_action('wp_enqueue_scripts','include_css');
The issue is that you are trying to enqueue the css file without specifying it's complete url the code you use i.e
> wp_enqueue_style('main_css',get_stylesheet_uri());
This means that you are adding the style.css file which is always on the root directory but still you are giving name as main_css first make sure which css you want to add then someone will able to properly answer your question and the css you are trying to add is in which directory.
Please refer to this link get_stylesheet_uri
To enqueue a stylesheet use this in functions.php
1.we need to register our stylesheet.
2.we should enqueue it.
3.Hook to an action
4.call the function where you want to enqueue it.
function include_css()
{
wp_register_style('main_css',get_template_directory_uri().'/style.css');
wp_enqueue_style('main_css');
}
add_action('wp_enqueue_scripts','include_css');
call,
<?php wp_head();?>
in the place you want to enqueue it.(mostly in header.)
Please add this code in theme's functions.php
function enqueue_scripts() {
wp_enqueue_style('style', get_stylesheet_directory_uri() . '/style.css', array());
}
add_action( 'wp_enqueue_scripts', 'enqueue_scripts' );
I am developing a Wordpress plugin to run a certain javascript function based on some parameters.
The first step is to include the javascript file from my server. Easy:
add_action('wp_head', 'my_plugin_head');
function my_plugin_head(){
echo '<script type="text/javascript" src="http://www.my-server.net/js/w.js" ></script>';
}
The second step is to change certain text in the WordPress body to the javascript function...something like:
add_filter('the_content', 'plugin_text_replace');
function plugin_text_replace($text){
$text=preg_replace('blah', 'blah');
return $text;
//I Am still researching how to setup the preg_replace.
//It will look for something like plugin_call[1, 40, 60]
//And Change it To jsFunction(1, 40, 60);
//Bonus for anyone who can help me with that :)
}
In any case, I realized that I want the my-server.net javascript included ONLY if needed (or in other words, only if the preg_replace found a match). This is problematic to me because I can't find anyway to add a script to the <head> tag without using an action on wp_head, which does not have any reference to the body of the text.
How can I add a header ONLY if a certain preg_match is found?
To add javascript the best way is using wp_enqueue_script(), like this:
function my_scripts_method() {
wp_enqueue_script('my_java_function');
}
// For use on the Front end (ie. Theme)
add_action('wp_enqueue_scripts', 'my_scripts_method');
Inserting code into the <body> can be achieved like this:
function wp_body() {
do_action( 'wp_body' );
}
add_action( 'wp_body', 'my_body_function' ) ; //
// In "header.php" place something like this after <body>:
if ( function_exists('wp_body')) wp_body(); ?>
I create (or better try) to create my first WordPress theme with Less.
What I do is to use a script like that into my functions.php
wp_register_style('screen_css', get_bloginfo('template_url') . '/css/screen.less', array(), false, 'screen');
wp_enqueue_style('screen_css');
and the result is that:
<link rel='stylesheet' id='stigma_screen-css' href='http://www.stigmahost.dch/wp-content/themes/stigmahost/css/screen.less?ver=1.0' type='text/css' media='screen' />
The question is, can I change somehow the rel="stylesheet" when I using the wp_register_style() function ?
While neither function will let you pass that value in, you do have access to the tag before it is rendered with the style_loader_tag filter. If you do something like this...
add_filter('style_loader_tag', 'my_style_loader_tag_function');
function my_style_loader_tag_function($tag){
//do stuff here to find and replace the rel attribute
return $tag;
}
...you should be able to replace the rel attribute with whatever you want. Keep in mind that this filter will pass in the whole tag as html, so you'll have to do a preg_replace() or something similar to replace the value with what you want. Also, this filter will run every time you enqueue a stylesheet, so make sure you test that you've got the right one (with a preg_match() or something) before you alter the rel attribute.
i know this is an old q, but it helped me figure this out. borrowing from brandwaffle's answer here's the full function I used:
function childtheme_scripts() {
wp_enqueue_style('less',get_stylesheet_directory_uri() .'/style.less');
add_filter('style_loader_tag', 'my_style_loader_tag_function');
wp_enqueue_script('less',get_stylesheet_directory_uri() .'/jscripts/less-1.3.0.min.js', false,'1.3.0');
}
add_action('wp_enqueue_scripts','childtheme_scripts', 1);
function my_style_loader_tag_function($tag){
//do stuff here to find and replace the rel attribute
return preg_replace("/='stylesheet' id='less-css'/", "='stylesheet/less' id='less-css'", $tag);
}
I'm working on a new Moodle Assignment plugin.
How can I include a custom CSS to my plugin?
I'm using Moodle 1.9.7.
Thanks in advance.
just add a file called styles.php into the Module's folder.
the file should output CSS code when it is parsed.
Moodle goes into each Module's folder and looks for that file, when the page's CSS layout is constructed.
Some themes can ignore these files using a special setting in the theme's config.php file
$THEME->modsheets = true;
/// When this is enabled, then this theme will search for
/// files named "styles.php" inside all Activity modules and
/// include them. This allows modules to provide some basic
/// layouts so they work out of the box.
/// It is HIGHLY recommended to leave this enabled.
here is a short sample of what could be the content of this styles.php file:
/* simple CSS code */
.left { float:left; }
/* php that generate CSS code */
< ?php if ( right_to_left() ) {echo ".right:text-align:left;"}
else {echo ".right:text-align:right;"} ?>
Perhaps could take a look at the function print_header_simple() defined in lib/weblib.php.
If you are writing a module which generates module specific pages, and they use the normal moodle libraries then stick something in $meta to be added to the <head> section while the page is being generated.
in lib/weblib.php:
function print_header_simple($title='', $heading='', $navigation='', $focus='', $meta='', $cache=true, $button=' ', $menu='', $usexml=false, $bodytags='', $return=false) {
This means that you probably want something like the following in your module:
$extra_css = $CFG->wwwroot.'/theme/SUPA4/supa_report.css';
$extra_css_meta = '<link rel="stylesheet" type="text/css" href="'.$extra_css.'" />';
print_header_simple($mytitle, "", $navigation, "", "", true, "", navmenu($course), '', $extra_css_meta);