Change user role when user’s first post gets published

There is a syntax error in the add_action(). You’ve used a period . instead of a comma , to separate the action and the callback. Otherwise the callback seems to work as expected when I tested it locally.

The correct syntax would be,

add_action( 'pending_to_publish', 'my_function', 10, 1 );

When doing development work with WordPress it is a good idea to have debugging on. This way you can check the log file for any errors when something is not working as expected. Further reading, Debugging in WordPress.


P.S. If you want the role change to happen only when your custom post type is published, then add a post type check to the top of the callback. Like so,

function my_function( $post )
{
    if ( 'portfolio' !== $post->post_type ) {
        return;
    }
    
    // code...
}