add_action vs do_action

Hi supporter,
Before I ask how to can add captcha to my site, and I do it, but have problem.
When CP2 call do_action( 'coursepress_before_signup_validation' );
You don't pass 2 params via it, and when I do add_action( 'coursepress_before_signup_validation' ) $form_message_class , $form_message
, I assign 2params but it not effect to signup validate. Why and how can i do it?
Thank you very much.

  • Adam Czajczyk

    Hello phuongbinh,

    I hope you're well today and thank you for your question!

    As a general rule, the "do_action()" calls and executes actions that were added using "add_action()". "add_action()" must specify a hook (callback) custom function that "does something". Since this hook doesn't accept parameters you won't pass any to it.

    You would want to call it like that:

    function my_before_signup_validation_function() {
    
    /* do something here */
    
    }
    add_action('coursepress_before_signup_validation', 'my_before_signup_validation_function');

    Then you'd want to handle everything "captcha-related" right inside your "my_before_signup_validation_function()" function. In this particular case if you need that function to read any variables these variables would either need to be globalized or fetched via some additional function.

    That being said, I understand you're aiming to hook to that in order to display your captcha, is that right? Is it any particular captcha or your own solution? Can you point me to that captcha's implementation docs? I'd then take a look and see how we could add it up.

    Best regards,
    Adam

  • Adam Czajczyk

    Hello phoungbinh!

    I see you check if ( ! $form_errors ) {}, but in my hook, I can't assign value $form_errors++? it not global and your function signup no receive value?

    You can't. You need to either figure out a way to handle entire "captcha flow" inside the function (e.g. via ajax) or to look for a different solution. That is why I asked if you could point me to that particular captcha docs. Knowing how it works, I could find a way to hook it up.

    Can you tell me please which captcha solution are you trying to implement?

    Best regards,
    Adam

  • phuongbinh

    I using:

    add_action('coursepress_before_signup_form', array(__CLASS__, 'keyframe_coursepress_before_signup_form'));
    add_action('coursepress_after_all_signup_fields', array(__CLASS__, 'keyframe_add_captchar_signup'));
    add_action('coursepress_before_signup_validation', array(__CLASS__, 'keyframe_signup_validation'), 10, 2);
    static function keyframe_signup_validation() {
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!isset($_POST['g-recaptcha-response']) || empty($_POST['g-recaptcha-response'])) {
    $form_message = 'Vui lòng nh?p mã xác nh?n.';
    set_transient( 'signup_form_message', $form_message, 5 );
    wp_redirect('/courses-signup');
    exit();
    }
    else {
    $response = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', array(
    'body' => array(
    'secret' => '6LcDLBgUAAAAAL2BIOpfRYcwxTPwBnpPDlSbrzn3',
    'response' => $_POST['g-recaptcha-response'],
    )) );
    $result = json_decode($response['body']);
    $response_code = wp_remote_retrieve_response_code( $response );
    if ( 200 != $response_code) {
    $form_message = 'Mã xác nh?n không xác th?c ???c';
    set_transient( 'signup_form_message', $form_message, 5 );
    wp_safe_redirect('/courses-signup');
    exit();
    }
    else if ($result->success != true) {
    $form_message = 'Mã xác nh?n không ?úng';
    set_transient( 'signup_form_message', $form_message, 5 );
    wp_safe_redirect('/courses-signup');
    exit();
    }
    }
    }
    }

    static function keyframe_add_captchar_signup(){
    ?>
    <div class="g-recaptcha" data-sitekey="6LcDLBgUAAAAAISjIXlnq7avj7iMV5D8NwcJB-sx"></div>
    <?php
    }

    You give action but no give client to set error if have? I don't know hot it work? Please please flexible for new version, it take me more more time to do something, waiting a long time for ask and awser :slight_frown:
    Thank you very much.

  • Adam Czajczyk

    Hello phunogbinh!

    Thank you for sharing code. I see that you are already using "coursepress_before_signup_form" to display your form and that should be working fine.

    In your code however there shouldn't be "exit" function used as this would break the site. Instead the way would be to store (e.g. in transient or in some other form in db or in a cookie) the "status" of captcha validation and then if you need to stop signup you would want to hook up to some native WP action/filter e.g. "wp_authenticate":

    do_action_ref_array( 'wp_authenticate', array( &$credentials['user_login'], &$credentials['user_password'] ) );

    https://core.trac.wordpress.org/browser/tags/4.7.3/src/wp-includes/user.php#L58

    Hooks in our code are only for displaying form and we are using native wp_signon() function later so the "error handling" should be on a WP core hooks level. That was suggested by one of our developers.

    I hope that will let you proceed. Please note however that implementation of custom code is a bit beyond the scope of support forum and in case you needed it to be done for you, you might want to ask on our "Jobs & Pros" job board (please note: no WPMU DEV staff involved!) here:

    https://premium.wpmudev.org/wordpress-development/

    Kind regards,
    Adam

  • Adam Czajczyk

    Hello phuongbinh!

    The CoursePress only displays form but do not perform login/registration on its own. To actually login/register user it uses native WP functions. There are two ways to handle this that I suggested in my previous posts.

    I have consulted this with one of the CoursePress developers and he suggested the way explained in my most recent post here:

    https://premium.wpmudev.org/forums/topic/add_action-vs-do_action#post-1231656

    Please note: we're more than happy to provide you with all kind of WordPress-related assistance, especially if it comes to our own product. However, creation of custom captcha code is outside the scope of the forum as it's a custom development. If you need help with such coding/developement you might ask for it on our "Jobs & Pros" job board here:

    https://premium.wpmudev.org/wordpress-development/

    Kind regards,
    Adam

  • Dimitris

    Hello phuongbinh,

    hope you're doing good and don't mind chiming in here! :slight_smile:

    HI supporter, When click signup ( Enroll) in Course detail, it not show My custom Captchar while in signup page it show, I see my code in it.?

    I can see in your code snippet that you're using coursepress_before_signup_form action hook but I can't see the callback function keyframe_coursepress_before_signup_form.
    Maybe you have to utilize this function too? Or you simply left that out when you pasted your code? Please advise!

    Thank you very much. You need goto my site? please give me email, will send you user + pass to visit it.

    We won't be able to be too helpful with that as the nature of this custom development, but we can give it a try.
    There's no need to share credentials (keep in mind that this is a public and well-indexed by Google forum so you shouldn't post any sensitive data), you can grant us with support access to your website via WPMUDEV Dashboard plugin as described here:
    https://premium.wpmudev.org/docs/getting-started/getting-support/#chapter-4

    Warm regards,
    Dimitris

  • Dimitris

    Hey there phuongbinh,

    hope you're doing good today! :slight_smile:

    You may have noticed that I removed the site credentials from your previous reply.
    That happened because our support forums are public and well-indexed by Google crawlers.
    As these details were publicly available for some time now, I strongly advise to reset this username and password and share these with us via email.
    You can use our safe contact form https://premium.wpmudev.org/contact/#i-have-a-different-question using this template:

    Subject: "Attn: Dimitris"
    - private site credentials (username/password)
    - link back to this thread for reference
    - any other relevant urls

    Keep in mind the subject line as ensures that it gets assigned to me.

    Warm regards,
    Dimitris

  • Dimitris

    Hey there phuongbinh,

    hope you're doing good today! :slight_smile:

    I just had a better look inside but I wasn't able to pinpoint the cause of that. :slight_frown:

    As far as I can see, there are both hooks in shortcode views which are used in these CP modal windows for user registration.
    coursepress_before_signup_form can be located in /coursepress/2.0/include/coursepress/data/shortcode/class-template.php #L1360 and coursepress_after_all_signup_fields can be found few lines after at #L1447.

    As this thread already has surpassed the scope of our support forums though, I'd rather advise to use our Jobs & Pros section about it (no WPMUDEV staff members included):
    https://premium.wpmudev.org/wordpress-development/
    Reference: https://premium.wpmudev.org/docs/getting-started/getting-support/#chapter-7

    Warm regards,
    Dimitris

  • phuongbinh

    Hi supporter,
    If can, please help to me, And I can do it myself.
    I see new update from CP2 below for register process:

    public static function process_registration_form() {
    			if ( isset( $_POST['student-settings-submit'] ) && isset( $_POST['_wpnonce'] )
    				&& wp_verify_nonce( $_POST['_wpnonce'], 'student_signup' ) ) {
    
    				check_admin_referer( 'student_signup' );
    
    				/**
    				 * Trigger before validating registration form
    				 **/
    				do_action( 'coursepress_before_signup_validation' );
    
    				$min_password_length = apply_filters( 'coursepress_min_password_length', 6 );
    				$username = $_POST['username'];
    				$firstname = $_POST['first_name'];
    				$lastname = $_POST['last_name'];
    				$email = $_POST['email'];
    				$passwd = $_POST['password'];
    				$passwd2 = $_POST['password_confirmation'];
    				$redirect_url = $_POST['redirect_url'];
    				$found_errors = 0;

    I don't know why CP no using do_action_ref_array to transfer param $found_errors for developer pass errors to coursepress display.
    I want add new field and validate field it? I must using coursepress_before_signup_validation but YOU don't give me how to do it?
    Please give client can add and validate field easy, not close source.
    Thank you very much.