WordPress.org

Ready to get started?Download WordPress

Forums

add_action passing parameters (15 posts)

  1. jaysonng
    Member
    Posted 6 years ago #

    from: http://codex.wordpress.org/Function_Reference/add_action

    function email_friends($post_ID)  {
       $friends = 'bob@example.org, susie@example.org';
       mail($friends, "sally's blog updated" , 'I just put something on my blog: http://blog.example.com');
       return $post_ID;
    }
    
    add_action('publish_post', 'email_friends');

    so how do I pass "$post_ID" to the email_friends function? I can't seem to find it anywhere on the net! help!

  2. hakre
    Member
    Posted 6 years ago #

    Does the action passes such a parameter? If yes, then it will be automatically passed. If not then why did you wrote it in your functions parameter list?

    Or do I get your question wrong?

  3. jaysonng
    Member
    Posted 6 years ago #

    this isn't my function. i got it from the link above.

    I'm just trying to understand how I could pass "$post_id" say, I want post number 20?

  4. jaysonng
    Member
    Posted 6 years ago #

    nobody? is this documented?

  5. hakre
    Member
    Posted 6 years ago #

    Jaysonng, it's pretty hard for me to help you. You do not write much and in detail of what exactly you want to do. You just copied and pasted some code and then, while not achieving what you want (and that is secret, you did'nt write down here clearly) you just simply mock about problems you get.

    To directly answer your question, you can pass any value to any PHP function by just passing it as a function parameter value.

    For Example:

    function myfunc($post_ID)  {
       return $post_ID;
    }

    To pass the value 20 as post_ID parameter to the function myfunc, the following code does this:

    myfunc(20);

    I'm pretty shure that is not what you are looking for. But I can not tell you any further - and I guess noone else can - because you need to describe in more detail what you like to achieve.

  6. chrismou
    Member
    Posted 6 years ago #

    Jaysonng

    I think he's asking the following (I hope, because I'm looking for the same information...!)

    In the following example:

    function email_friends($post_ID)  {
       $friends = 'bob@example.org, susie@example.org';
       mail($friends, "sally's blog updated" , 'I just put something on my blog: http://blog.example.com');
       return $post_ID;
    }
    
    add_action('publish_post', 'email_friends');

    You're specifying that during the publish_post action, you should run the email_friends function.

    But how do you pass the $post_ID parameter to the function? Theres nowhere to pass a parameter within add_action('publish_post', 'email_friends'). So where is it pulling the $post_ID from??

  7. chrismou
    Member
    Posted 6 years ago #

    Oops, sorry I meant to address that to hakre ;)

  8. mihu
    Member
    Posted 5 years ago #

    chrismou explained the question well.
    Could someone help to answer this question?
    I got the same issue too.

    Thanks in advance.

  9. renato_s
    Member
    Posted 5 years ago #

    I have the same doubt. From what I could understand from the Codex, each action API will pass a predetermined parameter to the new function created in the plugin.

    So, the "create_category" action will pass the category-id as parameter to the user-defined function that will hook to this action, and each of the other actions will behave much the same way. "http://codex.wordpress.org/Plugin_API/Action_Reference" lists the available actions and its supplied parameters.

    If that's the case, there is no way of sending another parameter; the function will have to do its job using the one that is passed automatically.

    Is that correct?

  10. Lloyd Budd

    Posted 5 years ago #

    so how do I pass "$post_ID" to the email_friends function? I can't seem to find it anywhere on the net! help!

    do_action( 'email_friends', $post_ID );

  11. Roger Theriault
    Member
    Posted 5 years ago #

    @renato_s that's right. Although, if you KNOW what globals are available at that time, you can access them...

    unfortunately publish_post is hard to research, but your email function should be written with two parameters, like this (the name of the params doesn't exactly matter of course):

    email_friends($post_ID, $post) {
    ...
    }

    where $post_ID is an integer and $post is your familiar $post object that accesses the fields directly.

    See here: http://adambrown.info/p/wp_hooks/hook/$%7Bnew_status%7D_$post-%3Epost_type just follow the code link to see the actual WordPress code. That's the best way to learn WordPress!

    Here's a full list of hooks and filters:

    http://adambrown.info/p/wp_hooks

    And to answer the older question "how do I pass a post id?" well if you know a specific one you probably don't need the hook. :-)

    The hooks are for people who want their code to be called every time a particular event happens in WordPress. In the case of this function, the code indicates that the event is when a post's status changes from something else to "publish". The example is to mail a notification to people every time a post is published.

    I hope that sheds some light on the mystery. This codex doc explains it in more detail: http://codex.wordpress.org/Plugin_API

  12. anoopu
    Member
    Posted 5 years ago #

    i want to check a widget control(egg. text area) by using a wp plugin. and i want to change it into editer. how its possible?

  13. Roger Theriault
    Member
    Posted 5 years ago #

    @anoopu: I see you've made several posts (which is good, since this thread is not the right place for a new topic), none answered... (which suggests you need to provide more - a lot more - detail). You may want to try searching, or provide more detail in the thread you started under "Plugins and Hacks".

  14. RickyV
    Member
    Posted 5 years ago #

    I'm looking for the same info.

    I want to add some extra fields on the edit User profile page and then I want to save the information on a separate table on the DB when the profile gets updated

    Example: let say I create this function for the new input field

    function my_field(){
    echo '<input type="text" name="new_field" id="cap-name" />';
    }

    Then I know I can use the edit_user_profile to add the extra field to the Edit User profile form

    add_action('edit_user_profile','my_field');

    Then I create a function to edit the DB:

    function update_db($my_value){
      $sql = 'INSERT INTO wp_my_table VALUES '.$my_value;
      $wpdb->query($sql);
    }

    Here is where I don't know what to do. How do I use the profile_update hook to execute the update_db? and more important, How do I pass the value of new_field to the update_db function.

    Thanks

  15. Roger Theriault
    Member
    Posted 5 years ago #

    Um, have you tried $_POST['new_field'] yet?

    (Don't forget to sanitize the user input before you run ANY query. See $wpdb->prepare here: http://codex.wordpress.org/WordPress_Coding_Standards.)

Topic Closed

This topic has been closed to new replies.

About this Topic