英文:
WordPress widget settings saving only some inputs
问题
我有这个小部件代码,它使用“使用Google登录”按钮来获取用户凭据,并最终获取他们的Google评论。按钮工作正常,我能够获取凭据和CSRF令牌。我尝试将它们保存在WordPress中以供以后使用,但这两个值不会保存,其他值会保存。
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Google Maps Reviews', 'text_domain' );
$place_id = ! empty( $instance['place_id'] ) ? $instance['place_id'] : '';
$api_key = ! empty( $instance['api_key'] ) ? $instance['api_key'] : '';
if ( !empty( $_POST['credential'] ) ) {
$google_credential = $_POST['credential'];
} else {
$google_credential = $instace['google_credential'];
echo "instance credential: ";
}
if ( !empty( $_POST['g_csrf_token'] ) ) {
$google_csrf_token = $_POST['g_csrf_token'];
} else {
$google_csrf_token = $instace['google_csrf_token'];
}
?>
<?php if(empty($_POST['credential'])){ ?>
<script src="https://accounts.google.com/gsi/client" async defer></script>
<?php } ?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'place_id' ) ); ?>"><?php _e( 'Place ID:' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'place_id' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'place_id' ) ); ?>" type="text" value="<?php echo esc_attr( $place_id ); ?>">
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'api_key' ) ); ?>"><?php _e( 'API Key:' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'api_key' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'api_key' ) ); ?>" type="text" value="<?php echo esc_attr( $api_key ); ?>">
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'google_credential' ) ); ?>"><?php _e( 'Google Credential:' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'google_credential' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'google_credential' ) ); ?>" type="text" value="<?php echo esc_attr( $google_credential ); ?>">
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'google_csrf_token' ) ); ?>"><?php _e( 'Google CSRF Token:' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'google_csrf_token' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'google_csrf_token' ) ); ?>" type="text" value="<?php echo esc_attr( $google_csrf_token ); ?>">
</p>
<?php if(empty($_POST['credential'])){ ?>
<div id="g_id_onload"
data-client_id="{removed for security}.apps.googleusercontent.com"
data-context="signin"
data-ux_mode="popup"
data-login_uri="https://{removed for security}/wp-admin/widgets.php"
data-auto_prompt="false">
</div>
<div class="g_id_signin"
data-type="standard"
data-shape="rectangular"
data-theme="filled_blue"
data-text="signin_with"
data-size="large"
data-locale="es-419"
data-logo_alignment="left">
</div>
<?php } ?>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';
$instance['place_id'] = ( ! empty( $new_instance['place_id'] ) ) ? sanitize_text_field( $new_instance['place_id'] ) : '';
$instance['api_key'] = ( ! empty( $new_instance['api_key'] ) ) ? sanitize_text_field( $new_instance['api_key'] ) : '';
$instance['google_credential'] = ( ! empty( $new_instance['google_credential'] ) ) ? sanitize_text_field( $new_instance['google_credential'] ) : '';
$instance['google_csrf_token'] = ( ! empty( $new_instance['google_csrf_token'] ) ) ? sanitize_text_field( $new_instance['google_csrf_token'] ) : '';
return $instance;
}
<details>
<summary>英文:</summary>
I have this widget code, which uses Sign In with Google button to get users credentials and eventually fetch their Google Reviews. Button works fine, and I'm able to get credentials and CSRF token. I'm trying to save them inside WordPress for later use, however this two values won't save, the other values do save.
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Google Maps Reviews', 'text_domain' );
$place_id = ! empty( $instance['place_id'] ) ? $instance['place_id'] : '';
$api_key = ! empty( $instance['api_key'] ) ? $instance['api_key'] : '';
if ( !empty( $_POST['credential'] ) ) {
$google_credential = $_POST['credential'];
} else {
$google_credential = $instace['google_credential'];
echo "instance credential: ";
}
if ( !empty( $_POST['g_csrf_token'] ) ) {
$google_csrf_token = $_POST['g_csrf_token'];
} else {
$google_csrf_token = $instace['google_csrf_token'];
}
?>
<?php if(empty($_POST['credential'])){ ?>
<script src="https://accounts.google.com/gsi/client" async defer></script>
<?php } ?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'place_id' ) ); ?>"><?php _e( 'Place ID:' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'place_id' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'place_id' ) ); ?>" type="text" value="<?php echo esc_attr( $place_id ); ?>">
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'api_key' ) ); ?>"><?php _e( 'API Key:' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'api_key' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'api_key' ) ); ?>" type="text" value="<?php echo esc_attr( $api_key ); ?>">
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'google_credential' ) ); ?>"><?php _e( 'Google Credential:' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'google_credential' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'google_credential' ) ); ?>" type="text" value="<?php echo esc_attr( $google_credential ); ?>">
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'google_csrf_token' ) ); ?>"><?php _e( 'Google CSRF Token:' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'google_csrf_token' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'google_csrf_token' ) ); ?>" type="text" value="<?php echo esc_attr( $google_csrf_token ); ?>">
</p>
<?php if(empty($_POST['credential'])){ ?>
<div id="g_id_onload"
data-client_id="{removed for security}.apps.googleusercontent.com"
data-context="signin"
data-ux_mode="popup"
data-login_uri="https://{removed for security}/wp-admin/widgets.php"
data-auto_prompt="false">
</div>
<div class="g_id_signin"
data-type="standard"
data-shape="rectangular"
data-theme="filled_blue"
data-text="signin_with"
data-size="large"
data-locale="es-419"
data-logo_alignment="left">
</div>
<?php } ?>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';
$instance['place_id'] = ( ! empty( $new_instance['place_id'] ) ) ? sanitize_text_field( $new_instance['place_id'] ) : '';
$instance['api_key'] = ( ! empty( $new_instance['api_key'] ) ) ? sanitize_text_field( $new_instance['api_key'] ) : '';
$instance['google_credential'] = ( ! empty( $new_instance['google_credential'] ) ) ? sanitize_text_field( $new_instance['google_credential'] ) : '';
$instance['google_csrf_token'] = ( ! empty( $new_instance['google_csrf_token'] ) ) ? sanitize_text_field( $new_instance['google_csrf_token'] ) : '';
return $instance;
}
答案1
得分: 1
你有两个小的拼写错误,应该是instance
(带有“N”)而不是instace
。
$google_credential = $instance['google_credential'];
echo "instance credential: ";
}
if (!empty($_POST['g_csrf_token'])) {
$google_csrf_token = $_POST['g_csrf_token'];
} else {
$google_csrf_token = $instance['google_csrf_token'];
}
我不能确定这将解决你所有的问题,但你应该从这里开始
英文:
You have two small typos of instace
where you would expect instance
(with "N")
$google_credential = $instace['google_credential'];
echo "instance credential: ";
}
if ( !empty( $_POST['g_csrf_token'] ) ) {
$google_csrf_token = $_POST['g_csrf_token'];
} else {
$google_csrf_token = $instace['google_csrf_token'];
I can't be sure this will solve all your problems but for certain you should begin here :}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论