If you have the path alias, you can easily get the normal path and then the node object with two single functions. I assume you have the path alias available in the $path_alias
variable.
$path = drupal_get_normal_path($path_alias);
$node = menu_get_object('node', 1, $path);
Once you have the node object, you just set $node->status
to NODE_PUBLISHED
, and save the node. So the full code becomes the following.
$path = drupal_get_normal_path($path_alias);
if ($node = menu_get_object('node', 1, $path)) {
$node->status = NODE_PUBLISHED;
node_save($node);
}
I am not sure how much safe it's assuming the path aliases are for nodes, but just to be sure, and avoid trying to load a node when Drupal didn't find any normal path matching the path alias, I would use the following code.
$path = drupal_get_normal_path($path_alias);
if (strpos($path, 'node/') === 0 && ($node = menu_get_object('node', 1, $path))) {
$node->status = NODE_PUBLISHED;
node_save($node);
}
Notice that ===
is necessary, since strpos()
can either return 0
to mean the path starts with 'node/'
, or FALSE to mean 'node/'
was not found in the path. (In PHP 0 == FALSE
is TRUE
.)