Advertisement
mikelittle

Redirectify Updated

Oct 6th, 2011
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.89 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Redirectify
  4. Plugin URI: http://www.asymptomatic.net/wp-hacks
  5. Description: A plugin that redirects posts and pages that contain a certain meta tag. Meta box added by <a href-"http://zed1.com/">Mike Little</a>.
  6. Author: Owen Winkler
  7. Version: 1.1
  8. Author URI: http://www.asymptomatic.net
  9. */
  10.  
  11. /*
  12. Redirectify - redirects posts and pages that contain
  13.  a certain meta tag.
  14. Copyright (c) 2004 Owen Winkler
  15.  
  16. Permission is hereby granted, free of charge, to any person
  17. obtaining a copy of this software and associated
  18. documentation files (the "Software"), to deal in the
  19. Software without restriction, including without limitation
  20. the rights to use, copy, modify, merge, publish,
  21. distribute, sublicense, and/or sell copies of the Software,
  22. and to permit persons to whom the Software is furnished to
  23. do so, subject to the following conditions:
  24.  
  25. The above copyright notice and this permission notice shall
  26. be included in all copies or substantial portions of the
  27. Software.
  28.  
  29. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  30. KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  31. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  32. PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  33. OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  34. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  35. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  36. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. */
  38.  
  39. define( 'REDIRECT_META_KEY', 'redirect' );
  40.  
  41. add_action('template_redirect', 'redirectify');
  42. function redirectify() {
  43.     global $wp_query;
  44.    
  45.     if ( is_single() || is_page() ) {
  46.         $redirect_url = get_post_meta( $wp_query->post->ID, REDIRECT_META_KEY, true );
  47.         if ( !empty( $redirect_url ) ) {
  48.             wp_redirect($redirect_url);
  49.             exit;
  50.         }
  51.     }
  52. }
  53.  
  54. add_action( 'admin_menu', 'redirectify_add_meta_box' );
  55. function redirectify_add_meta_box() {
  56.     add_meta_box( 'redirectify_metabox_id', __( 'Redirection URL', 'redirectify' ), 'redirectify_meta_box', 'post', 'normal' );
  57.     add_meta_box( 'redirectify_metabox_id', __( 'Redirection URL', 'redirectify' ), 'redirectify_meta_box', 'page', 'normal' );
  58. }
  59.  
  60. function redirectify_meta_box($post, $data) {
  61.     $redirectify_url = get_post_meta( $post->ID, REDIRECT_META_KEY, true );
  62.     echo '<p class="meta-options">';
  63.     wp_nonce_field( 'redirectify', 'redirectify_meta_nonce' );
  64.     echo '<label for="redirectify_url">' . __( 'URL:', 'redirectify' ) . '</label>';
  65.     echo '<input type="text" id="redirectify_url" name="redirectify_url" style="width:75%" value="' . esc_url( $redirectify_url ) . '" />';
  66.     echo __( " <small>Don't forget the 'http://'</small>", 'redirectify' );
  67.     echo '</p>';
  68. }
  69.  
  70.  
  71. add_action( 'save_post', 'redirectify_save_post' );
  72. function redirectify_save_post( $post_id ) {
  73.         global $post;
  74.  
  75.         // verify this came from our screen and with proper authorization,
  76.         // because save_post can be triggered at other times
  77.  
  78.         if ( ( 'post' !== $_POST['post_type'] ) && ( 'page' !== $_POST['post_type'] ) ) {
  79.             return;
  80.         }
  81.  
  82.         // don't save if this is an auto save
  83.         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  84.             return;
  85.  
  86.         // if we are saving a revision we do *not* want to save the current values to that revision
  87.         if ( wp_is_post_revision($post_id)  )
  88.             return;
  89.  
  90.         // nonce OK?
  91.         if ( !isset( $_POST['redirectify_meta_nonce'] ) || ( !wp_verify_nonce( $_POST['redirectify_meta_nonce'], 'redirectify' ) ) ) {
  92.             return;
  93.         }
  94.  
  95.         // user permissions?
  96.         if ( !current_user_can( 'edit_post', $post_id ) )
  97.             return;
  98.  
  99.         // We're authenticated: we need to get and save the data
  100.  
  101.         $redirectify_url = isset( $_POST['redirectify_url'] ) ? $_POST['redirectify_url'] : '';
  102.  
  103.         if ( !empty( $redirectify_url ) )
  104.             update_post_meta( $post_id, REDIRECT_META_KEY, $redirectify_url );
  105.         else
  106.             delete_post_meta( $post_id, REDIRECT_META_KEY );
  107.  
  108.         return; // k thnx bai!
  109.     } // end redirectify_save_post
  110.  
  111. ?>
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement