I want to add a custom function to my wordpress site functions.php file like this :
//Test adding basic REST API custom endpoint. /** * Grab latest post title by an author! * * @param array $data Options for the function. * @return string|null Post title for the latest, * or null if none. */ function my_awesome_func( $data ) { $posts = get_posts( array( 'author' => $data['id'], ) ); if ( empty( $posts ) ) { return null; } return $posts[0]->post_title; } add_action( 'rest_api_init', function () { register_rest_route( 'walden/v1', '/author/(?P<id>d+)', array( 'methods' => 'GET', 'callback' => 'my_awesome_func', ) ); } );
But wherever I paste this code it causes the whole functions to fail and all rest api links give me error .
Exactly where I should paste this piece of code in functions.php so it won’t ruin all the other functions ?
Answer
I Figured out the only problem was actually the name of the function itself , so I changes the name of the function and it works perfectly now