How to check if user is an admin in wordpress

The best way so far to check whether the current user is a super admin user with full permissions in wordpress is to use current_user_can function and check for manage_options . If user has manage_options they clearly have full permissions. I use this for example to hide ads from me and to hide google analytics so that it doesn’t count me while I’m logged in as admin.

  1.  <?php if (!current_user_can( 'manage_options' )):?>
  2.  google analytics code goes here
  3.  <?php endif;?>

How to login a customer/user programmatically in Magento

How to login a user programmatically in Magento? To find out how to do that, we can try to look at Magento code itself. so if we search for ->login in magento files, we will find among others. the following method inside Mage_Customer_AccountController (App/Code/Mage/Customer/controllers/AccountController.php)

  1.   public function loginPostAction()
  2.   {
  3.   if ($this->_getSession()->isLoggedIn()) {
  4.   $this->_redirect('*/*/');
  5.   return;
  6.   }
  7.   $session = $this->_getSession();
  8.   if ($this->getRequest()->isPost()) {
  9.   $login = $this->getRequest()->getPost('login');
  10.   if (!empty($login['username']) && !empty($login['password'])) {
  11.   try {
  12.   $session->login($login['username'], $login['password']);
  13.   if ($session->getCustomer()->getIsJustConfirmed()) {
  14.   $this->_welcomeCustomer($session->getCustomer(), true);
  15.   }
  16.   } catch (Mage_Core_Exception $e) {
  17.   switch ($e->getCode()) {
  18.   case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
  19.   $value = Mage::helper('customer')->getEmailConfirmationUrl($login['username']);
  20.   $message = Mage::helper('customer')->__('This account is not confirmed. Click here to resend confirmation email.', $value);
  21.   break;
  22.   case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
  23.   $message = $e->getMessage();
  24.   break;
  25.   default:
  26.   $message = $e->getMessage();
  27.   }
  28.   $session->addError($message);
  29.   $session->setUsername($login['username']);
  30.   } catch (Exception $e) {
  31.   // Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
  32.   }
  33.   } else {
  34.   $session->addError($this->__('Login and password are required.'));
  35.   }
  36.   }
  37.   $this->_loginPostRedirect();
  38.   }

Based on that, we can take the useful part for us:


  1.  $email="customer email address";
  2.  $password="customer password";
  3.  $session = Mage::getSingleton('customer/session');
  4.  $message=""; //error message to add to session
  5.  try {
  6.   $session->login( $email, $password );
  7.   if ($session->getCustomer()->getIsJustConfirmed()) {
  8.   //Customer is confirmed and successfully logged in
  9.   }
  10.  } catch (Mage_Core_Exception $e) {
  11.   //error occured
  12.   switch ($e->getCode()) {
  13.   case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
  14.   //email not confirmed actions here
  15.   break;
  16.   case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
  17.   //Email or password invalid actions here
  18.   $message = $e->getMessage();//echo the error message
  19.   break;
  20.   default:
  21.   $message = $e->getMessage(); //Display other error messages
  22.   }
  23.   $session->addError($message);
  24.  } catch (Exception $e) {
  25.   //login failed because of some other error.
  26.  }
  27.  

How to display Magento CMS blocks in template files programmatically

There are a lot of reasons you might want to call cms blocks programmatically in magento inside your template or inside your own plugin. Particularly cms blocks are very useful to allow admin access to html from control panel avoiding the need to change theme files.  So, how do you display a cms block inside a theme phtml file?

Here is how to get the CMS helper:


  1.  $cmsHelper = Mage::helper('cms');

Now to get the CMS block by id, you ll need to do this


  1.  $block = Mage::getModel('cms/block')->load("your CMS block id here");

It is better to check if the CMS block is active by:


  1.  if ($block->getIsActive()){

To get CMS block title:


  1.  $block->getTitle()

To get CMS block content/description:


  1.  $block->getContent()

However when you echo description directly you might notice it contains unprocessed magento path variables, therefore you’ll need to convert those to what they mean using Magento’s own method¬†getPageTemplateProcessor


  1.  $processor = $cmsHelper->getPageTemplateProcessor();
  2.  $block_content=$processor->filter($block->getContent());
  3.  

Here is a complete example that get the content and title of a CMS block, checking if it is active and process its variables


  1.  $cmsHelper = Mage::helper('cms');
  2.  $processor = $cmsHelper->getPageTemplateProcessor();
  3.  $block = Mage::getModel('cms/block')->load("my_cms_block_id");
  4.  if ($block->getIsActive()){
  5.   echo "Title:".$block->getTitle()."<br/>";
  6.   echo "Content:".$processor->filter($block->getContent())."<br/>";
  7.  }
  8.  

Why site is so slow on IE even to scroll – Poor IE 8,9 performance

Stop using IE

IF your site is running too slow on IE 8 or IE 9, but it is working perfectly fine on Chrome and Firefox. Check if you are using shadow filter in your css. While Internet Explorer is generally slower than Chrome and Firefox, It is not as slow as when using shadow filters. It really performs terribly that render your site useless and deter visitors from ever daring to visit your site. This is why you should never use filters again. Even scrolling becomes too slow and jumpy. Opening javascript dialog boxes such as jquery lightbox takes extra seconds that it makes the website hard to navigate. While Microsoft describe shadow filter as depreciated on MSDN for IE 9 and above. it is still inexcusable to have it perform this slow especially that IE 8 only supports that way.

The following css makes a huge performance degradation in IE :


  1.   filter: progid:DXImageTransform.Microsoft.Shadow(Color=#777777, Strength=2, Direction=0),
  2.   progid:DXImageTransform.Microsoft.Shadow(Color=#777777, Strength=2, Direction=90),
  3.   progid:DXImageTransform.Microsoft.Shadow(Color=#777777, Strength=2, Direction=180),
  4.   progid:DXImageTransform.Microsoft.Shadow(Color=#777777, Strength=2, Direction=270);

Here is a demo of the issue, try this on IE and then on Chrome/Operate/Firefox/Safari. Try to click remove and add filters and notice the difference. Click the images to trigger lightbox and see how slow it is when the filters implemented. Even on simple text page, it is still slow when scrolling. On Chrome and Firefox, the shadow implemented using box-shadow which performs excellently.

Demo showing IE shadow filters performance

Magento: How to hide products that have no images

If you would like to hide products without images in your Magento store product list page, this is one way to do it. edit app/design/frontend/[YOUR CURRENT TEMPLATE]/[YOUR CURRENT THEME]/template/catalog/list.phtml (e.g. app/design/frontend/default/default/template/catalog/list.phtml)

Change :


  1.  $_productCollection=$this->getLoadedProductCollection();

to


  1.  $_productCollection = clone $this->getLoadedProductCollection();
  2.  $_productCollection->clear()
  3.   ->addAttributeToFilter('image', array('neq' => 'no_selection'))
  4.   ->load();

Since the collection we get from getLoadedProductCollection was already fetched from database, we cannot add attribute filters to it, that’s why it wouldn’t work without the clone. So we need to clone it then clear it. then add the attribute filter.

If you are not familiar with neq, neq means not equal.