Monday, September 29, 2014

Change WordPress Language in Theme

If using _e("Hello","textdomain"); to translate theme you need to create an es_es.po file in text editor:

msgid ""
msgstr ""
"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n"
"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"
"Project-Id-Version: Theme Name\n"
"POT-Creation-Date: \n"
"PO-Revision-Date: \n"
"Last-Translator: Mike <w@w.com>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: es_ES\n"
"X-Generator: Poedit 1.6.9\n"
"X-Poedit-SourceCharset: UTF-8\n"

#: ../header.php:58
msgid "Hello"
msgstr "Hola"

Compile it with POEeditor you will get file es_es.mo. Upload both files
/wp-content/themes/{yourTheme}/languages

in functions.php add

add_action('after_setup_theme', 'my_theme_setup');
function my_theme_setup(){
    load_theme_textdomain('textdomain', get_template_directory() . '/languages');
}

In WP backend change language to Spanish. And both your theme and WP backend will be in Spanish. You can use plugin to leave backend in English.

Saturday, August 9, 2014

WordPress Pagination and Search in Backend

This an advanced WordPress tutorial about plugin that creates and searches a table in database. All that is happening in WordPress backend. This is useful when you need to see what your visitor have submitted to you in contact form.


After plugin activation populate table tablePrefix_messages with some

Tuesday, August 5, 2014

jQuery Advanced Event Handling

Event handler remove, attach and event stop propagation.




<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="utf-8">
  <title>Working with events</title>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script>
   $(document).ready(function() {
    var myHandleFunc = function(e) {
     alert('Before `stopPropagation()`');
     e.stopPropagation();
     alert('After `stopPropagation()`');     
    };
    
    $('p').click(myHandleFunc);
  
    $('div.chapter').click(function() {
     alert('I\'m not executed unless you click on yellow or remove event handler for p element');
    });
    
    $('#remove').click(function() {
     $('p').off('click');
    });  
    
    $('#attach').click(function() {
     $('p').on('click', myHandleFunc);
    });              
   });
  </script>
 </head>
 <body>
  <div id="container">
   <div>
    <h2>Click on paragraph <button id="remove">Remove event</button> <button id="attach">Attach event</button></h2>
   </div>
   <div style="padding: 50px;background: yellow;"  class="chapter">
    <p style="background: white;">
     Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </p>
   </div>
  </div>
 </body>
</html>

Saturday, June 28, 2014

Typical SVN Operations on Command Line

1) Checkout
2) Add SVN Ignore property
3) Create branch based on trunk
4) Switch to new branch
5) Merge dry run
6) Real megring

1) svn checkout https://xp-dev.com/svn/donald456/trunk -r HEAD --depth=infinity --force

2) svn propset svn:ignore "index.php" C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/donaldtrunk

3) svn copy -rHEAD https://xp-dev.com/svn/donald456/trunk https://xp-dev.com/svn/donald456/branches/grana001

4) svn switch https://xp-dev.com/svn/donald456/branches/grana001 C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/donaldtrunk -r HEAD --force


5) svn merge --dry-run --depth=infinity https://xp-dev.com/svn/donald456/trunk@HEAD \ 
                                        https://xp-dev.com/svn/donald456/branches/grana002@HEAD \
                                        C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/donaldtrunk \
    --- Merging differences between repository URLs into C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/donaldtrunk
     U C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/donaldtrunk
    Dry-run merge complete.
    ==== Property Statistics: =====
    Updated: 1

6) svn merge --depth=infinity https://xp-dev.com/svn/donald456/trunk@HEAD \
                              https://xp-dev.com/svn/donald456/branches/grana002@HEAD \ 
                              C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/donaldtrunk
    --- Merging differences between repository URLs into C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/donaldtrunk
     U C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/donaldtrunk
    Merge complete.
    ==== Property Statistics: =====
    Updated: 1

Sunday, June 22, 2014

SVN Merge into trunk from development branch

Make sure you are in the trunk when merging. Then right click on your SVN project Team->Merge and fill form like this:

Merging to trunk as destination and branch br2 as development branch



Files from branch br2 will be transferred to trunk. After merging new files are in your working copy so you must commit newly added files to your remote repo.

merge --depth=infinity https://xp-dev.com/svn/qwef/trunk@HEAD https://xp-dev.com/svn/qwef/branches/br2@HEAD C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/atrunk
    --- Merging differences between repository URLs into C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/atrunk
    A  C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/atrunk/docs/num4.txt
     G C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/atrunk
    Merge complete.
    ===== File Statistics: =====
    Added: 1
    ==== Property Statistics: =====
    Merged: 1
commit -m "added num4.txt" C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/atrunk/docs/num4.txt
    Adding         Documents/Aptana Studio 3 Workspace/atrunk/docs/num4.txt
    Committed revision 75.

And this is the output created in console in Eclipse

Monday, May 12, 2014

PHP Design Patterns - The Observer Pattern

In this case class NoticeBoard is publisher and classes Student and Teacher are subscribers

