从动作开始,无需刷新页面

时间:2019-05-06 09:04:06

标签: php wordpress

好的,我有一个Wordpress网站,这是一个简码。基本上,这将更新数据库并刷新当前页面。但是我想要这个短代码的另一个版本,它不会重新加载页面。

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

我想确保单击按钮时,仅当手动重新加载时才应重新加载页面。我尝试了简单的POST,但没有办法提出解决方案。

1 个答案:

答案 0 :(得分:1)

使用AJAX方法,该方法将在单击“提交”按钮时触发。尝试在Google上搜索操作方法,如果您仍需要帮助,请回来找我们。