Powered By Blogger
Showing posts with label plugin. Show all posts
Showing posts with label plugin. Show all posts

Tuesday, January 10, 2017

add code automatically in htaccess or any file when activate the plugin

function myplugin_enable_flush_rules() {

//add default value for notification
if (get_option('magNotifiy') == 0 ) {
update_option( 'magNotifiy', '1', '', 'yes' );
}else{
add_option( 'magNotifiy', '1', '', 'yes' );
}

//add code in .htaccess file for enable HTTP_AUTHORIZATION
$WPHE_new_content = '

RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1';
$WPHE_orig_path = ABSPATH.'.htaccess';

@clearstatcache();
@chmod($WPHE_orig_path, 0666);

$myfile = fopen($WPHE_orig_path , "r") or die("Unable to open file!");
$htaccess_exist =  fread($myfile,filesize($WPHE_orig_path));
fclose($myfile);

if( strpos( $htaccess_exist, 'HTTP_AUTHORIZATION' ) !== false ) {
   //echo "already added code";
}else{
if (!is_writable($WPHE_orig_path) ) {
echo ".htaccess file not writable permission.";  exit();
}
$WPHE_new_content = str_replace('\\\\', '\\', $WPHE_new_content);
$WPHE_new_content = str_replace('\"', '"', $WPHE_new_content);
$WPHE_write_success = @file_put_contents($WPHE_orig_path, $WPHE_new_content, FILE_APPEND |LOCK_EX);
@clearstatcache();
}
}
// on plugin activation, call the function that will make flush_rules
register_activation_hook( __FILE__, 'myplugin_enable_flush_rules' );