Hello I am trying to create a new rest API endpoint that connects to my database querying with mysql and then returning the id number from the school. However, the endpoint is not registering and I am getting an error from wordpress that states the following: The plugin generated 3 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
The code looks okay to me so I am unsure to exactly why I am getting this error.
<?php
/*
Plugin Name: College ID API
* Requires PHP: 7.2
*/
include connect.php
function collegeid()
{
$db = new mysqli('localhost', $dbuser, $dbpass, $dbname);
$queryusers = "SELECT 'id' FROM `Colleges` ";
$dbc = mysqli_query($db, $queryusers);
return $dbc;
};
add_action( 'rest_api_init', function () {
register_rest_route( 'schfind/v2', '/schools/', array(
'methods' => 'GET',
'callback' => ['collegeid'],
'permission_callback'=>'__return_true'
) );
} );
?>
For the final result I am just trying to be able to access the data using the namespace and custom endpoint. It would be a huge help any help at all really because I am stumped as to the issue. The output when visiting the link is:
"The handler for the route is invalid." Error 500
Here is the answer for your question. In my answer I have made some changes with the include and also comment out the return to make your function work.
include "connect.php";
function collegeid()
{
//Comment the below line to work on your query.
return 'Howdy!!!';
//Comment the above line to make your function work.
$db = new mysqli('localhost', $dbuser, $dbpass, $dbname);
$queryusers = "SELECT 'id' FROM `Colleges` ";
$dbc = mysqli_query($db, $queryusers);
return $dbc;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'schfind/v2', '/schools/', array(
'methods' => 'GET',
'callback' => 'collegeid',
'permission_callback'=>'__return_true'
) );
} );
Github link for full plugin link : https://github.com/SHABU89/wordpress_rest_api_help_in_stackoverfolw