提交后从方法刷新当前页面

好的,我是新来的PHP写作者,但我被卡住了。


我有以下代码:


    add_shortcode( 'mycred_take', 'mycred_pro_render_take_shortcode' );

function mycred_pro_render_take_shortcode( $atts, $label = 'Give Away' ) {


    extract( shortcode_atts( array(

        'user_id' => '',

        'confirm' => '',

        'amount'  => '',

        'unique'  => 0,

        'ref'     => 'mycred_take',

        'entry'   => '%plural% lose',

        'ctype'   => 'mycred_default'

    ), $atts ) );


    if ( ! is_user_logged_in() || ! function_exists( 'mycred' ) ) return '';


    if ( $user_id == '' )

        $user_id = get_current_user_id();


    // Load essentials

    $user_id = absint( $user_id );

    $mycred = mycred( $ctype );


    // User is excluded = has no balance

    if ( $mycred->exclude_user( $user_id ) ) return '';


    // Unique check

    if ( $unique == 1 && $mycred->has_entry( $ref, 0, $user_id, '', $ctype ) ) return '';


    $balance = $mycred->get_users_balance( $user_id, $ctype );


    $output = '';


    // If button was pushed

    if ( isset( $_POST['mycred-take-points-token'] ) && wp_verify_nonce( $_POST['mycred-take-points-token'], 'mycred-deduct-points' . $ref . $ctype ) ) {


        // Deduct

        $mycred->add_creds(

            $ref,

            $user_id,

            0 - $amount,

            $entry

        );


        // Update balance

        $balance = $balance - $amount;


    }


    // Too low balance

    if ( $balance < $amount ) return '';


    return $output . '<form action=""  method="post" id="mycred-take-shortcode' . $ref . $ctype . '"><input type="hidden" name="mycred-take-points-token" value="' . wp_create_nonce( 'mycred-deduct-points' . $ref . $ctype ) . '" /><input type="submit"  class="button"  value="' . $label . '" /></form>';

}

这是一个Wordpress简码。我想要在表单提交和数据库更新后刷新页面。由于此代码当前关闭且页面未更新(如果我手动刷新页面,则数据库已更新,并且会显示更改)。我尝试了onSubmit =“ window.location.reload()”,action =“”代码,但没有用。


胡子哥哥
浏览 260回答 2
2回答

尚方宝剑之说

请尝试用下面的代码替换您的代码。add_action("init","start_ob_start_cb");function start_ob_start_cb(){&nbsp; &nbsp; &nbsp;ob_start();}add_shortcode( 'mycred_take', 'mycred_pro_render_take_shortcode' );function mycred_pro_render_take_shortcode( $atts, $label = 'Give Away' ) {&nbsp; &nbsp; extract( shortcode_atts( array(&nbsp; &nbsp; &nbsp; &nbsp; 'user_id' => '',&nbsp; &nbsp; &nbsp; &nbsp; 'confirm' => '',&nbsp; &nbsp; &nbsp; &nbsp; 'amount'&nbsp; => '',&nbsp; &nbsp; &nbsp; &nbsp; 'unique'&nbsp; => 0,&nbsp; &nbsp; &nbsp; &nbsp; 'ref'&nbsp; &nbsp; &nbsp;=> 'mycred_take',&nbsp; &nbsp; &nbsp; &nbsp; 'entry'&nbsp; &nbsp;=> '%plural% lose',&nbsp; &nbsp; &nbsp; &nbsp; 'ctype'&nbsp; &nbsp;=> 'mycred_default'&nbsp; &nbsp; ), $atts ) );&nbsp; &nbsp; if ( ! is_user_logged_in() || ! function_exists( 'mycred' ) ) return '';&nbsp; &nbsp; if ( $user_id == '' )&nbsp; &nbsp; &nbsp; &nbsp; $user_id = get_current_user_id();&nbsp; &nbsp; // Load essentials&nbsp; &nbsp; $user_id = absint( $user_id );&nbsp; &nbsp; $mycred = mycred( $ctype );&nbsp; &nbsp; // User is excluded = has no balance&nbsp; &nbsp; if ( $mycred->exclude_user( $user_id ) ) return '';&nbsp; &nbsp; // Unique check&nbsp; &nbsp; if ( $unique == 1 && $mycred->has_entry( $ref, 0, $user_id, '', $ctype ) ) return '';&nbsp; &nbsp; $balance = $mycred->get_users_balance( $user_id, $ctype );&nbsp; &nbsp; $output = '';&nbsp; &nbsp; // If button was pushed&nbsp; &nbsp; if ( isset( $_POST['mycred-take-points-token'] ) && wp_verify_nonce( $_POST['mycred-take-points-token'], 'mycred-deduct-points' . $ref . $ctype ) ) {&nbsp; &nbsp; &nbsp; &nbsp; // Deduct&nbsp; &nbsp; &nbsp; &nbsp; $mycred->add_creds(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ref,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $user_id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 - $amount,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $entry&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; // Update balance&nbsp; &nbsp; &nbsp; &nbsp; $balance = $balance - $amount;wp_redirect(get_permalink()); die();&nbsp; &nbsp; }&nbsp; &nbsp; // Too low balance&nbsp; &nbsp; if ( $balance < $amount ) return '';&nbsp; &nbsp; return $output . '<form action=""&nbsp; method="post" id="mycred-take-shortcode' . $ref . $ctype . '"><input type="hidden" name="mycred-take-points-token" value="' . wp_create_nonce( 'mycred-deduct-points' . $ref . $ctype ) . '" /><input type="submit"&nbsp; class="button"&nbsp; value="' . $label . '" /></form>';}
打开App,查看更多内容
随时随地看视频慕课网APP