Forums

how to get arguments from action hooks (4 posts)

  1. ekitel
    Member
    Posted 3 years ago #

    so I'm reading that action hooks pass arguments to the function that you hook to the action, but how does it happen? I mean how do I access the argument in my function?

    this code doesn't get it

    function remove_post_dir($post_ID) {
      if(file_exists(ABSPATH. '/wp-content/uploads/'. $post_ID)) {
        rmdir(ABSPATH. '/wp-content/uploads/'. $post_ID);
      }
    }
    
    add_action( 'delete_post', remove_post_dir($post_ID));
  2. ekitel
    Member
    Posted 3 years ago #

    I used a bad example for add_action, it should be like this:

    add_action( 'delete_post', 'remove_post_dir');

  3. Otto
    Tech Ninja
    Posted 3 years ago #

    There's nothing wrong with your code there, as such. Try this for example:

    function remove_post_dir($post_ID) {
    echo "I JUST GOT $post_ID";
    }
    add_action('delete_post','remove_post_dir');

    That will work fine. The default setting is to receive 1 argument.

    If you wanted to be explicit, you could use:
    add_action('delete_post','remove_post_dir',10,1);
    which tells it that you have priority 10 and want to receive 1 argument.

  4. ekitel
    Member
    Posted 3 years ago #

    problem was my function wasn't in quotes, and had the parentheses like a regular function call, I copied that style out of an older plugin

Topic Closed

This topic has been closed to new replies.

About this Topic