Support » Plugins » Hacks » Storing data from add_action and retreiving from different add_action

  • sd17

    (@sd17)


    I have two different actions being triggered. I need to store some results from the first action before the second action is triggered because I’m comparing the two.

    Not sure how I should go about doing this? I’ve read it’s a bad idea to use $_GLOBAL or $_SESSION.

    Any help is appreciated.

    Thanks!

    add_action ('trigger_1','function_1');
    
    function function_1($user_id){
       $var1 = get_user_meta( $user_id, 'custom_meta', 'true' );
    }
    
    /// User meta is changed before second action triggers ///
    
    add_action ('trigger_2','function_2');
    function function_2($user_id){
       $var1 = get_user_meta( $user_id, 'custom_meta', 'true' );
       if ($var1 != $var2) {
         wp_mail( 'test@localhost.local', 'subject', 'meta changed' );
       }
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • aut0poietic

    (@aut0poietic)

    Not sure I can give good advice without knowing more of what you’re doing, but to answer the question you’ve asked:

    If this is throw-away information and you don’t plan on using it again ( which your example hints at ) a global might be acceptable, so long as you’re storing a small bit of information and you properly namespace your variable. Using Your example:

    add_action ('trigger_1','function_1');
    
    function function_1($user_id){
       global $mypluginname_custom_meta ;
       $mypluginname_custom_meta = get_user_meta( $user_id, 'custom_meta', 'true' );
    }
    
    /// User meta is changed before second action triggers ///
    
    add_action ('trigger_2','function_2');
    function function_2($user_id){
       global $mypluginname_custom_meta;
       $var1 = get_user_meta( $user_id, 'custom_meta', 'true' );
       if ($var1 != $mypluginname_custom_meta) {
         wp_mail( 'test@localhost.local', 'subject', 'meta changed' );
       }
    }

    I hesitate to say “this is the way to go” only because is has a weird code-smell. That usually hints at an architectural issue. Just thinking it through, if your user_meta can change between 2 actions, that means there are 1 or more additional action hooks that can possibly alter the value. What are you doing in those actions to alter the value and why don’t you know about it?

    Would it be better to create non-primitive meta value ( such as an array or object ) that maintains state, change history or has a simple modified flag or timestamp?

    Another option might be to have two different user_meta keys and compare them in function_2.

    Hope this was of some help.

    sd17

    (@sd17)

    Thanks for the response. I thought about creating a second meta value, but I’d like to avoid it since it’s very temporary and I have a large user base.

    I am using a plugin that handles my user profiles (UPME). Unfortunately when I fire update_profile it doesn’t contain the new saved profile values yet. The plugin includes it’s own action upme_profile_update but there is support for $old_user_data.

    You are correct in the “weird code smell”. I’m not sure why they couldn’t of used profile_update, but I’m sure there’s a reason.

    From what I understand I must use $GLOBALS since I’m setting my variable inside of a function (I’m not the greatest developer if you couldn’t already tell). Using $GLOBALS[‘custom_user_meta’] works, and then I have unset it after I’m finished with it.

    Why is it bad practice to use declare global vars?

    Moderator bcworkz

    (@bcworkz)

    Globals are bad because the majority says they are 😉

    The subject of globals is a matter of opinion. This is mine, others will certainly disagree. The main problem is global use is an indicator of flawed architecture. They also are subject to name collisions and if not managed, can chew up memory. Some devs will create class structures to manage data that would otherwise require globals. Other than solving the name collision issue, this approach is just globals in camouflage.

    If you read through WP core code, you will see frequent use of globals, many outside of $GLOBALS. Not that this justifies their use. When we are hacking code, the data we need is not always conveniently available to our callback functions, so some sort of global or equivalent becomes necessary. Yes, there is an architectural flaw, but we don’t have control over the architecture, we just need to get something done.

    You don’t have to use $GLOBALS in functions, you can declare your own globals within functions. You can use globals anywhere, that’s why they are called global. In PHP, globals must be declared in every function they are used. As long as they are first declared before use, you can use them anywhere.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Storing data from add_action and retreiving from different add_action’ is closed to new replies.