doudula1974 2019-05-03 20:19
浏览 76
已采纳

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

Okay, I'm new the PHP writing, and i got stuck.

I have this code:

    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>';
}

It's one Wordpress shortcode. I want after the form submited and the database updated, refresh the page. Since this code is currently down and the page is not updated (if i'm refresh the page manually the database updated, and the changes is appear). I try the onSubmit="window.location.reload()", action="" codes, but doesn't work.

  • 写回答

2条回答 默认 最新

  • du5114 2019-05-04 05:10
    关注

    Hey Meil Helt please try to replace your code with my below code.

    add_action("init","start_ob_start_cb");
    function start_ob_start_cb()
    {
         ob_start();
    }
    
    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;
    wp_redirect(get_permalink()); die();
        }
    
        // 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>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示