Your function requires an input argument $user
in order to work.
If you simply call wp_user_id()
without any argument it will return an error.
The solution is giving $user
a default value, making the argument optional.
function wp_user_id( $user = NULL ) { // Give $user a default value of NULL
if ( empty($user) ) { // check if $user has a non-empty value
if ( is_page_template( 'page-recipes.php' ) ) {
$user = get_current_user_id();
} else if ( is_singular() ) {
$user = get_the_author_meta( 'ID' );
}
}
return $user;
}
EDIT: I just realised your $user
argument is actually pointless in your example function. You could remove it entirely.
function wp_user_id() {
if ( is_page_template( 'page-recipes.php' ) ) {
$user = get_current_user_id();
} else if ( is_singular() ) {
$user = get_the_author_meta( 'ID' );
}
if ( isset($user) ) { // check if $user is set
return $user;
}
}