Categories
CMS Mastering Development Wordpress

In wordpress plugin wp_signon shows error

I written a plugin to handle login functions. The plugin checks for login info in the_content filter and shows only for logged users. wp_signon shows an error/warning

Warning: Cannot modify header information – headers already sent by (output started at W:\websites\ku\wp-includes\class.wp-styles.php:347) in W:\websites\ku\wp-includes\pluggable.php on line 943

<?php
/**
 * Plugin Name: test-plugin
 * Description: my test plugin writing experience in WordPress
 * Version: 1.0
 * Author: KK
*/

function my_add_shortcodes() {
    add_shortcode( 'my-login-form', 'my_login_form_shortcode' );
}

function my_login_form_shortcode() {
    ob_start();
    require_once('forms/login.php');
    return ob_get_clean();
}

function filter_content( $content ) {
        if ( ! empty( filter_input( INPUT_POST, 'login_submit' ) ) ) {
            $login_info = array_map( 'sanitize_text_field', $_POST );
            $email = $login_info['email'];
            $password = $login_info['password'];
            echo 'email '.$email.' password '.$password;
            $user = wp_signon(array('user_login' => $email, 'user_password' => $password, 'remember' => $rememberme), is_ssl());
            if( $user instanceof WP_User){
                echo " check_credentials ".$user->display_name;
                wp_set_current_user($user->ID, $user->user_login);
            } else {
                if( is_wp_error($user)){
                    echo $user->get_error_message();
                    $this->$error = "Email or Password error";
                    return $this->$error;
                } 
            }
        } else {
            return "<p>[my-login-form]</p";
        }
        
        return $content;
}
    
add_action( 'init', 'my_add_shortcodes' );
add_filter( 'the_content', 'filter_content' );

Leave a Reply

Your email address will not be published. Required fields are marked *