【问题标题】:Refresh current page from method after submited提交后从方法刷新当前页面
【发布时间】:2019-05-04 13:10:55
【问题描述】:

好吧,我是 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="" 代码,但不起作用。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    Hey Meil​​ Helt 请尝试用我下面的代码替换您的代码。

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

    【讨论】:

      【解决方案2】:

      你可以在return之前添加它

      header("Refresh:0");
      

      【讨论】:

      • 如果我这样做,一旦出现短代码,页面就会重新加载。而不是点击表单按钮后。
      猜你喜欢
      • 2013-08-26
      • 2020-12-06
      • 1970-01-01
      • 2012-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多