• Resolved kostas45

    (@kostas45)


    Hi,

    Providing a hook would be very helpful.
    For example, a hook on “Save settings” would help to change the state of a user custom field, eg user-active.

    Is there a chance of providing us with such a hook? Please?

    Thank you,
    Kostas

Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Author edward_plainview

    (@edward_plainview)

    I don’t quite see where I could provide an action for you to hook into. You mean the protect passwords plugin settings, when those are saved?

    If you provide me with some code (modify the plugin and send it to me?) then I can add it if you wish. [email protected]

    Thread Starter kostas45

    (@kostas45)

    Thank you for responding.

    Yes, I mean when the protect passwords plugin settings are saved.
    Unfortunately, I am not yet familiar with plugin development, so I cannot provide code, thanks anyway!

    I was hoping to include in my functions.php something like:

    
    add_action('plainview_save_data', 'save_data_action', 10);
    function save_data_action(...) {
    ... Set user-active field to 0 ...
    }
    

    Best regards,
    Kostas

    • This reply was modified 6 years, 6 months ago by kostas45.
    Plugin Author edward_plainview

    (@edward_plainview)

    How about

    do_action( ‘plainview_protect_passwords_settings_saved’, $form );

    The $form being the html form object from which you can extract the contents of the settings. I would insert that on line 102 of the src / traits / admin_menu.php file.

    Thread Starter kostas45

    (@kostas45)

    Sounds good!

    If you implement it (and provide some how-to guidelines) I can test it.

    Thanks,
    Kostas

    Plugin Author edward_plainview

    (@edward_plainview)

    That line you can add to the code yourself and do whatever you need to do with the $form object.

    To see how to interact with the form, just look a few lines higher up in the same file.

    Is there anything you need other than that, more specifically?

    Thread Starter kostas45

    (@kostas45)

    OK, I will test it later today and respond here.

    Thanks,
    Kostas

    Thread Starter kostas45

    (@kostas45)

    Well, this is too advanced for me!
    I just need a way to loop through Protected users (and then update a user field for each user), in my theme’s functions.php.
    Any guidance is welcome.

    Thanks,
    Kostas

    Plugin Author edward_plainview

    (@edward_plainview)

    Ah. Try this:

    $input = $form->input( ‘protected_users’ );
    $protected_users = $input->get_post_value();
    foreach( $protected_users … )

    Thread Starter kostas45

    (@kostas45)

    OK, I added:
    do_action('plainview_protect_passwords_settings_saved', $form);
    in line 101, just before
    $this->message( 'Options saved!' );

    Then in my theme’s functions.php, I added:

    add_action('plainview_protect_passwords_settings_saved', 'func_plainview_protect_passwords_settings_saved', 10);

    function func_plainview_protect_passwords_settings_saved ($form) {
    $input = $form->input('protected_users');
    $protected_users = $input->get_post_value();

    foreach($protected_users as $protected_user ) :
    update_user_meta($protected_user->user_id, 'wpcf-user-active', '0');
    endforeach;
    }

    which has no effect. Moreover, wpcf-user-active custom field value, should alternate between 0 and 1, depending on the user being checked to be protected or not.

    Any more ideas?

    Thanks again,
    Kostas

    Plugin Author edward_plainview

    (@edward_plainview)

    The $protected_user array is an array of user IDs already, so:

    update_user_meta($protected_user, 'wpcf-user-active', '0');

    Thread Starter kostas45

    (@kostas45)

    Yes, it works now, thanks!

    One last bit please. In the foreach, if user was not protected, I need:
    update_user_meta($protected_user, 'wpcf-user-active', '0');
    If user was protected and now is not, I need:
    update_user_meta($protected_user, 'wpcf-user-active', '1');
    How should I implement this?

    Thanks again,
    Kostas

    Plugin Author edward_plainview

    (@edward_plainview)

    Ooh… That’s … more difficult. Perhaps if we didn’t use the $form and instead consulted with the plugin directly? Start by removing the do_action line you added.

    Then, in the same file, on line 98 add

    do_action( 'plainview_protect_passwords_pre_save_settings', $this );

    and line 104

    do_action( 'plainview_protect_passwords_post_save_settings', $this );

    The result should be the pre action, then three lines of saving options using update_site_option, and then the post action.

    Retrieve the array of protected user IDs using:

    function my_plainview_protect_passwords_pre_save_settings( $plugin )
    {
     $user_ids = $plugin->get_site_option( 'protected_users' );
     foreach( $user_ids as $user_id )
      ...
    }

    So pre is before the options are saved, so you can set user active 0, and then post is when you can set 1.

    Thread Starter kostas45

    (@kostas45)

    Excellent, it works!
    To recap:

    
    add_action('plainview_protect_passwords_pre_save_settings', 'my_plainview_protect_passwords_pre_save_settings', 10);
    function my_plainview_protect_passwords_pre_save_settings( $plugin ) {
    	$user_ids = $plugin->get_site_option( 'protected_users' );
    	foreach( $user_ids as $user_id ) :
    		update_user_meta($user_id, 'wpcf-user-active', '1');
    	endforeach;
    }
    add_action('plainview_protect_passwords_post_save_settings', 'my_plainview_protect_passwords_post_save_settings', 10);
    function my_plainview_protect_passwords_post_save_settings( $plugin ) {
    	$user_ids = $plugin->get_site_option( 'protected_users' );
    	foreach( $user_ids as $user_id ) :
    		update_user_meta($user_id, 'wpcf-user-active', '0');
    	endforeach;
    }
    

    So, can you please include those two lines of code (in src/traits/admin_menu.php) in a plugin update?

    Thank you very much indeed,
    Kostas

    • This reply was modified 6 years, 6 months ago by kostas45.
    Plugin Author edward_plainview

    (@edward_plainview)

    Will do!

    Plugin Author edward_plainview

    (@edward_plainview)

    Included in v1.4 of the plugin.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Hooks?’ is closed to new replies.