Showing posts with label tips n tricks. Show all posts
Showing posts with label tips n tricks. Show all posts

17 May, 2012

Insert 'Read More' button in Blogger Imported posts in Wordpress

If you have just imported your posts and comments from Blogger to WordPress then you might have faced two problems:
1. There is a '>' symbol prepended to each post title and post content.
2. Your 'Read more' button from blogger posts is missing.
I have found a simple solution for these problems.

Removing '>' symbol form WordPress posts

To remove '>' symbol from post titles and content, just run following two queries on you wordpress database:
UPDATE wp_posts SET post_title = TRIM(LEADING '>' FROM post_title)
UPDATE wp_posts SET post_content = TRIM(LEADING '>' FROM post_content)

Adding 'Read More' link in WordPress posts

To add 'Read More' link in blogger imported posts you need to run following MySql query on your wordpress database:
UPDATE wp_posts SET post_content = REPLACE(post_content, "<a name='more'></a>", '<!--more-->');
UPDATE wp_posts SET post_content = REPLACE(post_content, "<a name="more"'></a>", '<!--more-->');
Blogger identifies the 'Read More' link by "<a name='more'></a>" code snippet. But WordPress does this by finding "<!--more-->". So we have replaced all occurrences of "<a name='more'></a>" with "<!--more-->". Hope this works for you.
Note: Please take backup of your database before applying above hacks.

Insert 'Read More' button in Blogger Imported posts in Wordpress

If you have just imported your posts and comments from Blogger to WordPress then you might have faced two problems:
1. There is a '>' symbol prepended to each post title and post content.
2. Your 'Read more' button from blogger posts is missing.
I have found a simple solution for these problems.

Removing '>' symbol form WordPress posts

To remove '>' symbol from post titles and content, just run following two queries on you wordpress database:
UPDATE wp_posts SET post_title = TRIM(LEADING '>' FROM post_title)
UPDATE wp_posts SET post_content = TRIM(LEADING '>' FROM post_content)

Adding 'Read More' link in WordPress posts

To add 'Read More' link in blogger imported posts you need to run following MySql query on your wordpress database:
UPDATE wp_posts SET post_content = REPLACE(post_content, "<a name='more'></a>", '<!--more-->');
UPDATE wp_posts SET post_content = REPLACE(post_content, "<a name="more"'></a>", '<!--more-->');
Blogger identifies the 'Read More' link by "<a name='more'></a>" code snippet. But WordPress does this by finding "<!--more-->". So we have replaced all occurrences of "<a name='more'></a>" with "<!--more-->". Hope this works for you.
Note: Please take backup of your database before applying above hacks.

08 May, 2012

How to Test PHP Emails on Localhost

While developing web applications on localhost you must have always missed the functionality to send mails. As localhost does not support email service, so it has always been a bottleneck for email testing on localhost. But I have found a simple way to test emails on localhost using PHP. I have overridden the default mail() function of PHP to create a text/html file based on the email format. If the email is in html format, an HTML file is created otherwise a text file is created. You can see in this file what exactly is the content sent in your mail. See how does it work.

Live demo Download Script

Overriding the mail() function

To override the PHP mail() function, we need to create our namespace and then define the function in that namespace.
#Create your own namespace
namespace localMail;

#Override the default mail function
function mail( $to, $subject, $message, $additional_headers='', $additional_parameters='' ) {
if (empty($to) || empty($subject) || empty($message)) {
return false;
}

#Check if email is sent as HTML or plaintext
if (isHTML($additional_headers)) {
$ext = 'html';
}else{
$ext = 'txt';
}

#Write the file
$myFile = "mail_".time().".".$ext;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $message);
fclose($fh);
}

#Function to detect HTML headers
function isHTML($isithtml) {
if (strpos($isithtml, 'html')) {
return true;
} else {
return false;
}
}

Usage

<html>
<head>
<title>Test Emails on localhost ! WebSpeaks.in</title>
</head>
<body>
<form method="post" action="send_mail.php">
<table align="center">
<tr>
<td>
Type your message<br />
<textarea name="message" cols="50" rows="5"><div style="color:#c3c;">This <i>email</i> is <u>sent</u> through <a href="#">localhost</a>.</div></textarea>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" class="button" name="send_text" value="Send as Plain Text" />
<input type="submit" class="button" name="send_html" value="Send as HTML" />
</td>
</tr>
</table>
</form>
</body>
</html>

