I am trying to add a script code on all pages of my drupal except a few. Is there any conditionals I can use in html.tpl.php ? or any functions on the template.php to achieve this?
I tried the code below on template.php but no luck :(
Thanks in advance!
function THEME_preprocess_page(&$variables) {
if (isset($variables['node']->type)) {
$variables['theme_hook_suggestions'][] = 'page__' .
$variables['node']->type;
}
//this is what I am trying
if ($variables['nid'] == '77') {
drupal_add_js(drupal_get_path('theme', 'THEME') .'/scripts/path.js');
}
}
You can use hook_page_build(&$page)
function MYMODULE_page_build(&$page){
$nids = array(123, 1234); // your nids
if(($node = menu_get_object('node', 1)) && in_array($node->nid ,$nids) ){
drupal_add_js(drupal_get_path('theme', 'mythemename') .'/scripts/path.js');
}
}
Clear all cache after create this function to see result , also ensure of path script is correct ;)
Related
I've been racking my brains over this one but I'll do my best to describe the problem as best as possible. I have a custom function written within template.php, with a bunch of conditionals. When a condition is true, I would like to assign a value to a variable, and then pass that variable intro a node preprocess function that allows that variables to be rendered on a node template.
The function containing the condition:
function _mytheme_date_repeat_string($vars) {
$exdate_pos = strpos($rrule['WKST'], 'EXDATE:');
if($exdate_pos > 0) {
$vars['testvar'] = 'abc123';
}
}
The preprocess function that I would like to render the variable in for node template use:
function mytheme_preprocess_node(&$vars, $hook) {
$vars['new_variable'] = $testvar;
}
Intended usage in node.tpl.php:
<?php print $new_variable; ?>
I'm not great with PHP, but I know enough about programming to know that variable scope might be an issue here. What would be the best way to implement this? Any guidance is greatly appreciated.
Thanks,
Mark.
If it is not called, your _mytheme_date_repeat_string() function will never be executed. Preprocess functions (ie. any function starting mytheme_preprocess_, are called automatically by Drupal's theme system.
What you need is either move the code of _mytheme_date_repeat_string() in mytheme_preprocess_node() or refactor it and call it.
function _mytheme_date_repeat_string($rrule) {
$exdate_pos = strpos($rrule['WKST'], 'EXDATE:');
if($exdate_pos > 0) {
return 'abc123';
}
else {
return NULL;
}
}
/**
* Prepares variables for node templates.
*/
function mytheme_preprocess_node(&$variables, $hook) {
// Get $rrule from somewhere
$rrule = ... ;
$testvar = _mytheme_date_repeat_string($rrule);
if ($testvar) {
$variables['new_variable'] = $testvar;
}
}
You code does not show where the $rrule calue comes from. I assume you would get it for $variables['node'].
I have this code in my header.php to change the website's logo based on the page's slug:
$logo_img = 'default';
if (is_page('brookside')) {
$logo_img = 'brookside';
} elseif (is_page('hilltop')) {
$logo_img = 'hilltop';
} elseif (is_page('reserve')) {
$logo_img = 'reserve';
}
Which works great. However, the pages a need to change the logo are query string URL, so they don't have slugs. I was able to add a unique class to each page. My question is:
How do I translate that code to "if a page has "x" class? So, what function should I use instead of is_page?
Any help would be very appreciated!
Thanks!
You can use:
if($_GET['ct_additional_features'] == 'brookside') {
$logo_img = 'brookside';
}
You could use get_class that outputs the Class Name and there solve your if with what are you saying
Im new in wordpress.
I want to use two tables and display some content without creating pages or posts.
The matched url will be something like this:
/[category]/[image] - /nature/green-tree
My method was to check the url in index.php from theme and split the url and create a mini route system just for the gallery. But I think it is not the smartest idea.
I don't want to use gallery plugins because I'm already using one, and this is a change that I need to to.
What is the best method to do this?
I suppose you need custom rewrite rules. Here is how you can do it by yourself.
If you are using a theme then open the functions.php file and enter the following code, otherwise if you are using a plugin, place this code somewhere in your plugin, but make sure, that it is loading immediately.
function registerCustomUrl($rewrite)
{
$rewrites = $rewrite->rules;
$newRewrites = array();
$newRewrites['([^/]+)/([^/]+)/?$'] = 'index.php?gallery_category=$matches[1]&gallery_image=$matches[2]';
foreach($rewrites as $rk => $rv)
{
$newRewrites[$rk] = $rv;
}
$rewrite->rules = $newRewrites;
}
add_action('generate_rewrite_rules', 'registerCustomUrl');
function registerCustomQueryVars( $vars ) {
$vars[] = 'gallery_category';
$vars[] = 'gallery_image';
return $vars;
}
add_filter('query_vars', 'registerCustomQueryVars');
function myCustomTemplateRedirect()
{
global $wp_query;
if(
isset($wp_query->query_vars['gallery_category']) &&
!empty($wp_query->query_vars['gallery_category']) &&
isset($wp_query->query_vars['gallery_image']) &&
!empty($wp_query->query_vars['gallery_image'])
)
{
// Within your custom template file you are able to
// use any theme function like the get_header(), get_footer(), etc
require_once('path/to/your/custom/template/file.php');
exit(0);
}
}
add_action("template_redirect", 'myCustomTemplateRedirect');
So I'm setting up a system that has a lot of emails, and variable replacement within it, so I'm writing a class to manage some variable replacement for templates stored in the database.
Here's a brief example:
// template is stored in db, so that's how this would get loaded in
$template = "Hello, %customer_name%, thank you for contacting %website_name%";
// The array of replacements is built manually and passed to the class
// with actual values being called from db
$replacements = array('%customer_name%'=>'Bob', '%website_name%'=>'Acme');
$rendered = str_replace(array_keys($replacements), $replacements, $template);
Now, that works well and good for single var replacements, basic stuff. However, there are some places where there should be a for loop, and I'm lost how to implement it.
The idea is there'd be a template like this:
"hello, %customer_name%, thank you for
requesting information on {products}"
Where, {products} would be an array passed to the template, which the is looped over for products requested, with a format like:
Our product %product_name% has a cost
of %product_price%. Learn more at
%product_url%.
So an example rendered version of this would be:
"hello, bob, thank you for requesting
information on:
Our product WidgetA has a cost of $1.
Learn more at example/A
Our product WidgetB has a cost of $2.
Learn more at example/B
Our product WidgetC has a cost of $3.
Learn more at example/C.
What's the best way to accomplish this?
Well, I really dont see the point in a template engine that uses repalcements/regex
PHP Is already a template engine, when you write <?php echo $var?> its just like doing <{$var}> or {$var}
Think of it this way, PHP Already translates <?php echo '<b>hello</b>'?> into <b>hello</b> by its engine, so why make it do everything 2 times over.
The way i would implement a template engine is like so
Firstly create a template class
class Template
{
var $vars = array();
function __set($key,$val)
{
$this->vars[$key] = $val;
}
function __get($key)
{
return isset($this->vars[$key]) ? $this->vars[$key] : false;
}
function output($tpl = false)
{
if($tpl === false)
{
die('No template file selected in Template::output(...)');
}
if(!file_exists(($dir = 'templates/' . $tpl . '.php')))
{
die(sprintf('Tpl file does not exists (%s)',$dir));
}
new TemplateLoader($dir,$this->vars);
return true;
}
}
This is what you use in your login such as index.php, you will set data just like an stdClass just google it if your unsure. and when you run the output command it sends the data and tpl to the next class below.
And then create a standalone class to compile the tpl file within.
class TemplateLoader
{
private $vars = array();
private $_vars = array(); //hold vars set within the tpl file
function __construct($file,$variables)
{
$this->vars = $variables;
//Start the capture;
ob_start();
include $file;
$contents = ob_get_contents();
ob_end_clean(); //Clean it
//Return here if you wish
echo $contents;
}
function __get($key)
{
return isset($this->vars[$key]) ? $this->vars[$key] : (isset($this->_vars[$key]) ? $this->_vars[$key] : false) : false;
}
function __set($key,$val)
{
$this->_vars[$key] = $val;
return true;
}
function bold($key)
{
return '<strong>' . $this->$key . '</string>';
}
}
The reason we keep this seperate is so it has its own space to run in, you just load your tpl file as an include in your constructor so it only can be loaded once, then when the file is included it has access to all the data and methods within TemplateLoader.
Index.php
<?php
require_once 'includes/Template.php';
require_once 'includes/TemplateLoader.php';
$Template = new Template();
$Template->foo = 'somestring';
$Template->bar = array('some' => 'array');
$Template->zed = new stdClass(); // Showing Objects
$Template->output('index'); // loads templates/index.php
?>
Now here we dont really want to mix html with this page because by seperating the php and the view / templates you making sure all your php has completed because when you send html or use html it stops certain aspects of your script from running.
templates/index.php
header
<h1><?php $this->foo;?></h1>
<ul>
<?php foreach($this->bar as $this->_foo):?>
<li><?php echo $this->_foo; ?></li>
<?php endforeach; ?>
</ul>
<p>Testing Objects</p>
<?php $this->sidebar = $this->foo->show_sidebar ? $this->foo->show_sidebar : false;?>
<?php if($this->sidebar):?>
Showing my sidebar.
<?php endif;?>
footer
Now here we can see that were mixing html with php but this is ok because in ehre you should only use basic stuff such as Foreach,For etc. and Variables.
NOTE: IN the TemplateLoader Class you can add a function like..
function bold($key)
{
return '<strong>' . $this->$key . '</string>';
}
This will allow you to increase your actions in your templates so bold,italic,atuoloop,css_secure,stripslashs..
You still have all the normal tools such as stripslashes/htmlentites etc.
Heres a small example of the bold.
$this->bold('foo'); //Returns <strong>somestring</string>
You can add lots of tools into the TempalteLoader class such as inc() to load other tpl files, you can develop a helper system so you can go $this->helpers->jquery->googleSource
If you have any more questions feel free to ask me.
----------
An example of storing in your database.
<?php
if(false != ($data = mysql_query('SELECT * FROM tpl_catch where item_name = \'index\' AND item_save_time > '.time() - 3600 .' LIMIT 1 ORDER BY item_save_time DESC')))
{
if(myslq_num_rows($data) > 0)
{
$row = mysql_fetch_assc($data);
die($row[0]['item_content']);
}else
{
//Compile it with the sample code in first section (index.php)
//Followed by inserting it into the database
then print out the content.
}
}
?>
If you wish to store your tpl files including PHP then that's not a problem, within Template where you passing in the tpl file name just search db instead of the filesystem
$products = array('...');
function parse_products($matches)
{
global $products;
$str = '';
foreach($products as $product) {
$str .= str_replace('%product_name%', $product, $matches[1]); // $matches[1] is whatever is between {products} and {/products}
}
return $str;
}
$str = preg_replace_callback('#\{products}(.*)\{/products}#s', 'parse_products', $str);
The idea is to find string between {products} and {products}, pass it to some function, do whatever you need to do with it, iterating over $products array.
Whatever the function returns replaces whole "{products}[anything here]{/products}".
The input string would look like that:
Requested products: {products}%product_name%{/products}
drupal_add_css('override/files/style.css');
This kind of statement can't ensure the css is loaded last,
how can I do the trick?
Use the group attribute :
drupal_add_css(drupal_get_path('theme', 'mytheme') . '/css/styles.css', array('group' => CSS_THEME);
The CSS gets added in the order drupal_add_css() is called, which depends largely on the weight of the module or theme doing the calling, stored in the system table. Drupal.org's instructions on changing module weight also work for themes.
The other factor determining the order of drupal_add_css() is simply the order in which the containing function is called in the code of Drupal. template_preprocess_page(), for example, is always called after template_preprocess_node(), simply because the node goes in the page.
For Drupal 6 use:
drupal_add_css('site.css', 'theme', '', array('weight' => 999))
if you look in the template.php theme file of the ninesixty theme you will find a solution.
The following function are called from the theme_preprocess_page hook:
function ninesixty_css_reorder($css) {
global $theme_info, $base_theme_info;
// Dig into the framework .info data.
$framework = !empty($base_theme_info) ? $base_theme_info[0]->info : $theme_info->info;
// Pull framework styles from the themes .info file and place them above all stylesheets.
if (isset($framework['stylesheets'])) {
foreach ($framework['stylesheets'] as $media => $styles_from_960) {
// Setup framework group.
if (isset($css[$media])) {
$css[$media] = array_merge(array('framework' => array()), $css[$media]);
}
else {
$css[$media]['framework'] = array();
}
foreach ($styles_from_960 as $style_from_960) {
// Force framework styles to come first.
if (strpos($style_from_960, 'framework') !== FALSE) {
$framework_shift = $style_from_960;
$remove_styles = array($style_from_960);
// Handle styles that may be overridden from sub-themes.
foreach ($css[$media]['theme'] as $style_from_var => $preprocess) {
if ($style_from_960 != $style_from_var && basename($style_from_960) == basename($style_from_var)) {
$framework_shift = $style_from_var;
$remove_styles[] = $style_from_var;
break;
}
}
$css[$media]['framework'][$framework_shift] = TRUE;
foreach ($remove_styles as $remove_style) {
unset($css[$media]['theme'][$remove_style]);
}
}
}
}
}
return $css;
}
Use the weight attribute :
drupal_add_css(drupal_get_path('theme', 'mytheme') . '/css/styles.css', array('weight' => 999));
Note : CSS added in your theme's info will always be last, so use the same function as before to change the weight of your stylesheets declared in theme's info file.
This worked for me. Adds the css after the last css on the page
$module_path = drupal_get_path('module', 'my_module');
drupal_add_css($module_path . '/css/my_module.css', array('weight' => 99999, 'group'=>CSS_THEME));