• Resolved IamMarvin

    (@wp-marvin261)


    Hello. I made a WordPress plugin with php files. However some of my php files can use is_user_logged_in() and some are not. Anyone here can help me.

    Here is the code of my main plugin file(pluginstart.php)
    <?php
    /** /public_html/wp-content/plugins/SLMS/pluginstart.php */
    /**
    * Plugin Name: SLMS
    * Description: School library Management System shortcodes.
    * Version: 1.0.1
    * Author: Marvin
    * Author Email: [email protected]
    * Author URI: https://www.upwork.com/freelancers/~01f07c797570a44277
    */
    include( plugin_dir_path( __FILE__ ).’slms-page.php’);
    include( plugin_dir_path( __FILE__ ).’user-record-page.php’);
    include( plugin_dir_path( __FILE__ ).’UserRecord.php’);
    include( plugin_dir_path( __FILE__ ).’borrow-books-page.php’);
    include( plugin_dir_path( __FILE__ ).’login-page.php’);
    include( plugin_dir_path( __FILE__ ).’library-records-page.php’);
    include( plugin_dir_path( __FILE__ ).’librarians-guide-page.php’);
    define(‘SLMS_JSCSS’, plugin_dir_url(__FILE__).’includes/’);

    /** wp_redirect() does not work in some web server/host. Use ob_clean() and ob_start(). clean the output buffer and turn on output buffering
    ob_start(); ob_clean();
    */
    //add front end css
    function slms_js_css(){
    wp_enqueue_style(‘slms_enqueue’, SLMS_JSCSS.”front-style.css”);
    wp_enqueue_script(‘slms_enqueue’);
    }
    add_action(‘wp_footer’,’slms_js_css’);

    /** Redirect WordPress Logout to Home Page */
    add_action(‘wp_logout’,create_function(”,’wp_redirect(home_url());exit();’));
    ?>






    Here is the code of some of my php file(UserRecord.php) that is_user_logged_in() is not working.

    /** public_html/wp-content/plugins/SLMS/UserRecord.php */
    if ( !is_user_logged_in() ) {
    wp_redirect( home_url() );
    exit;
    }
    elseif(isset($_POST[‘saveUserBtn’]) and $_SERVER[‘REQUEST_METHOD’] == “POST”) {
    insertRecord();

    /**
    insertRecord();
    echo “working” ;
    */

    }

    elseif(isset($_POST[‘searchUNBtn’]) and $_SERVER[‘REQUEST_METHOD’] == “POST”) {
    searchUNRecord();

    /**
    searchUNRecord();
    echo $_POST[‘searchUN’];
    echo “Success”;*/
    }

    elseif(isset($_POST[‘updateUserBtn’]) and $_SERVER[‘REQUEST_METHOD’] == “POST”) {
    updateRecord();

    /**
    updateRecord();
    echo $_POST[‘ID’];
    echo “Update Status”;*/
    }

    • This topic was modified 7 years, 1 month ago by IamMarvin. Reason: easily read by others
    • This topic was modified 7 years, 1 month ago by IamMarvin. Reason: easily read by others
    • This topic was modified 7 years, 1 month ago by IamMarvin. Reason: easily read by others
    • This topic was modified 7 years, 1 month ago by IamMarvin. Reason: easily read by others

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • is_user_logged_in() won’t be able to tell if the user is logged in until the init hook or later. So it will work in any callback functions hooked to init or later, but won’t work if you just use it in the plugin files outside of a hook.

    Since pretty much everything that involves WordPress in a plugin should be hooked somewhere, you shouldn’t need to use it outside of a hook anyway, unless you’re trying to conditionally add a hook based on whether the user is logged in. If you want to do that you’ll need to check is_user_logged_in() inside the callback function.

    Thread Starter IamMarvin

    (@wp-marvin261)

    Thanks Jacob Peattie. I understand your explanation how to use WordPress . I will not ask an example code how to use this is_user_logged_in() function. Some Web Developer will also ask this kind of questions and post a question to this forum and they want to know what is the example code how to use this is_user_logged_in() .

    I will mark this Post Question solve.

    Thank you.

    • This reply was modified 7 years, 1 month ago by IamMarvin. Reason: easily understand
    • This reply was modified 7 years, 1 month ago by IamMarvin. Reason: easily understand
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘is_user_logged_in() Not Working on Some of My Plugin PHP File’ is closed to new replies.