I have a template.php file with a theme called
themename_status_messages
It's not being called/invoked by my theme.
When I use devel themer info on a test dsm output, I am told the candidate functions are:
theme_messages_alter_status_messages()
themename_messages_alter_status_messages()
I'm not sure why the status_messages() call isn't being called during page load. Any ideas?
Looks like the problem was there was a module enabled that changed how this was handled, and I didn't know the module was there and did that. The module was Messages Alter. Taught me a good less on in check the modules page for mysteries.
On your page.tpl.php, is there a $messages var being printing on the page anywhere?
A better way to see if $messages are being passed to your theme is to use a THEME_preprocess_page(&$vars) function in template.php:
function THEME_preprocess_page(&$vars) {
dpm($vars);
// or use $dpr($vars) for a textual array printout
// Replace 'THEME' in the function name with the name of your theme.
}
Related
Bit is a basic question here but can someone confirm that this statement be confirmed: WordPress Pages (certain templates created within) can pull different CSS and JS?
Or - does WordPress only permit universal CSS + JS to be pulled across the entire site?
Thanks for clearing this up.
Depends on what plugin and themes you use. The WordPress/PHP functions wp_enqueue_style() and wp_enqueue_script() can be used literally by everyone (core, themes, plugins, you) to request WordPress to load styles or JavaSctript. You can combine this with WordPress functions to check whether the current page is something you want to filter for (post type, post, front-page, category archive, template, etc.). Here is an example to load a custom style if on front page :
if (is_front_page()) {
wp_enqueue_style('custom-frontpage', 'my/path/to/frontpage.css');
}
You will have to hook this piece of code to the wp_enqueue_script action so that WordPress executes it at the appropriate time. Here is an example using an anonymous function:
add_action('wp_enqueue_scripts', function() {
if (is_front_page())
wp_enqueue_style('custom-frontpage', 'my/path/to/frontpage.css');
});
You can also register your code as a "normal" function and pass the functions name to add_action() instead.
Edit: Enabling and disabling plugins is a bit more difficult, since you can never know how they implement their features without examining the source code. Here are my thoughts on this:
The plugin likely uses the above method (wp_enqueue_styles, wp_enqueue_scripts) to register it's styles and scripts. The plugin, since it assumes to be needed on all pages and posts, does this on every page without the conditional checking described earlier.
You could do one of the following to stop the plugin from doing this:
Identify the place where the plugin loads the styles and scripts and add the if-statement to only do so if the post-ID matches your desired post-ID. This method is bad since your changes are lost every time the plugin is updated.
Write a "counter plugin" (you could just add it to your theme or find a plugin that allowes you to add PHP to your page) that "dequeues" the style and script added by the plugin with inversed conditional tag
The counter-plugin approach would look as follows:
function custom_unregister_plugin() {
if (not the desired blog post) {
wp_dequeue_style('my-plugin-stylesheet-handle');
wp_dequeue_script('my-plugin-script-handle');
}
}
Make sure this function is executed after the enqueuing-code of your plugin by giving it a low priority in the same hook (999 is just an example, test it yourself):
add_action('wp_enqueue_scripts', 'custom_unregister_plugin', 999);
With wp_enqueue_style() you can add stylesheet (https://developer.wordpress.org/reference/functions/wp_enqueue_style/)
You can use it after detecting which template is used
function enqueue_custom_stylesheet() {
if(get_page_template() == 'contact.php')
wp_enqueue_style( 'contact-style', get_template_directory_uri().'/contact.css' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_custom_stylesheet' );
You can use wp_enqueue_style for your CSS, wp_enqueue_script for your JS, wp_localize_script to pass variables from PHP to JS.
You can call these with hooks like:
funtion enqueue_my_stuff()
{
// your enqueue function calls
}
add_action('wp_enqueue_scripts','enqueue_my_stuff'); //front end
add_action('admin_enqueue_scripts','enqueue_my_stuff'); //admin panel
add_action('login_enqueue_scripts','enqueue_my_stuff'); //login screen
I am suggesting a template file in the hook_preprocess_page() implementation done from a module, but the suggested template file doesn't seem to be used.
The template file is page--terminal-template.tpl.php, which is in the directory containing the module, and this is the implementation of hook_preprocess_page().
function terminal_preprocess_page(&$variables) {
if (arg(0) == "terminal") {
$variables['theme_hook_suggestions'][] = "page__terminal_template";
}
}
Could anyone please help me?
Preprocess and process functions can be implemented by modules. In fact, the documentation for theme() lists them when it shows in which order those functions are called.
The fact is that Drupal looks for the suggested template files in the theme directory. You have these alternatives:
Put the template files your module is suggesting in the directory containing the theme currently used
Follow what reported in Load view template on module activation to load the template files from the module directory
Suggest the template files you want to use in a preprocess function implemented by a theme
Following what reported in the other question, you would be able to use the template file found in the module directory. The only problem is that you would be using a generic template that could be different from the default page template used from the currently enabled theme.
If you are adding template files for the currently enabled theme, you should call drupal_theme_rebuild() to make Drupal rescan the directory containing the template files, after you added the new template file to the theme.
Actually, this hook can also be called from theme's template.php file along with module's hook.
Please refer Drupal 7 documentation here.
Say if your active theme is MY_THEME, then the code should be:
function MY_THEME_preprocess_page(&$variables) {
if (arg(0) == "terminal") {
$variables['theme_hook_suggestions'][] = "page__terminal_template";
}
}
And the template suggestions will work.
Edit: This functionality can also be implemented with Modules using hooks.
I wanted to perform a quick alteration of ubercart form data (I want to remove links to product nodes in the shopping-cart). I found a solution that involves implementing hook_form_alter() by creating a new module (link and code below).
My question is, do I have to make a new module or can I just add this function into my theme's template.php file? I tried the later but I couldnt get it to work (I renamed the function to theNameOfMyTheme_form_alter).
(The bigger issue here is me trying to get my head round all this drupal 6 override stuff. Eg if I come across some code that I want to alter, how can you tell if you are supposed to create a module or change your theme?).
http://www.ubercart.org/forum/support/2298/remove_product_links_shopping_cart
function your_module_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'uc_cart_view_form') {
foreach($form['items'] as $key => $item) {
if(!empty($item['desc']['#value'])) {
$form['items'][$key]['desc']['#value'] = strip_tags($item['desc']['#value']);
}
}
}
}
Adding to Clive's solution, just to confirm for anyone else looking for this: hook_form_alter isn't called for themes in Drupal 6.
hook_form_alter() is one of the hooks that's invoked for both themes and modules so putting it in your template.php file is fine.
It's actually documented to that effect on the version of the function for Drupal 7, but I guess they missed it on the Drupal 6 version.
Remember to clear Drupal's cache once you've added the hook so it gets picked up.
I'm trying to alter the comment links in my Drupal output, and I think I have found the function I want to influence, which is function comment_node_view($node, $view_mode).
It is in the Comment module. The problem is I can't seem to effect it, when I try to override it by putting it in my Template.php file and add my theme_ to the function name? In my template.php it looks like this now:
function themename_comment_node_view($node, $view_mode)
if I take off the themename_ it causes an error saying I can't redeclare it. I can copy the comment module and edit it directly but I thought this was how I theme something?
Drupal themes can only implement theme functions (which include template preprocess and process functions) or alter hooks.
comment_node_view() is a hook, but it's not an alter hook (differently the hook name would end with "_alter").
Why cannot themes implement hook_node_view()?
Because hook_node_view() is invoked in comment_build_comment() using the following code:
// Allow modules to make their own additions to the comment.
module_invoke_all('comment_view', $comment, $view_mode, $langcode);
module_invoke_all('entity_view', $comment, 'comment', $view_mode, $langcode);
As it is also highlighted from the comment, module_invoke_all() invokes the hooks implemented in modules, not themes.
If you want to change how a comment is rendered, from a theme, you should create the comment.tpl.php template file for your theme.
The name spacing is 'hook_node_view' so you need to replace 'comment' (the name spacing used by the comment module) with your theme name:
function mytheme_node_view($node, $view_node)
Hooks:
http://api.drupal.org/api/drupal/includes--module.inc/group/hooks/7
hook_node_view:
http://api.drupal.org/api/drupal/modules--node--node.api.php/function/hook_node_view/7
Hope that helps :)
this is a question how to override themable items in Drupal 6.
According to the book "Pro Drupal Development", we can override themable items in two ways:
overriding via Theme functions
overriding via Template files
So for example, in order to reformat the breadcrumb, I can:
via function theme_breadcrumb($breadcrumb)
via breadcrumb.tpl.php
But on my local testing server, the second approach (i.e. via template file) is not working! I see no breadcrumbs at all, while the first approach works fine.
Any idea how could this happen? any special settings I need to configure my Drupal?
thanks!
My custom theme "greyscale":
sites\all\themes\custom\greyscale:
- breadcrumb.tpl.php
- greyscale.info
- node.tpl.php
- page.tpl.php
- style.css
- template.php
relevant file contents:
* template.php:
function phptemplate_preprocess_breadcrumb(&$variables) {
$variables['breadcrumb_delimiter'] = '#';
}
breadcrumb.tpl.php:
Theme functions are setup to either use a template or a function to generate the markup, it will never use both as that's pointless.
For a theme function to use a template, it must be defined when you define it in hook_theme.
A template + preprocess function and a theme function really does the same thing: produce markup. It depends on the situation which method is best to use, that's why we have two. The good thing about templates, is that it allows themers to change the markup, without know much about PHP or Drupal.
Cache
Drupal caches all templates and theme functions defined in your theme, when you create new ones, you need to clear the cache, this can be done by:
Use drush
Clearing cache in admin/settings/performance
Use devel to clear it on each page load. Usable during development, biut will kill performance.
Switching theme back and forth will work too, but it really not the desired way to do it.
I personally always find it easier to alter breadcrumbs through template.php using hook_breadcrumb()
function mytheme_breadcrumb($breadcrumb) {
$sep = ' > ';
if (count($breadcrumb) > 0) {
return implode($breadcrumb, $sep) . $sep;
}
else {
return t("Home");
}
}
Any particular reason why you wish to use a .tpl.php file?