Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a class with this function to provide a user language based on $user_id

function provide_user_language($user_id) {

return apply_filters('get_user_language',$user_id);

}

So in my script, I can simply get the user language by doing this:

$user_language=$this->provide_user_language($user_id);

But I need to associate this $user_language to a "set_current_language". I can do this by doing an add filter hook, but add_filter does not accept parameters. This is the add_filter line with the function itself but I don't know how to pass the $user_id or the $user_language so that set_current_language will be set to $user_language:

add_filter('set_current_language', array(&$this,'provide_user_language')); 

I tried following this one but it won't work:

Passing a parameter to filter and action functions

I appreciate any tips. Probably there is an easy way.

share|improve this question
    
Have you already seen our "(WCM) User Language Switcher"-Plugin? Might be exactly what you need. –  kaiser Mar 5 '13 at 11:35
    
Nope, but I just solved this one..See my solution below. Thanks for the tip. –  Codex Meridian Mar 5 '13 at 11:48

2 Answers 2

The trick Toscho used was to get around the problem using Objects.

So lets say we have a filter named xyz, that passes in some post content. The goal of this hypothetical scenario is to append a word to the content that we can not "hardcode".

Here's the basic filter:

add_filter( 'xyz', 'test' );
function test( $content ) {
    return $content;
}

So we append to $content, but how do we get the value to append? That is the crux of your issue.

To solve this problem you can use OOP:

class test_object {
    public $appended_value = '';
    function test( $content ) {
        return $content.$this->appended_value;
    }
}

$obj = new test_object();
$obj->appended_value = 'hello world';
add_filter( 'xyz', array( $obj, 'test' ) );

Here the class/object is being used to store the extra data.

An alternative to this would be to use a closure ( not a lambda function ) to create a new function based on a value, but this will not work prior to PHP 5.3, e.g.:

add_filter('xyz', 
    function($content) use ($appended_value) {
        return $content.$appended_value;
    }
);

Disclaimer: None of this code is copy paste, it is for demonstrative purposes.

share|improve this answer
up vote 2 down vote accepted

Thanks Tom, Unfortunately Toscho concept didn't work for me, so I dig deeper in the add filter documentation here: http://codex.wordpress.org/Function_Reference/add_filter and they allow anonymous functions as callback.

This method finally solved my problem. This is how I resolve this one for other users that are experiencing this issue:

add_filter('set_current_language', 
    function($lang_set) use ($user_language) {
    $lang_set=$user_language;
    return $lang_set;
});

In this method, I pass the $user_language(which is available) to the add_filter hook. $lang_set is the dummy variable used to return the callback output.

I have finally set the "set_current_language" to the user language using add_filter.

share|improve this answer
2  
Gratulations, that you found the answer yourself :) Afaik, this only works with PHP 5.3, while WP requires only PHP 5.2.1 –  kaiser Mar 5 '13 at 11:50
    
Thanks for letting me know.. –  Codex Meridian Mar 5 '13 at 12:01
    
hmmm the only difference between this and my answer is that this answer doesn't mention several other methods of passing the parameter, but does have copy pasta, which skips over 'why' it works –  Tom J Nowell Aug 31 '13 at 20:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.