This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jun 23rd, 2012  |  syntax: PHP  |  size: 2.68 KB  |  views: 35  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. function create_first_login_menu_item() {
  4.  
  5.         add_options_page( 'First Login Redirect Options', 'First Login', 'manage_options', 'first-login-redirect', 'first_login_options_page');
  6.  
  7. }
  8. add_action( 'admin_menu', 'create_first_login_menu_item' );
  9.  
  10. function first_login_options_page() {
  11.  
  12.         if( !current_user_can( 'manage_options' ) ) {
  13.                 wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
  14.         }
  15.         ?>
  16.         <div class="wrap">
  17.                 <form action="options.php" method="post">
  18.                   <?php settings_fields( 'first_login_options' ); ?>
  19.                   <?php do_settings_sections( 'first-login-redirect'); ?>
  20.       <input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" class="button-primary" />
  21.                 </form>
  22.         </div>
  23.         <?php
  24. }
  25.  
  26. function redirect_first_login( $redirect_to, $url_redirect_to = '', $user = null ) {
  27.  
  28.         if( $user instanceof WP_User ) {
  29.  
  30.                 $has_logged_in = get_user_meta( $user->ID, 'has_logged_in', true );
  31.  
  32.                 if( $has_logged_in ) {
  33.  
  34.                         return $redirect_to;
  35.  
  36.                 } else {
  37.  
  38.                         update_user_meta( $user->ID, 'has_logged_in', true );
  39.                         $first_time_url = get_option( 'first_time_url' );
  40.  
  41.                         if( $first_time_url !== FALSE ) {
  42.  
  43.                                 return get_permalink( $first_time_url );
  44.  
  45.                         } else {
  46.  
  47.                                 return home_url();
  48.  
  49.                         }
  50.                        
  51.                 }
  52.  
  53.         }
  54.  
  55. }
  56. add_filter( 'login_redirect', 'redirect_first_login', 10, 3 );
  57.  
  58. function first_login_validate( $input ) {
  59.  
  60.         $save = get_option( 'first_login_options' );
  61.        
  62.         if( is_page( intval( $input['first_time_url'] ) ) ) {
  63.                 $save['first_time_url'] = trim( $input['first_time_url'] );
  64.         }
  65.  
  66.         return $save;
  67.  
  68. }
  69.  
  70. function register_first_login_settings() {
  71.        
  72.         register_setting( 'first_login_options', 'first_login_options', 'first_login_validate' );
  73.  
  74.         add_settings_section( 'first_login_main', 'First Login Settings', 'first_login_text', 'first-login-redirect' );
  75.  
  76.         add_settings_field( 'first_time_url', 'Page', 'first_login_select', 'first-login-redirect', 'first_login_main' );
  77.  
  78. }
  79. add_action( 'admin_init', 'register_first_login_settings' );
  80.  
  81. function first_login_text() {
  82.         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>';
  83. }
  84.  
  85. function first_login_select() {
  86.        
  87.         $options = get_option( 'first_login_options' );
  88.         $pages = get_pages();
  89.  
  90.         echo '<select id="first_login_options[first_time_url]" name="first_login_options[first_time_url]" >';
  91.         echo '  <option value="">Select a Page</option>';
  92.         foreach ( $pages as $page ) {
  93.  
  94.                 $is_selected = ( $options['first_time_url'] == $page->ID ) ? ' selected="selected"' : '';
  95.  
  96.                 echo '<option value="' . $page->ID . '"' . $is_selected . '>' . $page->post_title . '</option>';
  97.  
  98.         }
  99.  
  100.         echo '</select>';
  101. }
clone this paste RAW Paste Data