send_mail.php

<?php
namespace localMail;
#Override the default mail function
function mail( $to, $subject, $message, $additional_headers='', $additional_parameters='' ) {
if (empty($to) || empty($subject) || empty($message)) {
return false;
}

#Check if email is sent as HTML or plaintext
if (isHTML($additional_headers)) {
$ext = 'html';
}else{
$ext = 'txt';
}

#Write the file
$myFile = "mail_".time().".".$ext;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $message);
fclose($fh);

header('Content-disposition: attachment; filename='.$myFile);
if (isHTML($additional_headers)) {
header('Content-type: application/html');
} else {
header('Content-type: application/text');
}
readfile($myFile);
unlink($myFile);
die();
}

#Function to detect HTML headers
function isHTML($isithtml) {
if (strpos($isithtml, 'html')) {
return true;
} else {
return false;
}
}

if (isset($_POST) && !empty($_POST)) {
$to="bhardwajs.on.height@gmail.com";
$subject="Test Local Mail on WebSpeaks";
$message = $_POST['message'];
if (isset($_POST['send_text']) && !empty($_POST['send_text'])) {
mail($to, $subject, $message);
} else {
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
}
}
?>

How to Test PHP Emails on Localhost

While developing web applications on localhost you must have always missed the functionality to send mails. As localhost does not support email service, so it has always been a bottleneck for email testing on localhost. But I have found a simple way to test emails on localhost using PHP. I have overridden the default mail() function of PHP to create a text/html file based on the email format. If the email is in html format, an HTML file is created otherwise a text file is created. You can see in this file what exactly is the content sent in your mail. See how does it work.

Live demo Download Script

Overriding the mail() function

To override the PHP mail() function, we need to create our namespace and then define the function in that namespace.
#Create your own namespace
namespace localMail;

#Override the default mail function
function mail( $to, $subject, $message, $additional_headers='', $additional_parameters='' ) {
if (empty($to) || empty($subject) || empty($message)) {
return false;
}

#Check if email is sent as HTML or plaintext
if (isHTML($additional_headers)) {
$ext = 'html';
}else{
$ext = 'txt';
}

#Write the file
$myFile = "mail_".time().".".$ext;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $message);
fclose($fh);
}

#Function to detect HTML headers
function isHTML($isithtml) {
if (strpos($isithtml, 'html')) {
return true;
} else {
return false;
}
}

Usage

<html>
<head>
<title>Test Emails on localhost ! WebSpeaks.in</title>
</head>
<body>
<form method="post" action="send_mail.php">
<table align="center">
<tr>
<td>
Type your message<br />
<textarea name="message" cols="50" rows="5"><div style="color:#c3c;">This <i>email</i> is <u>sent</u> through <a href="#">localhost</a>.</div></textarea>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" class="button" name="send_text" value="Send as Plain Text" />
<input type="submit" class="button" name="send_html" value="Send as HTML" />
</td>
</tr>
</table>
</form>
</body>
</html>

send_mail.php

<?php
namespace localMail;
#Override the default mail function
function mail( $to, $subject, $message, $additional_headers='', $additional_parameters='' ) {
if (empty($to) || empty($subject) || empty($message)) {
return false;
}

#Check if email is sent as HTML or plaintext
if (isHTML($additional_headers)) {
$ext = 'html';
}else{
$ext = 'txt';
}

#Write the file
$myFile = "mail_".time().".".$ext;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $message);
fclose($fh);

header('Content-disposition: attachment; filename='.$myFile);
if (isHTML($additional_headers)) {
header('Content-type: application/html');
} else {
header('Content-type: application/text');
}
readfile($myFile);
unlink($myFile);
die();
}

#Function to detect HTML headers
function isHTML($isithtml) {
if (strpos($isithtml, 'html')) {
return true;
} else {
return false;
}
}

if (isset($_POST) && !empty($_POST)) {
$to="bhardwajs.on.height@gmail.com";
$subject="Test Local Mail on WebSpeaks";
$message = $_POST['message'];
if (isset($_POST['send_text']) && !empty($_POST['send_text'])) {
mail($to, $subject, $message);
} else {
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
}
}
?>

02 May, 2012

Add/Update Attribute Option Values Programmatically in Magento

Adding/Updating the attribute option values programmatically in Magento can be tedious sometimes. Adding attribute options through custom codes is not big deal but updating the values for already added attribute options is tricky enough.
Problem:Lets understand this by an example. Suppose we have a product attribute 'manufacturer'. It is of type select/dropdown. And we have few options added in it like Mac, Google, Android etc. Now at some point in our Magento application we want to update these options with Apple, Nexus, HTC etc without using the admin panel, then it is a problem.


Solution:To update the option values programmatically, I came up with a simple solution which is as follows. For this, you need to find the 'option_id' of the attribute options in the 'eav_attribute_option_value' table.

Then create an option array and save it in the appropriate model.


Updating Attribute options

//Get the eav attribute model
$attr_model = Mage::getModel('catalog/resource_eav_attribute');

//Load the particular attribute by id
//Here 73 is the id of 'manufacturer' attribute
$attr_model->load(73);

//Create an array to store the attribute data
$data = array();

//Create options array
$values = array(
//15 is the option_id of the option in 'eav_attribute_option_value' table
15 => array(
0 => 'Apple' //0 is current store id, Apple is the new label for the option
),
19 => array(
0 => 'Nexus'
),
12 => array(
0 => 'HTC'
),
);

//Add the option values to the data
$data['option']['value'] = $values;

//Add data to our attribute model
$attr_model->addData($data);

//Save the updated model
try {
$attr_model->save();
$session = Mage::getSingleton('adminhtml/session');
$session->addSuccess(
Mage::helper('catalog')->__('The product attribute has been saved.'));

/**
* Clear translation cache because attribute labels are stored in translation
*/
Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
$session->setAttributeData(false);
return;
} catch (Exception $e) {
$session->addError($e->getMessage());
$session->setAttributeData($data);
return;
}
This is all and you are done.

Adding new Attribute option

$arg_attribute = 'manufacturer';
$arg_value = 'value to be added';

$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
$attr_id = $attr->getAttributeId();

$option['attribute_id'] = $attr_id;
$option['value']['any_option_name'][0] = $arg_value;

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);

Add/Update Attribute Option Values Programmatically in Magento

Adding/Updating the attribute option values programmatically in Magento can be tedious sometimes. Adding attribute options through custom codes is not big deal but updating the values for already added attribute options is tricky enough.
Problem:Lets understand this by an example. Suppose we have a product attribute 'manufacturer'. It is of type select/dropdown. And we have few options added in it like Mac, Google, Android etc. Now at some point in our Magento application we want to update these options with Apple, Nexus, HTC etc without using the admin panel, then it is a problem.


Solution:To update the option values programmatically, I came up with a simple solution which is as follows. For this, you need to find the 'option_id' of the attribute options in the 'eav_attribute_option_value' table.

Then create an option array and save it in the appropriate model.


Updating Attribute options

//Get the eav attribute model
$attr_model = Mage::getModel('catalog/resource_eav_attribute');

//Load the particular attribute by id
//Here 73 is the id of 'manufacturer' attribute
$attr_model->load(73);

//Create an array to store the attribute data
$data = array();

//Create options array
$values = array(
//15 is the option_id of the option in 'eav_attribute_option_value' table
15 => array(
0 => 'Apple' //0 is current store id, Apple is the new label for the option
),
19 => array(
0 => 'Nexus'
),
12 => array(
0 => 'HTC'
),
);

//Add the option values to the data
$data['option']['value'] = $values;

//Add data to our attribute model
$attr_model->addData($data);

//Save the updated model
try {
$attr_model->save();
$session = Mage::getSingleton('adminhtml/session');
$session->addSuccess(
Mage::helper('catalog')->__('The product attribute has been saved.'));

/**
* Clear translation cache because attribute labels are stored in translation
*/
Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
$session->setAttributeData(false);
return;
} catch (Exception $e) {
$session->addError($e->getMessage());
$session->setAttributeData($data);
return;
}
This is all and you are done.

Adding new Attribute option

$arg_attribute = 'manufacturer';
$arg_value = 'value to be added';

$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
$attr_id = $attr->getAttributeId();

$option['attribute_id'] = $attr_id;
$option['value']['any_option_name'][0] = $arg_value;

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);

 

© 2013 Jquery Giants. All rights resevered. Designed by Templateism

Back To Top