admin管理员组

文章数量:1323729

I have a page with a list of webinars, and a shortcode that displays a form button labeled "Enroll". The form should simply update the user's WordPress profile and redirect them to a page. I don't understand why I'm not getting this to work:

/*
 * Usage: [enroll_form_button redirect="#" webinar="ABC"]
 */
if (!function_exists('enroll_form_button_function')) {
    function enroll_form_button_function($atts){
        $atts = shortcode_atts(array('redirect' => '#', 'webinar' => '',), $atts);
        $redirect_to = $atts['redirect'];
        $webinar_code = $atts['webinar'];
        
        $results .='<form id="form-'.$webinar_code.'" method="post" action="'.$redirect_to.'">
            <input type="submit" value="Enroll" name="form-'.$webinar_code.'" >
        </form>';
        
        if (isset($_POST['form-'.$webinar_code])){
            update_user_meta( get_current_user_id(), $webinar_code, 'true');
        }
        return $results;
    }
    add_shortcode('enroll_form_button', 'enroll_form_button_function');
} else {
    echo "enroll_form_button_function is not available.<br />\n";
}

The parameters, the redirect, and the function to update the user's profile is working fine, but the if-statement doesn't seem to be working. If I take the function out of the if-statement, it updates all webinars in the user's profile, not just the one they are submitting. So, I need to identify the form and only call the action to that form. What am I missing here?

I have a page with a list of webinars, and a shortcode that displays a form button labeled "Enroll". The form should simply update the user's WordPress profile and redirect them to a page. I don't understand why I'm not getting this to work:

/*
 * Usage: [enroll_form_button redirect="#" webinar="ABC"]
 */
if (!function_exists('enroll_form_button_function')) {
    function enroll_form_button_function($atts){
        $atts = shortcode_atts(array('redirect' => '#', 'webinar' => '',), $atts);
        $redirect_to = $atts['redirect'];
        $webinar_code = $atts['webinar'];
        
        $results .='<form id="form-'.$webinar_code.'" method="post" action="'.$redirect_to.'">
            <input type="submit" value="Enroll" name="form-'.$webinar_code.'" >
        </form>';
        
        if (isset($_POST['form-'.$webinar_code])){
            update_user_meta( get_current_user_id(), $webinar_code, 'true');
        }
        return $results;
    }
    add_shortcode('enroll_form_button', 'enroll_form_button_function');
} else {
    echo "enroll_form_button_function is not available.<br />\n";
}

The parameters, the redirect, and the function to update the user's profile is working fine, but the if-statement doesn't seem to be working. If I take the function out of the if-statement, it updates all webinars in the user's profile, not just the one they are submitting. So, I need to identify the form and only call the action to that form. What am I missing here?

Share Improve this question edited Sep 14, 2020 at 16:36 Michael asked Sep 14, 2020 at 16:17 MichaelMichael 2811 silver badge14 bronze badges 1
  • why would $webinar_code even exist on the page that receives the form? That's a shortcode attribute so wont exist on that page. Therefore your if doesn't know what to look for. – vancoder Commented Sep 14, 2020 at 18:05
Add a comment  | 

1 Answer 1

Reset to default 0

Few problems I see here.

1.) You're using concatenation for $results (ie $results .= should be $results =)

2.) You're assuming that $webinar_code will always be available. If the page they are redirected to is not the same exact page, that will not be the case, or if the page redirected to has the shortcode but different webinar in attributes, will update the wrong one.

I would instead do it like this:

add_shortcode( 'enroll_form_button', 'enroll_form_button_function' );

function enroll_form_button_function( $atts ) {

    $atts         = shortcode_atts( array( 'redirect' => '#', 'webinar' => '', ), $atts );
    $redirect_to  = $atts['redirect'];
    $webinar_code = $atts['webinar'];

    $results = "<form id=\"form-{$webinar_code}\" method=\"post\" action=\"{$redirect_to}\"><input type=\"submit\" value=\"{$webinar_code}\" name=\"webinar_code\" /></form>";

    return $results;
}

add_action( 'wp', 'check_enroll_form_submit' );

function check_enroll_form_submit(){

    if( ! isset( $_POST['webinar_code'], $_POST['webinar_nonce'] ) ){
        return;
    }

    $webinar_code = sanitize_text_field( $_POST['webinar_code'] );
    update_user_meta( get_current_user_id(), $webinar_code, 'true' );
}

I did a couple things here:

  1. Moved the check to update the user's meta to the wp action
  2. Sanitized the value sent in $_POST (even if you know it's secure, it's ALWAYS good to be in the habit of sanitizing any kind of $_POST $_GET or $_REQUEST data)
  3. Instead of passing the value in the name, just pass it in the value (i think you were overthinking that part)

本文标签: functionsPHP multiple formssame pageisset(POST) not working