ThemeShaper Forums » Thematic

[closed]

Hooks, Filters, Actions, Apply, Add

(5 posts)
  • Started 3 years ago by DataGnostics
  • Latest reply from DataGnostics
  • This topic is resolved
  1. I do not have a clear picture in my mind of which kind of function-verb to use for a customization. This is what I mean:

    I have, in my theme's function.php:

    function mytheme_thing () {
        something that will be different for my theme;
       }

    Assume that there's a function thematic_thing(). I don't have a good sense of when to use apply_filters, add_filter, add_action, or what, to attach mytheme_thing to thematic_thing. Sometimes I want mytheme_thing to replace all of thematic_thing, sometimes I want it to be in addition to themetic_thing, sometimes I want to replace part of thematic_thing.

    Right now I'm specifically trying to add a javascript onclick function to h2 class="entry-title" on certain pages, but while I can replace thematic_postheader_posttitle, I want to merely *add* something, instead. I need a better cookbook.

    Posted 3 years ago #
  2. check out the thematic guide for a decent explanation of hooks and filters.

    http://themeshaper.com/thematic/guide/

    since the writing of the guide thematic has evolved to also include override functions. following your example of a function thematic_thing(). you would only need to define a function named childtheme_override_thing() in your functions.php and thematic will automagically use it in place of the original.

    adding, removing and filtering are still the same as defined in the guide. I like to think of filtering any time the original function returns a variable.... such as $content or $something. Maybe I only want to add "bacon" in front of what is already there. I'd use a filter.

    function add_bacon($content){
      $content .= "bacon";
      return $content;
    }
    add_filter('function_to_be_filtered','add_bacon');

    adding and removing is more like putting coats on pegs. If i want to add a coat (function) to a specific peg then i'd use add_action with the appropriate hook name

    add_action('thematic_hook_name','my_function_name',option_position_number);
    Posted 3 years ago #
  3. Still not clear to me, so I shall be more specific.

    In thematic content-extensions there is the following code:

    function thematic_postheader_posttitle() {
    
    	    if (is_single() || is_page()) {
    	        $posttitle = '<h1 class="entry-title">' . get_the_title() . "</h1>\n";
    	    } elseif (is_404()) {
    	        $posttitle = '<h1 class="entry-title">' . __('Not Found', 'thematic') . "</h1>\n";
    	    } else {
    	        $posttitle = '<h2 class="entry-title"><a href="';

    I want to basically change the last two lines here, to be:

    } else {
    	        $posttitle = '<h2 class="entry-title"';
                    if ([certain conditions are met]){
    			$posttitle .= ' [onclick my_custom_function]';
    		} $posttitle .= '"><a href="';

    but I don't know how to put it in there in mid-thematic-function.

    Posted 3 years ago #
  4. well ask a vague question, get a vague answer. ;)

    what you are aiming to do is best solved with a filter, since you don't want to change everything. i didn't test this, but try:

    my_postheader_posttitle($posttitle) {
    
    	if ([certain conditions are met]){
    			$posttitle .= ' [onclick my_custom_function]';
            }
      return $posttitle;
    }
    add_filter('thematic_postheader_posttitle','my_postheader_posttitle');

    i'm not sure whether it should be

    $posttitle .= [functions];

    or

    $posttitle = [functions];

    b/c i still don't know exactly what you are trying to do. just be aware that .= means it will tack [functions] onto the end of what already exists for $posttitle... whereas a straight = means you are wiping out $posttitle in favor of [functions]

    }

    Posted 3 years ago #
  5. I ended up writing a childtheme_override_postheader_posttitle, because either my function was replacing thematic's posttitle, or it was being tacked on the end, and I wanted it in the middle.

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.