<?php
/*
Plugin Name: Http Error Codes Manager
Plugin URI: http://www.newbie-project.net/wordpress-stuff/plugins/error-codes-manager/
Description: Edit in One Clic the HTTP error code for a specifik post ( 301, 302 or 404 )
Author: Thomas GRIM & Amaury BALMER
Version: 0.1
Author URI: http://www.newbie-project.net/
*/

Class HttpErrorCodesManager {
    
// Displays two fields for 1. the error code and 2. the url to redirect the visitor to
    
function printFields() {
        global 
$post;
        
$code = (int) get_post_meta($post->ID'redirect_code'true);
        
$url get_post_meta($post->ID'redirect_url',  true);        
        
$url = !empty($url) ? $url 'http://';
        
        echo 
'<hr />',"\n",
        
'<fieldset><legend>' .__("Force a code error for this post"'hecm'). '</legend>',"\n",
        
'  <p><label>' .__('Actions''hecm'). ' ',"\n",
        
'    <select name="redirect_code">',"\n",
        
'      <option value="0"',(($code == 0) ? ' selected' ''),'>' .__('Do nothing''hecm'). '</option>',"\n",
        
'      <option value="1"',(($code == 1) ? ' selected' ''),'>' .__('301 (permanent redirection)''hecm'). '</option>',"\n",
        
'      <option value="2"',(($code == 2) ? ' selected' ''),'>' .__('302 (temporary redirection)''hecm'). '</option>',"\n",
        
'      <option value="3"',(($code == 3) ? ' selected' ''),'>' .__('404 (not found)''hecm'). '</option>',"\n",
        
'    </select>',"\n",
        
'  </label></p>',"\n",
        
'  <p><label>' .__('URL (301 or 302 ):''hecm'). ' <input type="text" name="redirect_url" value="',$url,'" size="75" /></label></p>',"\n",
        
'</fieldset>',"\n";
        return;
    }

    
// Performs the redirection (if requested)
    
function redirect() {        
        
// If we are on a single post or a single page
        
if ( is_single() || is_page() ) {
            global 
$wp_query;
            
$code = (int) get_post_meta($wp_query->post->ID'redirect_code'true);
            
            if ( 
$code === ) {
                
header('HTTP/1.1 404 Not Found');
                die(
__('<strong>404 File not found !</strong>''hecm'));
            } elseif ( 
$code === || $code === ) {
                
$url trim(get_post_meta($wp_query->post->ID'redirect_url',  true));
                if ( 
$code === 2  && !empty($url) ) {
                    
wp_redirect($url302);
                } 
                if ( 
$code === 3  && !empty($url) ) {
                    
wp_redirect($url301);
                }
            }
        }
        return;
    }

    
// Updates the fields on form submission (publish_and edit post)
    
function saveFields($post_id) {
        if( isset(
$_POST['redirect_code']) || isset($_POST['redirect_url']) ) {
            
delete_post_meta($post_id'redirect_url');
            
delete_post_meta($post_id'redirect_code');        
            
add_post_meta($post_id'redirect_code', (int) $_POST['redirect_code']);        
            
add_post_meta($post_id'redirect_url'clean_url($_POST['redirect_url']));        
        }
        return;
    }
}

// Now we register our functions
add_action('edit_form_advanced', array('HttpErrorCodesManager''printFields'));
add_action('simple_edit_form',   array('HttpErrorCodesManager''printFields'));
add_action('edit_page_form',     array('HttpErrorCodesManager''printFields'));
add_action('publish_post',       array('HttpErrorCodesManager''saveFields'));
add_action('template_redirect',  array('HttpErrorCodesManager''redirect'));
?>