How to programmatically set node alias on node save I need to set a special path alias for nodes created by users in the template.php file of my theme. the pathauto module is enabled.
I already tried the solutions that are given in the following links


Create path alias programatically when node is created
How To Set Node Path/Alias Programatically
Programmatically set path alias


But none of them worked. Here is a few codes I tried but I failed.

function THEME_nodeapi(&amp;$node, $op, $a3=NULL, $a4=NULL){
    if($node-&gt;type == "image"){
        switch ($op){
            case "insert":
                $default_node_path = 'node/'.$node-&gt;nid;
                $alternate_url = "SOME SPECIAL PATH";
                path_set_alias($default_node_path, $alternate_url, 0, '');
                break;
        }
    }
}


or this one

function THEME_insert($node){
    $path = "SOME SPECIAL PATH";
    $node-&gt;path = array('alias' =&gt; $path, 'pathauto' =&gt; FALSE);
}

function THEME_node_insert($node){
    $path = "SOME SPECIAL PATH";
    $node-&gt;path = array('alias' =&gt; $path, 'pathauto' =&gt; FALSE);
}


or

function THEME_nodeapi(&amp;$node, $op, $a3=NULL, $a4=NULL){
    if($node-&gt;type == "image"){
        switch ($op){
            case "insert":
                $node-&gt;path = t("SOME SPECIAL PATH");
                break;
        }
    }
}


How can I do set path alias programmatically ?



UPDATE

I tried this code that recommended by @Ajit S and it didn't work again

&lt;?php
    function THEME_node_insert($node){
        $alias = "arar/".$node-&gt;nid;
        $lang_code = "en";
        $path = array(
          'source' =&gt; "node/{$node-&gt;nid}",
          'alias' =&gt; $alias,  // Any alias that you want to set.
          'language' =&gt; $lang_code,  // Optional, if you are working on a multilingual site.
        );
        path_save($path);
    }
Path alias could be set using the path module in the core. Just call path_save the following from a function (or hook) of your choice. In your case I'd recommend you to implement the following code in hook_node_insert, because it is called after a node has been saved, and the node will have a valid nid.

// Assuming you have a $node object already.
$path = array(
  'source' =&gt; "node/{$node-&gt;nid}",
  'alias' =&gt; $alias,  // Any alias that you want to set.
  'language' =&gt; $lang_code,  // Optional, if you are working on a multilingual site.
);
path_save($path);


If you have global redirect set, the node page will be redirected to the alias.