Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function create_first_login_menu_item() {
- add_options_page( 'First Login Redirect Options', 'First Login', 'manage_options', 'first-login-redirect', 'first_login_options_page');
- }
- add_action( 'admin_menu', 'create_first_login_menu_item' );
- function first_login_options_page() {
- if( !current_user_can( 'manage_options' ) ) {
- wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
- }
- ?>
- <div class="wrap">
- <form action="options.php" method="post">
- <?php settings_fields( 'first_login_options' ); ?>
- <?php do_settings_sections( 'first-login-redirect'); ?>
- <input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" class="button-primary" />
- </form>
- </div>
- <?php
- }
- function redirect_first_login( $redirect_to, $url_redirect_to = '', $user = null ) {
- if( $user instanceof WP_User ) {
- $has_logged_in = get_user_meta( $user->ID, 'has_logged_in', true );
- if( $has_logged_in ) {
- return $redirect_to;
- } else {
- update_user_meta( $user->ID, 'has_logged_in', true );
- $first_time_url = get_option( 'first_time_url' );
- if( $first_time_url !== FALSE ) {
- return get_permalink( $first_time_url );
- } else {
- return home_url();
- }
- }
- }
- }
- add_filter( 'login_redirect', 'redirect_first_login', 10, 3 );
- function first_login_validate( $input ) {
- $save = get_option( 'first_login_options' );
- if( is_page( intval( $input['first_time_url'] ) ) ) {
- $save['first_time_url'] = trim( $input['first_time_url'] );
- }
- return $save;
- }
- function register_first_login_settings() {
- register_setting( 'first_login_options', 'first_login_options', 'first_login_validate' );
- add_settings_section( 'first_login_main', 'First Login Settings', 'first_login_text', 'first-login-redirect' );
- add_settings_field( 'first_time_url', 'Page', 'first_login_select', 'first-login-redirect', 'first_login_main' );
- }
- add_action( 'admin_init', 'register_first_login_settings' );
- function first_login_text() {
- echo '<p>Select the page that you would like user\'s to be redirected to when they log in for the very first time.</p>';
- }
- function first_login_select() {
- $options = get_option( 'first_login_options' );
- $pages = get_pages();
- echo '<select id="first_login_options[first_time_url]" name="first_login_options[first_time_url]" >';
- echo ' <option value="">Select a Page</option>';
- foreach ( $pages as $page ) {
- $is_selected = ( $options['first_time_url'] == $page->ID ) ? ' selected="selected"' : '';
- echo '<option value="' . $page->ID . '"' . $is_selected . '>' . $page->post_title . '</option>';
- }
- echo '</select>';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement