I can't seem to override a function using template.php - drupal

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 :)

Related

Theme Action to hook for one time only function

I am developing a custom theme. After my theme is activated/installed I need to run a one time only function that will set some options. What is the best action to hook for this?
Should I use after_setup_theme? My understanding is that this event/action fires on each page load so I don't think I should use this action correct?
*Ps: is it best to place this code in my functions.php file or somewhere else? (I am using the underscores theme template).
You should look at after_switch_theme action. It'll fired only on your theme activation( just one time ). So, your one time function will look:
add_action('after_switch_theme', 'my_one_time_function');
function my_one_time_function () {
//Your code
}
Code goes to functions.php file.
P.S. If you want to run some function before deactivating theme, you can use switch_theme hook. It has same use as after_switch_theme.

What is the right place for reusable render functions in WordPress theme?

Let's say I have a reusable peace of HTML used on different pages in my theme.
In order to not duplicate myself I want to put this peace of HTML into some place and reuse it later. And because it has some parameters (needed for rendering) - it should be a function.
It seems to me that I cannot use get_template_part function because I want to pass some parameters into the render function.
functions.php feels more like a place for low level theme functionality.
What is the right place for render functions in WordPress?
UPD I've just found that in _s theme that I am using as a base code for my theme they have inc/template-tags.php. It feels like a good place. Is it the right way?
check codex set_query_var and get_query_var
In main template
set_query_var('customer_name', $customer_name);
get_template_part( $slug, $name );
template part retrieve it as
$customer_name= get_query_var( 'customer_name', 1 );
echo $customer_name;
Ok so in _s theme they suggest:
Custom template tags in inc/template-tags.php that keep your templates clean and neat and prevent code duplication.
Seems like a good place was found :)

(WP) Override Plugin Function In Theme Functions

How to override function inside plugin and put in theme functions.php file? I've try using remove_filter and add_filter, apply_filter, add_action etc, but still not worked.
https://github.com/mikejolley/WP-Job-Manager/blob/master/wp-job-manager-template.php#L72
I want to override function on line 72. How can I done this? I don't want to edit plugin files because I just want this function replaced only when this theme activated. If not activated, function will be as the original function.
This function used by plugin. And no any filters or actions inside. So, my sorry, you can't solve this issue.

Where to put add_action() in WordPress

I am new to WordPress. I was referring to code of WordPress theme. I am confused about where to put our function and add_action() for that function.
I want to know whether it is right to define function and add_action in one PHP file and put it anywhere in our theme folder.
"functions.php" is the file where you want to put your custom functions, hooks and actions. If your theme doesn't have a functions.php just create it. Wordpress will get the contents of it automatically, so you will have access to defined functions anywhere in your theme.

theme_status_messages() is not firing in my Drupal 6 theme

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.
}

Resources