Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Redirectify
- Plugin URI: http://www.asymptomatic.net/wp-hacks
- 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>.
- Author: Owen Winkler
- Version: 1.1
- Author URI: http://www.asymptomatic.net
- */
- /*
- Redirectify - redirects posts and pages that contain
- a certain meta tag.
- Copyright (c) 2004 Owen Winkler
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated
- documentation files (the "Software"), to deal in the
- Software without restriction, including without limitation
- the rights to use, copy, modify, merge, publish,
- distribute, sublicense, and/or sell copies of the Software,
- and to permit persons to whom the Software is furnished to
- do so, subject to the following conditions:
- The above copyright notice and this permission notice shall
- be included in all copies or substantial portions of the
- Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
- KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
- OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
- OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
- define( 'REDIRECT_META_KEY', 'redirect' );
- add_action('template_redirect', 'redirectify');
- function redirectify() {
- global $wp_query;
- if ( is_single() || is_page() ) {
- $redirect_url = get_post_meta( $wp_query->post->ID, REDIRECT_META_KEY, true );
- if ( !empty( $redirect_url ) ) {
- wp_redirect($redirect_url);
- exit;
- }
- }
- }
- add_action( 'admin_menu', 'redirectify_add_meta_box' );
- function redirectify_add_meta_box() {
- add_meta_box( 'redirectify_metabox_id', __( 'Redirection URL', 'redirectify' ), 'redirectify_meta_box', 'post', 'normal' );
- add_meta_box( 'redirectify_metabox_id', __( 'Redirection URL', 'redirectify' ), 'redirectify_meta_box', 'page', 'normal' );
- }
- function redirectify_meta_box($post, $data) {
- $redirectify_url = get_post_meta( $post->ID, REDIRECT_META_KEY, true );
- echo '<p class="meta-options">';
- wp_nonce_field( 'redirectify', 'redirectify_meta_nonce' );
- echo '<label for="redirectify_url">' . __( 'URL:', 'redirectify' ) . '</label>';
- echo '<input type="text" id="redirectify_url" name="redirectify_url" style="width:75%" value="' . esc_url( $redirectify_url ) . '" />';
- echo __( " <small>Don't forget the 'http://'</small>", 'redirectify' );
- echo '</p>';
- }
- add_action( 'save_post', 'redirectify_save_post' );
- function redirectify_save_post( $post_id ) {
- global $post;
- // verify this came from our screen and with proper authorization,
- // because save_post can be triggered at other times
- if ( ( 'post' !== $_POST['post_type'] ) && ( 'page' !== $_POST['post_type'] ) ) {
- return;
- }
- // don't save if this is an auto save
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
- return;
- // if we are saving a revision we do *not* want to save the current values to that revision
- if ( wp_is_post_revision($post_id) )
- return;
- // nonce OK?
- if ( !isset( $_POST['redirectify_meta_nonce'] ) || ( !wp_verify_nonce( $_POST['redirectify_meta_nonce'], 'redirectify' ) ) ) {
- return;
- }
- // user permissions?
- if ( !current_user_can( 'edit_post', $post_id ) )
- return;
- // We're authenticated: we need to get and save the data
- $redirectify_url = isset( $_POST['redirectify_url'] ) ? $_POST['redirectify_url'] : '';
- if ( !empty( $redirectify_url ) )
- update_post_meta( $post_id, REDIRECT_META_KEY, $redirectify_url );
- else
- delete_post_meta( $post_id, REDIRECT_META_KEY );
- return; // k thnx bai!
- } // end redirectify_save_post
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement