• I’m confused about filter hooks

    You pass in a value and you pass out a value
    But, how do you know what value to pass in?

    AND: am I right in saying that you don’t need to actually pass in a value – since you are just using global variables??

    Global variables: how do I know which one to use??

    In plugins I’ve looked at, they use $content

    What other global variables like $content are there?

    If I was writing a plugin to change the_title for example, what global variable would I use?

    Thanks!!

    Omar

Viewing 5 replies - 1 through 5 (of 5 total)
  • WordPress assigns the parameters to the filters – you don’t get to choose. The input parameter is the unmodified value of the data that the filter is assigned to. the_title will get $title. the_content will get $content, etc.

    You can also access any global variable by declaring it in the function you define. You can even declare your own global variables to use in the filter functions. That is a way to pass your own data to a filter.

    See this thread for an example where one filter sets a value for use by another.

    Thread Starter OM2

    (@om2)

    thanks for the reply – much appreciated

    i had a look at the link u gave

    they have:

    add_filter(‘pre_user_login’, ‘sd_new_login_filter’);
    add_filter(‘pre_user_display_name’, ‘sd_substitute_displayname_filter’);

    for the first one, the variable dealt with is $login
    for the second one, the variable dealt with is $display_name

    is the convention that everything that comes after the first undersocre is the name of the relevant variable?

    i.e. hookName_abc_def, then the variable concerend would be $abc_def?

    where is this documented?
    i’ve read most pages (but could have easily forgotten that i’ve read it if it does exist!)

    besides, i’m sure i’ve seen other hooks, whether output has been $content??

    still confused. i’m sure the answer is very simple

    thanks!

    I have not found any documentation, either. You can get some help by reading the code in various modules. I usually just put code in the function to dump out the input to see what is there.

    Thread Starter OM2

    (@om2)

    ok
    so i’m not going mad!
    (or maybe it’s the 2 of us that is mad?)
    i think that’s an inconsistency

    in one of my questions u have replied to, i was trying to change the look of a page link in the navigation menu

    amongst other things i have:

    add_filter(‘wp_list_pages’, ‘colour_links’);

    now, the variable i was using was $wp_list_pages
    BUT: is this the correct one?? doesit even exist!?

    should i instead be using $list_pages?

    thanks

    It is confusing! The name of the variable in your function colour_links($wp_list_pages) is the same as the name of a function, but it is only a name. You could just as well have written function colour_links($anything), and used $anything inside the function.

    function colour_links( $anyname )
    {
    	$stringToLookFor = ">My Dummy<";
    	$stringToReplace = "><strong>My Dummy</strong><";
    
    	$anyname = str_replace($stringToReplace, $stringToLookFor, $anyname);
    	return $anyname;
    }
    
    add_filter('wp_list_pages', 'colour_links');
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Question about filter hooks $content’ is closed to new replies.