Magento: Quotes being escaped when magic_quotes_gpc is set to off
“Magento is escaping apostrophes when magic_quotes_gpc is set to off. When I set magic_quotes_gpc to on, Magento stops inserting slashes. It’s completely backwards.
I can’t have Magento escaping my apostrophes, but I also do not want to have magic_quotes_gpc set to on because I am concerned about the implications it might have on other parts of my site (vBulletin forum, WordPress blog, etc.).
Just to note – Magento wasn’t always behaving this way, it only started today.”
That was a scenario I just encountered with two of my clients websites where the required WordPress integrations. I noticed that Magento all of a sudden started escaping quotes and it became very troublesome to edit CMS pages in Magento due to that.
In order to resolve the issue I did this:
It turns out that WordPress has it’s own function to add in slashes. As of WordPress version 3.2.1, you can find function wp_magic_quotes() around line 530 of /wp-includes/load.php
To fix the issue, I commented out everything within the function (not the function itself, so as to prevent a call to an undefined function). It’s removed the issue of escaped quotes. I haven’t done extensive testing, but from what I understand, this may break older WordPress plug-ins, so be careful.
It will go from this:
function wp_magic_quotes() {
// If already slashed, strip.
if ( get_magic_quotes_gpc() ) {
$_GET = stripslashes_deep( $_GET );
$_POST = stripslashes_deep( $_POST );
$_COOKIE = stripslashes_deep( $_COOKIE );
}
// Escape with wpdb.
$_GET = add_magic_quotes( $_GET );
$_POST = add_magic_quotes( $_POST );
$_COOKIE = add_magic_quotes( $_COOKIE );
$_SERVER = add_magic_quotes( $_SERVER );
// Force REQUEST to be GET + POST.
$_REQUEST = array_merge( $_GET, $_POST );
}
to this:
function wp_magic_quotes() {
// If already slashed, strip.
//if ( get_magic_quotes_gpc() ) {
// $_GET = stripslashes_deep( $_GET );
// $_POST = stripslashes_deep( $_POST );
// $_COOKIE = stripslashes_deep( $_COOKIE );
//}
// Escape with wpdb.
//$_GET = add_magic_quotes( $_GET );
//$_POST = add_magic_quotes( $_POST );
//$_COOKIE = add_magic_quotes( $_COOKIE );
//$_SERVER = add_magic_quotes( $_SERVER );
// Force REQUEST to be GET + POST.
// $_REQUEST = array_merge( $_GET, $_POST );
}
Magento: Numbers are added to the end of my URLs automatically
Sometimes when you make changes to your products, or enable a certain extension, Magento might start to rewrite all your URLs to include a suffix “-1″ or some other number. Within the URL Rewrites, Magento differentiates between System URLs and Custom URLs. If the System URLs are broken like this, you should not fix this by adding new Custom URLs.
Instead, open up phpMyAdmin, create a backup of your Magento database, and flush the Magento table core_url_rewrite (so that it becomes totally empty). Immediately afterwards, refresh the Catalog Url Rewrites under Index Management. This will regenerate all System URLs.
Credit: yireo.com
Magento: Will my hosting support Magento?
Over the past couple of years I have had to set up several LAMP boxes from scratch to run Magento. Up until today I’ve had a script I could run that would tell me if all of the requirements are met to run Magento on the box. That script used phpversion() function for version comparison and now that function doesn’t seem to be working as it used to. A little Googling brought me to the answer that the function can be replaced with a PHP constant “PHP_VERSION”. I have provided my new working script for your perusal. I hope this is valid code for you guys for at least 6 months…
<?php
extension_check(array(
'curl',
'dom',
'gd',
'hash',
'iconv',
'mcrypt',
'pcre',
'pdo',
'pdo_mysql',
'simplexml'
));
function extension_check($extensions) {
$fail = '';
if(version_compare(PHP_VERSION, '5.2.0', '<')) {
$fail .= '<li>PHP 5.2.0 (or greater)</li>';
}
if(!ini_get('safe_mode')) {
if(preg_match('/[0-9].[0-9]+.[0-9]+/', shell_exec('mysql -V'), $version)) {
if(version_compare($version[0], '4.1.20', '<')) {
$fail .= '<li>MySQL 4.1.20 (or greater)</li>';
}
}
}
foreach($extensions as $extension) {
if(!extension_loaded($extension)) {
$fail .= '<li>'.$extension.'</li>';
}
}
if($fail) {
echo '<p>Your server does not meet the requirements for Magento.';
echo 'The following requirements failed:</p>';
echo '<ul>'.$fail.'</ul>';
} else {
echo '<p>Congratulations! Your server meets the requirements for Magento.</p>';
}
}
?>
Magento: Can’t upload product images
If for some reason you cannot all of a sudden upload product images into Magento through the admin area I suggest you SSH into your server and navigate to the Magento directory for your website and run the following:
# chmod 777 -R media
Magento: Product Image Upload Problem
My client was having an issue on his Godaddy shared hosting account with his Magento product image uploads. I went through a dozen or more troubleshooting steps to determine the cause of the issue. After careful comparison with a Magento instance just like it hosted on another hosting company server I determined Magento was not at fault! Magento tries to write to a tmp directory on your server when it uploads and if it doesn’t have the privileges to do so will fail on image upload everytime.
My solution on Godaddy shared hosting is to create a php5.ini in the root of your Magento installation. Inside the php5.ini file add the following line:
upload_tmp_dir = var/tmp
Then under your Magento var directory create a directory named “tmp” with 755 permissions.
If this does not resolve your issue please contact me as anyone of the other troubleshooting steps could resolve your issue!
Magento: Get RAW MySQL query information
Ok, so you have a project where the client asks you to bypass the Magento API because it’s so painfully slow…what do you do? Not wanting to start digging through the haystack that is Magento to find the needles of MySQL I began Googling for an answer. What I quickly learned is that Magento’s MySQL queries are abstracted through PDO. Therefore I wouldn’t be able to get what I wanted through digging through source code. At this point I began wondering if there was some sort of application that could act as a proxy and then it dawned on me. I wondered if there was some sort of logging that could be done in MySQL to capture every database transaction.
Surprise surprise, there is a very simple way to enable query logging in MySQL! First off you will need shell access to your Linux box so that you can open up /etc/my.cnf which is MySQL’s configuration file. Once you’ve opened that file insert the following and close/save:
[mysqld]
log=/var/log/mysqld.log
If /var/log/mysqld.log doesn’t exist on your system you will need to create it. You can do so by running:
bash> touch /var/log/mysqld.log
Then set owner and group owner for the mysqld.log file to allow MySQL server to write to the file
bash> chown mysql:mysql /var/log/mysqld.log
At this point you should restart MySQL server. On CentOS you can simply run:
bash> /sbin/service mysqld restart
Now you should be ready to watch real-time MySQL transactions. To see those transactions you can run the following command:
bash> tail -f /var/log/mysqld.log
What I suggest doing at this point is to navigate to a particular process in Magento that you are wanting to get RAW MySQL. Stop tailing the log file and then run:
bash> cat /dev/null > /var/log/mysqld.log
This will empty the contents of the MySQL log file. Start your tail of the log file again and run the Magento process. You should now have an excellent snap shot of exactly what MySQL is being run for a given process.
xpathbuilder.com: An awesome SEO research tool
I have a wide range of development experience with WordPress and Magento. But I also love to sink my teeth into custom projects. So when my colleague Ryan Boots approached me about a unique site concept, I had to pursue it.
As an SEO professional, Ryan was looking for an easy way to study search engine results, preferably within a spreadsheet. After some research, he discovered Google Doc spreadsheets supported an importXML function which, combined with XPath code, can instantly import Google search results. The problem was that putting the code together for different queries ate up a lot of time. In addition, he wanted to scrape results from Bing and Yahoo, which require completely different XPath code.
So he approached me about putting together a site that would generate importXML strings quickly for multiple queries and search engines. The result: xpathbuilder.com.
A few notes:
- The tool was 100% custom development from the ground up. The string fields are built with jQuery.
- We started with a handful of search engines – Google, Yahoo, Bing and Ask. We also tossed in Google Suggest – the autocomplete feature you see when entering a search term in Google. But we built the tool with expansion in mind, so we’ll probably add more search engines down the road, pending user feedback.
- We put quite a bit of thought into the usability of this tool. We’re hoping that a reasonably intelligent site visitor will find it fairly straightforward to use.
We’re hoping this will become a useful tool in the SEO community. It’s already getting some positive attention on Twitter, and Distilled had some nice things to say about it, which is gratifying.
Do me a favor? Go check it out and let me and Ryan know what you think. If you see a way it could be improved, we especially want to hear from you.
Magento: Sort Product Options by Value
Today I was presented with a Magento configurable product that has options that were not appearing in order when you clicked the drop down. A quick look with Firebug showed me that the drop down option values would be in proper alpha numeric order if I simply sorted the values of the options. First thing I thought to do is fix it with jQuery. So I created and uploaded a JavaScript file with a jQuery function I found online for sorting the options by value. Here’s the contents of that file:
jQuery.fn.sortOptionsByValue = function()
{
var byValueSortCallback = function(x, y)
{
var xVal = jQuery(x).val();
var yVal = jQuery(y).val();
return (xVal < yVal) ? -1 : (xVal > yVal) ? 1 : 0;
};
return this.sortOptions(byValueSortCallback);
};
Next thing I did was open up my Magento layout/catalog.xml file and locate the XML block that begins with catalog_product_view. Below that you will find a reference name=”head”. In there I inserted the following action which includes the JavaScript file I created and mentioned above:
<action method="addJs"><script>jQuery/sortselectbox.js</script></action>
In order to specifically fix the option order on this one configurable product I opened up that product in the Magento backend and inserted the following javascript into the product’s short description:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#attribute965').sortOptionsByValue();
jQuery('#attribute965').val('').attr({selected: 'selected'});
});
</script>
This script will sort the options by value alpha numerically (a-z0-9) and then it will put the empty option for “Choose an Option” as the first option and sets selected=”selected” on that option. From there you should be good to go. If you have a much more elegant method to resolving this feel free to let me know.
Magento: Administration for Beginners
Someone asked me today how they can quickly come up to speed on Magento Administration. They said everything seems so overwhelming to manage. I recommend the Magento Beginner’s Guide book shown here in this post.
Amazon has this to say about this book: “Magento is the world’s most evolved e-commerce solution. It runs on the Apache/MySQL/PHP platform. From one installation, you can control multiple storefronts, all sharing customer and product information. Magento’s templates and themes enable you to customize the look and feel of your store, even optimizing it for mobile phones. Extensions enable you to connect Magento to a large number of payment gateways and shipping services. Modular code enables you to upgrade your Magento installation while retaining your customizations. Support is provided free by an active open source community and by subscription to Varien, the company behind Magento.
Magento is one of the most exciting, flexible, and customizable e-commerce systems. It offers you an extensive suite of powerful tools for creating and managing an online store. As your online store grows, you can be sure that this robust e-commerce system can handle your needs. However, getting started with Magento can be difficult without the right guidance.
This book provides that guidance in the form of a step-by-step approach to building a simple, effective online store. The book covers the key features of Magento that will help you get your store up and running. It guides you through installation, configuration, populating your store with products, accepting payments, maintaining relationships with your customers, and fulfilling orders.
When you create an online store with Magento, you usually follow a defined series of steps. This book is arranged to support that process. Each chapter shows you how to get the most from one step.
You will learn to customize the default Magento storefront so that it becomes your store and also about Magento’s directory structure and where some of the elements of a store are customized. This experience will help you if you decide to go beyond this book and install new themes or create your own themes.
As you work your way through each chapter, your store will grow in scope and sophistication. By the time you finish this book, you should have a basic but complete, working online store.”
I hope this book helps you get rolling with your own Magento Administration too!
Mobile Phone: Web page click on phone number to dial
The other day I was working on a site for a friend and I suggested he put his business phone number at the top right hand corner of the website so that his visitors could easily find the number and call his business, thus creating a conversion of sorts. I told him that I frequently look up numbers on other people’s websites using my cell phone and click on the number to dial. He agreed that it sounded like a great idea so I went ahead and placed the number in the page and proceeded to test whether it would allow me to click on it or not. To my surprise it didn’t work. So with a little bit of Googling I found my answer.
Some cell phones require you to format the phone number as a HREF like the one shown below:
<a href="wtai://wp/mc;7132340582">713-234-0582</a>
The iPhone requires you to format the number like this:
<a href="tel:1-713-234-0582">1-713-234-0582</a>