Pages

Wednesday, December 19, 2012

Changing Wordpress DB all tables prefix - PHP code snippet

The following code will change the Wordpress  table prefix.
We need to make some corrections also as mentioned below:

<?php
$host='localhost';
$user='root';
$pass='';
$dbase = 'wordpress';

$db = mysqli_connect($host,$user, $pass, $dbase);

$allTables = $db->query('show tables');

while( $row = mysqli_fetch_array($allTables) ):
    $table = $row[0];
    $tSchema = $db->query('RENAME TABLE `wordpress`.`'.$table.'` TO `wordpress`.`'.str_replace('wp_', 'newprefix_', $table).'` ' );
endwhile;   
?>

The `wp_user_roles` need to be `newprefix_user_roles` of wp_options table

 and

`wp_capabilities` to `newprefix_capabilities` of wp_usermeta table.


Monday, April 23, 2012

Wordpress database modification during migration

To setup an existing Wordpress DB into the new server we should run these minimal 2 queries to adopt all required URL changes. It is exactly changing the Site URLs from old one to new one.


update `wp_options` set `option_value` = replace(`option_value`,'<SOURCE URL>' , '<DESTINATION URL>')

update `wp_posts` set `post_content` = replace( `post_content`,'<SOURCE URL>' , '<DESTINATION URL>'),  `guid` = replace( `guid`,'<SOURCE URL>' , '<DESTINATION URL>')

Here wp may need to changed with required prefix.

We have changed only 2 tables

  • wp_options  and
  • wp_posts

 A sample Php code is available here.
Please Note:

If Page/post is using Permalink other than post/page id, e.g. Post Name, you have to go to Setting -> Permalink and re-save the settings to be effective in new site.

Tuesday, March 6, 2012

Wordpress emailing with attachment

Some of the code lines are not visible, just copy the code block and paste into a PHP editor - entire code will be available.

 
<?php 
    require_once ABSPATH . WPINC . '/class-phpmailer.php';
    $mail_to_send = new PHPMailer(); 
    $mail_to_send->FromName = $_REQUEST['name'];
    $mail_to_send->From     = $_REQUEST['email'];
    $mail_to_send->Subject  = 'This is Subject';
    $mail_to_send->Body     = 'This is Body'
    $mail_to_send->AddReplyTo($_REQUEST['email']);
    $mail_to_send->AddAddress( 'to@todomain.???' ); //destination e-mail
    // e.g. $mail_to_send->AddAddress('abedin.fakhrul@gmail.com');
 
    // Attachment
    if ( !$_FILES['attachment']['error'] == 4 ) { //something was send        
        $extTest=true;
        $extTest = preg_match('/\.(doc|docx|pdf)$/', $_FILES['attachment']['name']);
        //var_dump($_FILES['attachment']['error']);
        if ( $_FILES['attachment']['error'] == 0 && is_uploaded_file($_FILES['attachment']['tmp_name']) && $extTest ) { 
            $mail_to_send->AddAttachment($_FILES['attachment']['tmp_name'], $_FILES['attachment']['name']);        
            if ( !$mail_to_send->Send() ) wp_die('Error: e-mail can not be sent.');
        }
    } else $mail_to_send->Send();
?>

Sunday, February 19, 2012

Thumbnail generation of 3 types of images (jpeg, gif and png)

Here is a code block used in a cakephp controller to generate thumbnails of 3 types of images:
I have used the extract function and the output for the pathinfo funciton.

The parameter $file receives the full system path+filename
$id - is used in part of the thumbnail name since each file has a record in db on my app

    function makeThumb($file, $id ) {
        extract( pathinfo($file) );
        //$dirname, $basename, $extension, $filename
       
        // Get new sizes
        list($width, $height) = getimagesize($file);
        $newheight = 180;//$height * $percent;
        $newwidth = $width/$height*$newheight;

        // Load
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        switch(  $extension ) {
            case 'jpg': $source = imagecreatefromjpeg($file); break;
            case 'gif': $source = imagecreatefromgif($file); break;
            case 'png': $source = imagecreatefrompng($file); break;
        }
        // Resize
        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        // Output
        $name = 'thumb_'.$id.'.'.$extension;
        switch( $extension  ) {
            case 'jpg': imagejpeg($thumb, $dirname.'/'.$name, 100); break;
            case 'gif': imagegif($thumb, $dirname.'/'.$name, 100); break;
            case 'png': imagepng($thumb, $dirname.'/'.$name, 100); break;
        }
    }

Monday, February 13, 2012

bangla.org.bd এর এডিটরে কিছু স্পেশাল চিহ্ন লেখার নিয়ম

bangla.org.bd এর এডিটরে কিছু স্পেশাল চিহ্ন লিখতে হয় এভাবে

ৎ খন্ডক - ত লিখতে হয় শিফ্ট+ ২xটি  (Shift+2XT)
ং  অনুঃস্বার লিখতে হয় শিফ্ট+এন-জি (Shift+N-G)
ঃ  বিস্বর্গ লিখতে হয় - শিফ্ট+এইচ (Shift+H)
ঁ চন্দ্র বিন্দু - শিফ্ট+ছয় (Shift + 6 or ^)

ঞ -  (N+Y)
ড় - r+h
ঢ় - r+hh

Tuesday, January 24, 2012

Checking and prepending `http://` in a link

Sometimes users/site admins are required to submit some URL, e.g. Facebook fanpage url or Twitter URL - that is an outbound link.

But HTML anchor tag href attribute does not work properly without http:// at the beginning of the url string.

So, we should have a validity check. The below line can do the work and will add the http:// if user does not give it.

In PHP:

if( stristr( $url, 'http://' )===false ) $url = 'http://'.$url;

Monday, January 23, 2012

Wordpress: Ordering posts by Meta value

In the query_posts functions parameter, we need to add this:

meta_key=<KEY_NAME>&orderby=meta_value_number&order=DESC

Replace <KEY_NAME> the with your meta key name For example:


<?php query_posts( 'meta_key=<KEY_NAME>&orderby=meta_value_number&order=ASC' ); ?>


Order Could be ASC - Ascending or DESC - descending