tags:

views:

200

answers:

3

can I do something like that? to pass arguments to my function? I already studied add_action doc but did not figure out how to do it. What the exact syntax to pass two arguments would look like. In particular how to pass text & integer arguments.

function recent_post_by_author($author,$number_of_posts) {
  some commands;
}
add_action('thesis_hook_before_post','recent_post_by_author',10,'author,2')

UPDATE

it seems to me that it is done somehow through do_action but how? :-)

+1  A: 

Yes, you can (but the documentation for it is horrible).

See here: http://frumph.net/wordpress/understanding-do_action-and-add_action/

hollsk
the link you provided is good. It put some light into 'how' but still do not know exactly how :-)
Radek
Man, I'm a noob. No code in comments :p Have added a new answer instead.
hollsk
+1  A: 

I've wrote wordpress plugin long time ago, but I went to Wordpress Codex and I think that's possible: http://codex.wordpress.org/Function_Reference/add_action

<?php add_action( $tag, $function_to_add, $priority, $accepted_args ); ?> 

I think you should pass them as an array. Look under examples "take arguments".

Bye

dierre
Radek
Oh, I see. I didn't notice it must be an (int). Sorry. Well, don't you pass arguments through do_action_ref_array usually? I mean you first declare you action and then you use it with do_action.
dierre
I can probably code it differently but if I want to reuse the function somewhere else it would be nice to be able pass some arguments to it, wouldn't it be
Radek
Ok, but the add_action function, for what I can understand reading more carefully the Codex, is just a way to hook up your function to the system through an action, so I don't think is in its design to accept more arguments. In your definition of the function you can use parametric parameters but then you have to pass actual parameter using do_action or do_action_ref_array.I can be wrong of course. But if there is a way, I'm sorry, I don't know it.
dierre
+1  A: 

Basically the do_action is placed where the action should be executed, and it needs a name plus your custom parameters.

When you come to call the function using add_action, pass the name of your do_action() as your first argument, and the function name as the second. So something like:

function recent_post_by_author($author,$number_of_posts) { some commands; } add_action('get_the_data','recent_post_by_author',10,'author,2');

// this is where it's executed do_action('get_the_data',$author,$number_of_posts);

Should hopefully work.

hollsk
@hollsk: I tried what you suggested but do_action. I believe that do_action is handled by Thesis and I shouldn't touch that...
Radek
So I take it thesis_hook_before_post is a custom hook you've added that's already tied to an action? Is it worth combining your actions, or adding an additional one? Sorry this isn't getting you closer to an answer very quickly, but code in the WP hooks system tends to get really muddled really quickly so it's difficult to pick apart what's needed.
hollsk
I do not know how to add some action to existing Thesis one. I'd say it's not worth to do
Radek