WordPress小部件设置仅保存部分输入。

huangapple go评论65阅读模式
英文:

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&#39;m able to get credentials and CSRF token. I&#39;m trying to save them inside WordPress for later use, however this two values won&#39;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[&#39;credential&#39;] ) ) {
$google_credential = $_POST[&#39;credential&#39;];
} else {			
$google_credential = $instace[&#39;google_credential&#39;];
echo &quot;instance credential: &quot;; 
}
if ( !empty( $_POST[&#39;g_csrf_token&#39;] ) ) {
$google_csrf_token = $_POST[&#39;g_csrf_token&#39;];
} else {
$google_csrf_token = $instace[&#39;google_csrf_token&#39;];
}
?&gt;
&lt;?php if(empty($_POST[&#39;credential&#39;])){ ?&gt;
&lt;script src=&quot;https://accounts.google.com/gsi/client&quot; async defer&gt;&lt;/script&gt;
&lt;?php } ?&gt;
&lt;p&gt;
&lt;label for=&quot;&lt;?php echo esc_attr( $this-&gt;get_field_id( &#39;title&#39; ) ); ?&gt;&quot;&gt;&lt;?php _e( &#39;Title:&#39; ); ?&gt;&lt;/label&gt; 
&lt;input class=&quot;widefat&quot; id=&quot;&lt;?php echo esc_attr( $this-&gt;get_field_id( &#39;title&#39; ) ); ?&gt;&quot; name=&quot;&lt;?php echo esc_attr( $this-&gt;get_field_name( &#39;title&#39; ) ); ?&gt;&quot; type=&quot;text&quot; value=&quot;&lt;?php echo esc_attr( $title ); ?&gt;&quot;&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;label for=&quot;&lt;?php echo esc_attr( $this-&gt;get_field_id( &#39;place_id&#39; ) ); ?&gt;&quot;&gt;&lt;?php _e( &#39;Place ID:&#39; ); ?&gt;&lt;/label&gt; 
&lt;input class=&quot;widefat&quot; id=&quot;&lt;?php echo esc_attr( $this-&gt;get_field_id( &#39;place_id&#39; ) ); ?&gt;&quot; name=&quot;&lt;?php echo esc_attr( $this-&gt;get_field_name( &#39;place_id&#39; ) ); ?&gt;&quot; type=&quot;text&quot; value=&quot;&lt;?php echo esc_attr( $place_id ); ?&gt;&quot;&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;label for=&quot;&lt;?php echo esc_attr( $this-&gt;get_field_id( &#39;api_key&#39; ) ); ?&gt;&quot;&gt;&lt;?php _e( &#39;API Key:&#39; ); ?&gt;&lt;/label&gt; 
&lt;input class=&quot;widefat&quot; id=&quot;&lt;?php echo esc_attr( $this-&gt;get_field_id( &#39;api_key&#39; ) ); ?&gt;&quot; name=&quot;&lt;?php echo esc_attr( $this-&gt;get_field_name( &#39;api_key&#39; ) ); ?&gt;&quot; type=&quot;text&quot; value=&quot;&lt;?php echo esc_attr( $api_key ); ?&gt;&quot;&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;label for=&quot;&lt;?php echo esc_attr( $this-&gt;get_field_id( &#39;google_credential&#39; ) ); ?&gt;&quot;&gt;&lt;?php _e( &#39;Google Credential:&#39; ); ?&gt;&lt;/label&gt; 
&lt;input class=&quot;widefat&quot; id=&quot;&lt;?php echo esc_attr( $this-&gt;get_field_id( &#39;google_credential&#39; ) ); ?&gt;&quot; name=&quot;&lt;?php echo esc_attr( $this-&gt;get_field_name( &#39;google_credential&#39; ) ); ?&gt;&quot; type=&quot;text&quot; value=&quot;&lt;?php echo esc_attr( $google_credential ); ?&gt;&quot;&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;label for=&quot;&lt;?php echo esc_attr( $this-&gt;get_field_id( &#39;google_csrf_token&#39; ) ); ?&gt;&quot;&gt;&lt;?php _e( &#39;Google CSRF Token:&#39; ); ?&gt;&lt;/label&gt; 
&lt;input class=&quot;widefat&quot; id=&quot;&lt;?php echo esc_attr( $this-&gt;get_field_id( &#39;google_csrf_token&#39; ) ); ?&gt;&quot; name=&quot;&lt;?php echo esc_attr( $this-&gt;get_field_name( &#39;google_csrf_token&#39; ) ); ?&gt;&quot; type=&quot;text&quot; value=&quot;&lt;?php echo esc_attr( $google_csrf_token ); ?&gt;&quot;&gt;
&lt;/p&gt;
&lt;?php if(empty($_POST[&#39;credential&#39;])){ ?&gt;
&lt;div id=&quot;g_id_onload&quot;
data-client_id=&quot;{removed for security}.apps.googleusercontent.com&quot;
data-context=&quot;signin&quot;
data-ux_mode=&quot;popup&quot;
data-login_uri=&quot;https://{removed for security}/wp-admin/widgets.php&quot;
data-auto_prompt=&quot;false&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;g_id_signin&quot;
data-type=&quot;standard&quot;
data-shape=&quot;rectangular&quot;
data-theme=&quot;filled_blue&quot;
data-text=&quot;signin_with&quot;
data-size=&quot;large&quot;
data-locale=&quot;es-419&quot;
data-logo_alignment=&quot;left&quot;&gt;
&lt;/div&gt;
&lt;?php } ?&gt;
&lt;?php 
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance[&#39;title&#39;] = ( ! empty( $new_instance[&#39;title&#39;] ) ) ? sanitize_text_field( $new_instance[&#39;title&#39;] ) : &#39;&#39;;
$instance[&#39;place_id&#39;] = ( ! empty( $new_instance[&#39;place_id&#39;] ) ) ? sanitize_text_field( $new_instance[&#39;place_id&#39;] ) : &#39;&#39;;
$instance[&#39;api_key&#39;] = ( ! empty( $new_instance[&#39;api_key&#39;] ) ) ? sanitize_text_field( $new_instance[&#39;api_key&#39;] ) : &#39;&#39;;
$instance[&#39;google_credential&#39;] = ( ! empty( $new_instance[&#39;google_credential&#39;] ) ) ? sanitize_text_field( $new_instance[&#39;google_credential&#39;] ) : &#39;&#39;;
$instance[&#39;google_csrf_token&#39;] = ( ! empty( $new_instance[&#39;google_csrf_token&#39;] ) ) ? sanitize_text_field( $new_instance[&#39;google_csrf_token&#39;] ) : &#39;&#39;;
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'];
}

我不能确定这将解决你所有的问题,但你应该从这里开始 WordPress小部件设置仅保存部分输入。

英文:

You have two small typos of instace where you would expect instance (with "N")

        $google_credential = $instace[&#39;google_credential&#39;];
echo &quot;instance credential: &quot;; 
}
if ( !empty( $_POST[&#39;g_csrf_token&#39;] ) ) {
$google_csrf_token = $_POST[&#39;g_csrf_token&#39;];
} else {
$google_csrf_token = $instace[&#39;google_csrf_token&#39;];

I can't be sure this will solve all your problems but for certain you should begin here :}

huangapple
  • 本文由 发表于 2023年1月9日 09:03:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定