0

I can't find a way to properly insert wp_nonce field, without braking the code. The code below is from a class function that sends back string, which will be echoed.

    $end_data .= '<input type="text" class="ss_title_form_ajax_' . $this->id . '"     limit="133" value="' . $this->native_title . '" size = "60" placeholder=""/>';
    $end_data .= '<span class="ss_title_form_ajax_' . $this->id . '"></span>';
    $end_data .= '<span id="ShowMe_' . $this->id . '">' . ( 60 - strlen( $this->native_title ) ) . '</span>';
    //-------------------------
    foreach ( $this->have_results as $keywords_rf ) {
        $end_data .= '<span class="user_select_rf" data-role="' . $this->id . '">' . $keywords_rf . '   </span>';
    }
    //-------------------------
    return $end_data . '</form>';

How to add the wp_nonce in these circumstances?

0

You use wp_nonce_field with the fourth parameter set to false. This way you can get the nonce field instead of echoing it:

$end_data .= wp_nonce_field( "name-of-action", "name-for-the-form-field", true, false );

$end_data .= '<input type="text" class="ss_title_form_ajax_' . $this->id . '"     limit="133" value="' . $this->native_title . '" size = "60" placeholder=""/>';
$end_data .= '<span class="ss_title_form_ajax_' . $this->id . '"></span>';
$end_data .= '<span id="ShowMe_' . $this->id . '">' . ( 60 - strlen( $this->native_title ) ) . '</span>';
//-------------------------
foreach ( $this->have_results as $keywords_rf ) {
    $end_data .= '<span class="user_select_rf" data-role="' . $this->id . '">' . $keywords_rf . '   </span>';
}
//-------------------------
return $end_data . '</form>';

If you can not modified the code if the form class, you can use wp_nonce_field as described about and insert it on the form string with any appropiate PHP method. For example with substr_replace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.