如何在提交表格后显示正确的消息?

时间:2014-08-27 作者:Neil

我正在构建一个模板页面,用于提交配置文件更改。我想以绿色显示一条“正确”的消息(示例),让用户知道。

这里是我的模板代码:

<?php
global $avia_config;
/**
 * Template Name: User Profile
 *
 * Allow users to update their profiles from Frontend.
 *
 */

/* Get user info. */
global $current_user, $wp_roles;
get_currentuserinfo();

/* Load the registration file. */
require_once( ABSPATH . WPINC . \'/registration.php\' );
$error = array();
$correct = array();    
/* If profile was saved, update profile. */
if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) && $_POST[\'action\'] == \'update-user\' ) {

    /* Update user password. */
    if ( !empty($_POST[\'pass1\'] ) && !empty( $_POST[\'pass2\'] ) ) {
        if ( $_POST[\'pass1\'] == $_POST[\'pass2\'] )
            wp_update_user( array( \'ID\' => $current_user->ID, \'user_pass\' => esc_attr( $_POST[\'pass1\'] ) ) );
        else
            $error[] = __(\'The passwords you entered do not match.  Your password was not updated.\', \'profile\');
    }

    /* Update user information. */
    if ( !empty( $_POST[\'url\'] ) )
        update_user_meta( $current_user->ID, \'user_url\', esc_url( $_POST[\'url\'] ) );

    if ( !empty( $_POST[\'nickname\'] ) )
        update_user_meta( $current_user->ID, \'nickname\', esc_attr( $_POST[\'nickname\'] ) );

    if ( !empty( $_POST[\'description\'] ) )
        update_user_meta( $current_user->ID, \'description\', esc_attr( $_POST[\'description\'] ) );

    /* Redirect so the page will show updated info.*/
  /*I am not Author of this Code- i dont know why but it worked for me after changing below line to if ( count($error) == 0 ){ */
    if ( count($error) == 0 ) {
        $correct[] = __(\'Correct message\', \'profile\');
        //action hook for plugins and extra fields saving
        do_action(\'edit_user_profile_update\', $current_user->ID);
        wp_redirect( get_permalink() );
        exit;
    }
}
    /*
     * get_header is a basic wordpress function, used to retrieve the header.php file in your theme directory.
     */
     get_header();


     if( get_post_meta(get_the_ID(), \'header\', true) != \'no\') echo avia_title();
     ?>

        <div class=\'container_wrap container_wrap_first main_color <?php avia_layout_class( \'main\' ); ?>\'>

            <div class=\'container\'>

                <main class=\'template-page content  <?php avia_layout_class( \'content\' ); ?> units\' <?php avia_markup_helper(array(\'context\' => \'content\',\'post_type\'=>\'page\'));?>>

                    <?php
                    /* Run the loop to output the posts.
                    * If you want to overload this in a child theme then include a file
                    * called loop-page.php and that will be used instead.
                    */
                    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div id="post-<?php the_ID(); ?>">
        <div class="entry-content entry">
            <?php the_content(); ?>
            <?php if ( !is_user_logged_in() ) : ?>
                    <p class="warning">
                        <?php _e(\'You must be logged in to edit your profile.\', \'profile\'); ?>
                    </p><!-- .warning -->
            <?php else : ?>

                <form method="post" id="adduser" action="<?php the_permalink(); ?>">
                    <p class="nickname">
                        <label for="nickname"><?php _e(\'Nickname *\', \'profile\'); ?></label>
                        <input class="text-input" name="nickname" type="text" id="nickname" value="<?php the_author_meta( \'nickname\', $current_user->ID ); ?>" />
                    </p><!-- .form-nickname -->
                    <p class="username">
                        <label for="user-login"><?php _e(\'Username (Not editable)\',\'profile\')?></label>
                        <input class="text" name="username" type="text" id="username" readonly value="<?php the_author_meta( \'user_login\', $current_user->ID ); ?>"/>
                    </p>
                   <!-- <p class="form-username">
                        <label for="last-name"><?php _e(\'Last Name\', \'profile\'); ?></label>
                        <input class="text-input" name="last-name" type="text" id="last-name" value="<?php the_author_meta( \'last_name\', $current_user->ID ); ?>" />
                    </p>--><!-- .form-username -->
                    <p class="form-email">
                        <label for="email"><?php _e(\'E-mail (Not editable)\', \'profile\'); ?></label>
                        <input class="text-input" name="email" type="text" id="email" readonly value="<?php the_author_meta( \'user_email\', $current_user->ID ); ?>" />
                    </p><!-- .form-email -->
                    <p class="form-url">
                        <label for="url"><?php _e(\'Website\', \'profile\'); ?></label>
                        <input class="text-input" name="url" type="text" id="url" value="<?php the_author_meta( \'user_url\', $current_user->ID ); ?>" />
                    </p><!-- .form-url -->
                    <p class="form-password">
                        <label for="pass1"><?php _e(\'Password *\', \'profile\'); ?> </label>
                        <input class="text-input" name="pass1" type="password" id="pass1" />
                    </p><!-- .form-password -->
                    <p class="form-password">
                        <label for="pass2"><?php _e(\'Repeat Password *\', \'profile\'); ?></label>
                        <input class="text-input" name="pass2" type="password" id="pass2" />
                    </p><!-- .form-password -->
                    <p class="form-textarea">
                        <label for="description"><?php _e(\'Biographical Information\', \'profile\') ?></label>
                        <textarea name="description" id="description" rows="3" cols="50"><?php the_author_meta( \'description\', $current_user->ID ); ?></textarea>
                    </p><!-- .form-textarea -->

                    <?php 
                        //action hook for plugin and extra fields
                        do_action(\'edit_user_profile\',$current_user); 
                    ?>
                     <?php if ( count($error) > 0 ) echo \'<p class="error" style="color: red">\' . implode("<br />", $error) . \'</p>\'; ?>
                    <?php if ( count($correct) > 0 ) echo \'<p class="correct" style="color: green">\' . implode("<br />", $correct) . \'</p>\'; ?>

                    <p class="form-submit">
                        <?php echo $referer; ?>
                        <input name="updateuser" type="submit" id="updateuser" class="submit button" value="<?php _e(\'Update\', \'profile\'); ?>" />
                        <?php wp_nonce_field( \'update-user\' ) ?>
                        <input name="action" type="hidden" id="action" value="update-user" />
                    </p><!-- .form-submit -->
                </form><!-- #adduser -->
            <?php endif; ?>
        </div><!-- .entry-content -->
    </div><!-- .hentry .post -->
    <?php endwhile; ?>
<?php else: ?>
    <p class="no-data">
        <?php _e(\'Sorry, no page matched your criteria.\', \'profile\'); ?>
    </p><!-- .no-data -->
<?php endif; 
                    $avia_config[\'size\'] = avia_layout_class( \'main\' , false) == \'entry_without_sidebar\' ? \'\' : \'entry_with_sidebar\';
                    get_template_part( \'includes/loop\', \'page\' );
                    ?>

                <!--end content-->
                </main>

                <?php

                //get the sidebar
                $avia_config[\'currently_viewing\'] = \'page\';
                get_sidebar();

                ?>

            </div><!--end container-->

        </div><!-- close default .container_wrap element -->



<?php get_footer(); ?>
已解决。

3 个回复
最合适的回答,由SO网友:Neil 整理而成

我解决了这个问题。首先检查是否有错误,然后添加如下自定义重定向url:

if ( count($error) == 0 ) {
     wp_redirect( get_permalink() . \'?updated=true\' );
}
最后,我使用“GET”方法添加了一条消息:

<?php if ( $_GET[\'updated\'] == \'true\' ) : ?> <p class="form-submit-success" style="color: green">Your profile has been updated</p>

SO网友:Racheal

Use this for admin

<div class="updated"><p><strong><?php _e(\'Total 12 Records Saved.\' ); ?></strong>    </p></div>
SO网友:Jasper

此页面是在前端还是在wp admin中?如果后者可以轻松使用admin_notices 为此:

admin_notices

结束

相关推荐

Front-page.php加载最近发布的帖子时出现问题

嗨,我有一个奇怪的问题,我有我的自定义头版。php带有设置页面(在阅读设置中)静态页面。我想要的是一个带有欢迎信息的静态页面,以及1篇来自帖子类型的最新帖子和4篇来自自定义帖子类型的最新帖子。要获取静态页面信息,我使用:<?php while ( have_posts() ) : the_post(); ?> <?php get_template_part( \'content\', \'page\' ); ?> <?php endwhile; // en