<?php
class NoticeBoard{
  private $_observers = array();

  public function addNote( $name )  {
    foreach( $this->_observers as $obs )
      $obs->onChanged( $this, $name );
  }

  public function addObserver( $observer )  {
    $this->_observers []= $observer;
  }
}

class Student{
  public function onChanged( $sender, $args )  {
    echo("The ".__CLASS__." is informed '$args' \n" );
  }
}

class Teacher{
  public function onChanged( $sender, $args )  {
    echo( "The ".__CLASS__." is informed '$args' \n" );
  }
}

$s = new Student(); 
$t = new Teacher();

$nb = new NoticeBoard(); 
$nb -> addObserver($s); 
$nb -> addObserver($t);

$nb->addNote('Due storm exams are postponed.');
?>

Result is
The Student is informed 'Due storm exams are postponed.'
The Teacher is informed 'Due storm exams are postponed.'

Thursday, March 27, 2014

Consuming SOAP services

Full web service description is at http://www.webservicex.net/ws/WSDetails.aspx?CATID=12&WSID=64
Yellow highlighted portion is SOAP Web Service URI
Green Highlighted portion is the method name.

Monday, March 24, 2014

Post installation tasks on CentOS regarding Apache web server

This is a short troubleshooting guide for system administrators. Almost all tasks described in this post should be done as root. You need to be familiar with basic Linux commands / editors such as cat, vi, chmod and chown.

Wednesday, March 19, 2014

Wednesday, March 12, 2014

Most Useful PHP Snippets

Dumping arrays or objects with HTML markup text as member. This prevents creating iFrame and similar tags in your debug output.

Monday, March 10, 2014

Git Workflow for Local and Remote Repository

Typical workflow for two developer that clone/fetch/push to a remote git repo. Each developer use one tracking branch. Once changes are pushed to remote repo we use rebase twice to merge the local branches into the master branch.

Tuesday, March 4, 2014

Git resolving conflict on remote machine

Git conflict resolution on remote server after two branches merging into master branch
Branches are master, armand and ralph.

Monday, March 3, 2014

OOP Basics

/**
 * Instances CANNOT use private and protected members
 * Children CAN inherit only public and protected members
 */
class MyClass
{
    public $name = 'John';
    protected $username = 'JohnT23';
    private $password = 'secret';

    function printAll()
    {
        echo "From class MyClass:";
        echo $this->name." ";
        echo $this->username." ";
        echo $this->password." ";
    }
}

$obj = new MyClass();
echo "From Instance \$obj:".$obj->name."\n"; //Works
//echo $obj->username; // Fatal Error
//echo $obj->password; // Fatal Error
$obj->printAll();

echo"\n<br>";

class MyClass2 extends MyClass
{
    // We can re-declare/access the public and protected method
    // but not private ones
    function printAll()
    {
        echo "From class MyClass2:";
        echo $this->name." ";
        echo $this->username." ";
        // Children cannot access private member of parent
        // echo $this->password; // Error
    }
}

$obj2 = new MyClass2();
echo "<br>
 From Instance \$obj2:".$obj2->name; // Works
//echo $obj->username; // Fatal Error
//echo $obj->password; // Fatal Error
$obj2->printAll();

Sunday, February 23, 2014

Testing File Permission for file upload and database connectivity

On a freshly installed Linux server you need to test file permission and database connectivity.
Here are some scripts that can help you with that:

Wednesday, February 12, 2014

Git protocol on server and client side

This is an advanced tutorial for experienced users that are familiar with git, SSH protocols and shell scripts.
 
On server side

[user@test_server ~]$ git daemon --base-path=/var/www/html --reuseaddr  --export-all --verbose --enable=receive-pack 

Do not close the terminal on server.
 --base-path=/var/www/html is path to your repository.

Saturday, January 18, 2014

Two Way Encryption

Two way encryption on PHP works with mcrypt extension enabled.
Here's a simple snippet that uses password and generates different encryption string every time you reload.
<?php
$string = "This is a string to be encrypted";
$key = "This is a key";
// Encryption Algorithm
$alg = MCRYPT_TWO_FISH;
// Create the initialization vector for added security
$iv = mcrypt_create_iv(mcrypt_get_iv_size($alg, MCRYPT_MODE_ECB), MCRYPT_RAND);
// Output original string
print "Original string: $string <p>";
// Encrypt $string
$encrypted_string = mcrypt_encrypt($alg, $key, $string, MCRYPT_MODE_CBC, $iv);
// Convert to hexadecimal and output to browser
print "Encrypted string: ".bin2hex($encrypted_string)."<p>";
$decrypted_string = mcrypt_decrypt($alg, $key, $encrypted_string, MCRYPT_MODE_CBC, $iv);

print "Decrypted string: $decrypted_string";
?>
A short video how code works

Saturday, January 11, 2014

Two aliases for the same MySQL table

Here's how to establish database relationships between connected entities. In this example we'll use a highways map. The final query will show city names and highway that connects them in  result set rows.

Map of highways:

Connections between cities
Connections between cities