I created a custom admin menu:
function new_menu() {
add_menu_page(
'New Menu',
'New Menu',
'administrator',
'new_menu',
'new_menu_content'
);
}
add_action('admin_menu', 'new_menu');
Inside that new menu, I get some data from a Database:
function fishing_data_content($conn){
require_once(get_template_directory() . '/connect.php');
require_once(get_stylesheet_directory() . '/data.php');
}
The connect.php
connects to the DB:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "data";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username,
$password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo "Error: " . $e->getMessage();
}
And the data.php
fetches some data from that DB:
$stmt = $conn->prepare('SELECT * FROM posts');
$stmt->execute();
$values = $stmt->fetchAll();
foreach($values as $val){
echo $val . '<br>';
}
}
Also I tried to require the connection file inside the data.php:
require_once(dirname(__FILE__) . '/connect.php');
But still having the same error:
Fatal error: Call to a member function prepare() on string in /home/public_html/wordpress/wp-content/themes/theme/data.php on line 10