Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am working on site. I uses some custom error/success messages for front end form but currently it display messages to on same page, But as soon as I refresh the page or redirect the user to next page messages not save. To save the success messages I use the code

$success = __('You have successfully Login.', 'frontendprofile');

And for the error messages I use the default varible that is $error. How can save them the for next page or for when if I refresh the page?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You could save the messages in a $_SESSION variable. This way, the values will be preserved untill you decide to remove them again.

function save_message( $type, $message = '' ) {
    $_SESSION['messages']['type'] = $message;
}

function get_messages() {
    $return = '';

    if ( isset( $_SESSION['messages'] ) && is_array( $_SESSION['messages'] ) ) {
        foreach( $_SESSION['messages'] as $type => $message ) {
            $return .= sprintf( '<p class="%1$s">%2$s</p>', $type, $message );
        }
    }

    if ( strlen( $return ) > 0 )
        return $return;

    return false;
}

function clean_messages( $type = false ) {
    if ( ! $type )
        $_SESSION['messages'] = array();

    else
        unset( $_SESSION['messages'][$type];
}

Something like this.
You can use it like follows:

save_message( 'success', __( 'You have successfully Login.', 'frontendprofile' ) );

if ( $messages = get_messages() ) {
    echo $messages;

    clean_messages();
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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