Powered By Blogger

Wednesday, January 11, 2017

How to Add Admin Alerts and Error Messages to the Backend of WordPress

https://premium.wpmudev.org/blog/adding-admin-notices/?mksi=b&utm_expid=3606929-94.Ie3dH-CaRwe6MU3VrZsdvw.1



plugin.php : 

function my_error_notice() {
@$WPHE_orig_path = ABSPATH.'.htaccess';
if (!file_exists($WPHE_orig_path)) {
   echo "The file $WPHE_orig_path exists";
    ?>
   <div class="error notice">
       <p><?php _e( "The file $WPHE_orig_path exists. This file is mandetory for Magnificent API plugin" ); ?></p>
   </div>
   <?php
}else {

//READ THE CONTENT FROM HTACCESS FILE
@$myfile = fopen($WPHE_orig_path , "r") or die("Unable to open file!");
@$htaccess_exist =  fread($myfile,filesize($WPHE_orig_path));
fclose($myfile);

//If not have writable permission
if (!is_writable($WPHE_orig_path) &&  strpos( $htaccess_exist, 'HTTP_AUTHORIZATION' ) == false  ) {
   ?>
   <div class="error notice">
       <p><?php _e( '.htaccess file not writable permission. Please add the below code in .htaccess file.
       <p>RewriteEngine on</p>
<p>RewriteCond %{HTTP:Authorization} ^(.*)</p>
<p>RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]</p>
<p>SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1', 'my_plugin_textdomain</p>' ); ?></p>
   </div>
   <?php
   }
   //If have writable permission
if (is_writable($WPHE_orig_path) &&  strpos( $htaccess_exist, 'HTTP_AUTHORIZATION' ) == false  ) {
    //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_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();
   }
   }

}
add_action( 'admin_notices', 'my_error_notice' );

No comments:

Post a Comment