Support » Plugin: Helpful » Save user info stored that is currently stored in a session instead of anonymous

  • Resolved mesonrhinitis

    (@mesonrhinitis)


    Hi,

    Thanks for this great plugin. On my website people have to login with an API from a third party website. Their user information is saved in a session. They don’t have an WordPress account. So when they answer the helpful question their answer is saved as an Anonymous user. Is it possible with an hook to save the name stored in the session instead of Anonymous?

Viewing 15 replies - 1 through 15 (of 18 total)
  • Plugin Author Pixelbart

    (@pixelbart)

    Thank you for your feedback!

    There is a hook that you can use to edit the fields that are sent via the feedback form:

    https://helpful-plugin.info/documentation/helpful_feedback_submit_fields/

    There is no hook for the user ID because the WP_User objects are used here.

    You can use is_user_logged_in in the hook to check whether the user is logged in, provided the API does this correctly and logs the user in.

    Hi Bart,

    Thanks for your reply. I thought I had everything working until I realised it only worked when I was also logged in on an admin account. That’s when the helpful_feedback_submit_fields only seems to work.

    Users on the website login with the credentials of a third party using an API. The account information is not stored in WordPress. For that reason I am not able to use is_user_logged_in right?

    Is there anything else I can do to make this work? Thanks in advance!

    Hi Pixelbart, would love to hear from you if this is possible so I can keep making use of your plugin.

    Plugin Author Pixelbart

    (@pixelbart)

    @mesonrhinitis

    If is_user_logged_in does not work, you can also simply check if the user is logged in.

    You can easily do this with PHP. How is the user data stored? Can you send me an example so I can create an example for you.

    function custom_function( $fields ) {
    
        if(isset($_SESSION['data'])) {
            $fields = array(
                "name" => $_SESSION['data']['data']['firstname'] . ' ' . $_SESSION['data']['data']['name'],
                "email" => $_SESSION['data']['data']['email'],
            );
    
            return $fields;
        }
    
    }
    
    add_filter( 'helpful_feedback_submit_fields', 'custom_function' );

    This is the code I used and it works only when an user is admin logged in. The user data is stored in a session.

    Plugin Author Pixelbart

    (@pixelbart)

    @mesonrhinitis

    I have adapted your code a bit.

    You must set return $fields outside the IF-query, otherwise no more fields will be returned.

    /**
     * Fills the fields, if a session exists.
     *
     * @param array $fields
     * 
     * @return array
     */
    function custom_function( $fields ) {
    	if ( isset( $_SESSION['data'] ) && ! empty( $_SESSION['data'] ) ) {
    		$fields['name']  = sanitize_text_field( $_SESSION['data']['data']['firstname'] . ' ' . $_SESSION['data']['data']['name'] );
    		$fields['email'] = sanitize_email( $_SESSION['data']['data']['email'] );
    	}
    
    	return $fields;
    }
    
    /**
     * Extends the filter and uses PHP_INT_MAX to ensure that changes are made at the last position.
     */
    add_filter( 'helpful_feedback_submit_fields', 'custom_function', PHP_INT_MAX );

    sanitize is necessary so that no malicious code can be transmitted!

    • This reply was modified 2 weeks, 1 day ago by Pixelbart.

    @pixelbart

    Thanks for adapting the code. I tried it again but the feedback still shows as Anonymous for me. It works when I am also logged on an admin account.

    Plugin Author Pixelbart

    (@pixelbart)

    @mesonrhinitis

    You might want to check if the session is filled then…

    Helpful does not internally check if you are an admin or a user.

    add_action( 'wp_footer', function() {
    	if ( ! isset( $_SESSION['data'] ) ) return;
    	printf( '<pre>%s</pre>', print_r( $_SESSION['data'], true ) );
    });

    With this: If a session exists, it is output in the footer (directly on the page).

    • This reply was modified 2 weeks, 1 day ago by Pixelbart.

    Yes the session is filled with the data. But the users on my website don’t have a WordPress account. It looks like the filter below only works when an user is officially logged into WordPress.

    add_filter( 'helpful_feedback_submit_fields', 'custom_function', PHP_INT_MAX );

    Could the be the problem?

    Plugin Author Pixelbart

    (@pixelbart)

    @mesonrhinitis

    Ah now I understand. The problem is not Helpful, but the session. Helpful uses Ajax to store the contents. But the session is not passed on using Ajax.

    It is hard to get to the user data without wp_get_current_user. You have to create a WP_User instance with the API to get the data.

    Have you ever tested the plugin? Maybe the sessions work better then:

    https://wordpress.org/plugins/wp-native-php-sessions/

    • This reply was modified 2 weeks, 1 day ago by Pixelbart.

    @pixelbart

    Thanks for your reply. I installed the plugin, but I am not sure how I make it work from there. I am not able to store any user information on the website. So I am not really sure if I can create an WP_User instance with the API information.

    Is there anyway I can pass on the session using Ajax?

    I am sorry if this is not really plugin related.

    Plugin Author Pixelbart

    (@pixelbart)

    @mesonrhinitis

    The plugin works by itself in the background. I will think of a solution for your session problem and put it in. I will have a look at it right away.

    Plugin Author Pixelbart

    (@pixelbart)

    @mesonrhinitis

    I just ran an update. You should be able to access the session now.

    The code looks like this:

    /**
     * Fills the fields, if a session exists.
     *
     * @param array $fields submitted fields
     * @param array $session submitted session data
     * 
     * @return array
     */
    function custom_function( $fields, $session ) {
    	if ( isset( $session['data'] ) && ! empty( $session['data'] ) ) {
    		$fields['name']  = sanitize_text_field( $session['data']['data']['firstname'] . ' ' . $session['data']['data']['name'] );
    		$fields['email'] = sanitize_email( $session['data']['data']['email'] );
    	}
    
    	return $fields;
    }
    
    /**
     * Extends the filter and uses PHP_INT_MAX to ensure that changes are made at the last position.
     */
    add_filter( 'helpful_feedback_submit_fields', 'custom_function', PHP_INT_MAX, 2 );

    Patch Notes: https://github.com/pixelbart/helpful/releases/tag/4.3.0

    @pixelbart thanks again for helping. I updated the plugin and tried the code. But it doesn’t seem to do the job yet. The user information is stored in $_SESSION[‘data’]. I changed it to the following:

    function custom_function( $fields, $session ) {
        
        if ( isset( $_SESSION['data'] ) && ! empty( $_SESSION['data'] ) ) {
            $fields['name']  = sanitize_text_field( $_SESSION['data']['data']['firstname'] . ' ' . $_SESSION['data']['data']['name'] );
            $fields['email'] = sanitize_email( $_SESSION['data']['data']['email'] );
        }
    
        return $fields;
    }

    Or should I be doing something with the $session variable

    Also:
    When both admin and API logged in, I get 500 internal server error. That shouldn’t be a problem because the admin doesn’t vote.

    Plugin Author Pixelbart

    (@pixelbart)

    @mesonrhinitis

    You have to use the $session because that is where the session is stored. In my tests this worked wonderfully. Do you still have the Native PHP Sessions installed?

    https://wordpress.org/plugins/wp-native-php-sessions/

    It sounds to me like the server doesn’t like to work with sessions. Many hosters have this problem, that’s why the plugin is so important. It fixes many bugs with PHP Sessions.

    Otherwise I can’t think of anything else. Without error messages or debug log I can’t help you.

Viewing 15 replies - 1 through 15 (of 18 total)
  • You must be logged in to reply to this topic.