.
*/
// Actions
//----------
add_action('admin_menu', 'add_edit_box_ppr');
add_action('save_post', 'ppr_save_postdata', 1, 2); // save the custom fields
add_action('wp','ppr_do_redirect', 1, 2);
// Variables
//----------
global $wpdb;
global $wp_query;
// Functions
//----------
function addmetatohead_theme(){
global $ppr_url;
$meta_code = '';
echo $meta_code;
exit;//stop loading page so meta can do it's job without rest of page loading.
}
function ppr_do_redirect(){
global $post,$wp_query,$ppr_url,$ppr_active,$ppr_url,$ppr_type;
if($wp_query->is_single || $wp_query->is_page ):
$thisidis_current= $post->ID;
$ppr_active=get_post_meta($thisidis_current,'pprredirect_active',true);
$ppr_url=get_post_meta($thisidis_current,'pprredirect_url',true);
$ppr_type=get_post_meta($thisidis_current,'pprredirect_type',true);
if( $ppr_active==1 && $ppr_url!='' ):
if($ppr_type===0){$ppr_type='200';}
if($ppr_type===''){$ppr_type='302';}
if($ppr_type=='meta'):
//metaredirect
add_action('wp_head', "addmetatohead_theme",1);
elseif($ppr_url!=''):
//check for http:// - as full url - then we can just redirect if it is //
if( strpos($ppr_url, 'http://')=== 0 || strpos($ppr_url, 'https://')=== 0){
$offsite=$ppr_url;
header('Status: '.$ppr_type);
header('Location: '.$offsite, true, $ppr_type);
exit; //stop loading page
}elseif(strpos($ppr_url, 'www')=== 0){ // check if they have full url but did not put http://
$offsite='http://'.$ppr_url;
header("Status: $ppr_type");
header("Location: $offsite", true, $ppr_type);
exit; //stop loading page
}elseif(is_numeric($ppr_url)){ // page/post number
if($ppr_postpage=='page'){ //page
$onsite=get_bloginfo('url').'/?page_id='.$ppr_url;
header("Status: $ppr_type");
header("Location: $onsite", true, $ppr_type);
exit; //stop loading page
}else{ //post
$onsite=get_bloginfo('url').'/?p='.$ppr_url;
header("Status: $ppr_type");
header("Location: $onsite", true, $ppr_type);
exit; //stop loading page
}
//check permalink or local page redirect
}else{ // we assume they are using the permalink / page name??
$onsite=get_bloginfo('url'). $ppr_url;
header("Location: $onsite", true, $ppr_type);
exit; //stop loading page
}
endif;
endif;
endif;
}
//=======================================
// Add options to post/page edit pages
//=======================================
// Adds a custom section to the Post and Page edit screens
function add_edit_box_ppr() {
if( function_exists( 'add_meta_box' )) {
add_meta_box( 'edit-box-ppr', __( 'Quick Page/Post Redirect', 'sp' ), 'edit_box_ppr_1', 'page', 'normal', 'high' ); //for page
add_meta_box( 'edit-box-ppr', __( 'Quick Page/Post Redirect', 'sp' ), 'edit_box_ppr_1', 'post', 'normal', 'high' ); //for post
}
}
// Prints the inner fields for the custom post/page section
function edit_box_ppr_1() {
global $post;
$ppr_option1='';
$ppr_option2='';
$ppr_option3='';
$ppr_option4='';
$ppr_option5='';
// Use nonce for verification ... ONLY USE ONCE!
echo '';
// The actual fields for data entry
if(get_post_meta($post->ID, 'pprredirect_active', true)!=''){$pprediractive="checked";}else{$pprediractive="";}
echo ' ';
echo ' (check to turn on) NOTE: The page or post needs to be published for the redirect to happen UNLESS you publish it first, then save it as a Draft..
';
echo ' ';
if(get_post_meta($post->ID, 'pprredirect_url', true)!=''){$pprredirecturl=get_post_meta($post->ID, 'pprredirect_url', true);}else{$pprredirecturl="";}
echo ' (i.e., http://example.com or /somepage/ or p=15 or 155. Use FULL URLincludinghttp:// for all external and meta redirects. )
';
echo ' ';
if(get_post_meta($post->ID, 'pprredirect_type', true)!=''){$pprredirecttype=get_post_meta($post->ID, 'pprredirect_type', true);}else{$pprredirecttype="";}
switch($pprredirecttype):
case "":
$ppr_option2=" selected";//default
break;
case "301":
$ppr_option1=" selected";
break;
case "302":
$ppr_option2=" selected";
break;
case "307":
$ppr_option3=" selected";
break;
case "meta":
$ppr_option5=" selected";
break;
endswitch;
echo ' (Default is 302)
';
}
// When the post is saved, saves our custom data
function ppr_save_postdata($post_id, $post) {
if(isset($_POST['pprredirect_active']) || isset($_POST['pprredirect_url']) || isset($_POST['pprredirect_type']) ):
// verify authorization
if(isset($_POST['pprredirect_noncename'])){
if ( !wp_verify_nonce( $_POST['pprredirect_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
}
// check allowed to editing
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post->ID ))
return $post->ID;
} else {
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
}
// find & save the form data
// Put it into an array
$mydata['pprredirect_active'] = $_POST['pprredirect_active'];
$mydata['pprredirect_url'] = $_POST['pprredirect_url'];
if($mydata['pprredirect_url']!=''){
$mydata['pprredirect_type'] = $_POST['pprredirect_type'];
}
// Add values of $mydata as custom fields
foreach ($mydata as $key => $value) { //Let's cycle through the $mydata array!
if( $post->post_type == 'revision' ) return; //don't store custom data twice
$value = implode(',', (array)$value); //if $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { //if the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { //if the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); //delete if blank
}
endif;
}
?>