Creating a WordPress plugin involves several steps, and developing one that connects to a MySQL server with password-protected tables requires careful handling of credentials and database connections. Below is a simplified example of a WordPress plugin that retrieves and displays data from the specified MySQL tables.
Please note that handling database credentials in code is a security risk. It’s recommended to use WordPress options or environment variables to store sensitive information. Additionally, you need to consider security practices, error handling, and proper escaping to prevent SQL injection.
- Create a new folder for your plugin:
- Create a folder in the WordPress plugins directory (e.g.,
wp-content/plugins/my-mysql-plugin
).
- Create a folder in the WordPress plugins directory (e.g.,
- Create the main plugin file (my-mysql-plugin.php):
<?php
/*
Plugin Name: My MySQL Plugin
Description: Display data from MySQL server.
Version: 1.0
Author: Valter Schmaltz
*/
// Enqueue scripts and styles
function enqueue_plugin_scripts() {
wp_enqueue_style('mysql-plugin-style', plugin_dir_url(__FILE__) . 'style.css');
}
add_action('wp_enqueue_scripts', 'enqueue_plugin_scripts');
// Shortcode to display data
function display_mysql_data() {
// Connect to the database (Replace with your actual database credentials)
$db_host = 'your_db_host';
$db_user = 'your_db_user';
$db_password = 'your_db_password';
$db_name = 'your_db_name';
$connection = new mysqli($db_host, $db_user, $db_password, $db_name);
// Check for a successful connection
if ($connection->connect_error) {
return 'Error connecting to the database.';
}
// Fetch data from the mysql_table table
$query = "SELECT * FROM mysql_table";
$result = $connection->query($query);
// Display data
$output = '<div class="mysql-plugin">';
$output .= '<h2>MySQL Data</h2>';
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$output .= '<p>' . esc_html($row['your_column']) . '</p>';
// Add more columns as needed
}
} else {
$output .= '<p>No data found.</p>';
}
$output .= '</div>';
// Close the database connection
$connection->close();
return $output;
}
add_shortcode('mysql_data', 'display_translation_data');
Create a CSS file for styling (style.css):
.mysql-plugin {
border: 1px solid #ccc;
padding: 15px;
margin: 15px 0;
}
Usage:
- Add the shortcode
[mysql_data]
to any WordPress post or page where you want to display the data.
Remember to replace the placeholder credentials with your actual database details. Ensure that you follow best practices for security, and consider using WordPress options or environment variables for sensitive information. Additionally, handle errors gracefully and sanitize/escape data to prevent security vulnerabilities.
Debugging Output: Add some debugging output to your shortcode function to see if it’s being called and if there are any issues with the database queries. For example:
function display_mysql_data() {
// ... existing code ...
// Debugging output
echo '<pre>';
var_dump($result); // Check the query result
echo '</pre>';
// ... existing code ...
}
Download the code from GitHub
Comments are closed