Overview
This addon allows users to insert icons in posts by referencing HTML entities through a shortcode like [icon name=”heart”]. It provides an admin page for managing icon names and their HTML entity mappings.
The addon comes with pre-installed icons and allows users to add, edit, and delete icons directly from WordPress admin.
Example Prompt.
I want to create a WordPress addon called “EasyCodingWithAI – Custom Icon Shortcodes.” This addon should allow users to insert icons in their posts using HTML entities with a shortcode format like [icon name=”heart”]. The addon needs to have an admin control panel where users can add, edit, and delete icon mappings. It should come preinstalled with a selection of common icons, such as hearts and stars, stored in a custom database table. Please provide all the necessary PHP code to create this addon, ensuring the admin page follows standard WordPress styling.
Plugin Structure
- Folder Name:
easycoding-custom-icon-shortcodes
- Main File:
easycoding-custom-icon-shortcodes.php
Steps to Create the Addon
Step 1: Create Plugin Folder and File
- Access your WordPress installation: Use FTP or your hosting file manager to navigate to the
wp-content/plugins
directory. - Create a new folder: Name it easycoding-custom-icon-shortcodes. The folder name is important as it is used for the plugin’s directory.
- Create the main plugin file: Inside the new folder, create a file named easycoding-custom-icon-shortcodes.php. This file will contain the main code for your plugin.
Step 2: Add Plugin Code
Open the easycoding-custom-icon-shortcodes.php file in a text editor and add the following code:
<?php
/**
* Plugin Name: EasyCodingWithAI - Custom Icon Shortcodes
* Description: Allows users to insert icons in posts using HTML entities. Comes with an admin panel for managing icon mappings.
* Version: 1.0
* Author: EasyCodingWithAI
*/
// Exit if accessed directly.
if (!defined('ABSPATH')) {
exit;
}
// Create the database table on plugin activation.
function easycoding_custom_icons_install() {
global $wpdb;
$table_name = $wpdb->prefix . 'custom_icons'; // Table name
$charset_collate = $wpdb->get_charset_collate();
// SQL to create the table
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name tinytext NOT NULL,
entity_code text NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
// Include the WordPress file to handle database operations
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
// Create the table and handle errors
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
if (!dbDelta($sql)) {
error_log("Database table creation failed for $table_name");
}
}
// Pre-install some common icons
$icons = [
['name' => 'heart', 'entity_code' => '♥'],
['name' => 'star', 'entity_code' => '☆'],
// Add more icons as needed
];
foreach ($icons as $icon) {
$wpdb->insert($table_name, $icon);
}
}
register_activation_hook(__FILE__, 'easycoding_custom_icons_install');
// Register the shortcode
function easycoding_custom_icon_shortcode($atts) {
global $wpdb;
// Extract shortcode attributes
$atts = shortcode_atts(['name' => ''], $atts);
$name = sanitize_text_field($atts['name']);
// Retrieve the icon from the database
$table_name = $wpdb->prefix . 'custom_icons';
$icon = $wpdb->get_row($wpdb->prepare("SELECT entity_code FROM $table_name WHERE name = %s", $name));
// Check if the icon exists
if ($icon) {
return $icon->entity_code; // Return the HTML entity
} else {
error_log("Icon not found: $name");
return ''; // Return an empty string if the icon is not found
}
}
add_shortcode('icon', 'easycoding_custom_icon_shortcode');
// Create the admin menu
function easycoding_custom_icons_menu() {
add_menu_page('Custom Icons', 'Custom Icons', 'manage_options', 'easycoding-custom-icons', 'easycoding_custom_icons_admin_page');
}
add_action('admin_menu', 'easycoding_custom_icons_menu');
// Admin page for managing icons
function easycoding_custom_icons_admin_page() {
global $wpdb;
$table_name = $wpdb->prefix . 'custom_icons';
// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['add_icon'])) {
$name = sanitize_text_field($_POST['icon_name']);
$entity_code = wp_kses_post($_POST['icon_entity_code']);
$wpdb->insert($table_name, ['name' => $name, 'entity_code' => $entity_code]);
echo '<div class="notice notice-success is-dismissible"><p>Icon added successfully!</p></div>';
} elseif (isset($_POST['delete_icon'])) {
$id = intval($_POST['icon_id']);
$wpdb->delete($table_name, ['id' => $id]);
echo '<div class="notice notice-success is-dismissible"><p>Icon deleted successfully!</p></div>';
}
}
// Fetch existing icons
$icons = $wpdb->get_results("SELECT * FROM $table_name");
?>
<div class="wrap">
<h1 class="wp-heading-inline">Custom Icons</h1>
<form method="post" class="post">
<h2>Add New Icon</h2>
<table class="form-table">
<tr>
<th scope="row"><label for="icon_name">Icon Name</label></th>
<td><input type="text" name="icon_name" id="icon_name" required></td>
</tr>
<tr>
<th scope="row"><label for="icon_entity_code">HTML Entity Code</label></th>
<td><input type="text" name="icon_entity_code" id="icon_entity_code" required></td>
</tr>
</table>
<?php submit_button('Add Icon', 'primary', 'add_icon'); ?>
</form>
<h2>Existing Icons</h2>
<table class="wp-list-table widefat striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Entity Code</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php if ($icons): foreach ($icons as $icon): ?>
<tr>
<td><?php echo esc_html($icon->id); ?></td>
<td><?php echo esc_html($icon->name); ?></td>
<td><?php echo esc_html($icon->entity_code); ?></td>
<td>
<form method="post" style="display:inline;">
<input type="hidden" name="icon_id" value="<?php echo esc_attr($icon->id); ?>">
<?php submit_button('Delete', 'secondary', 'delete_icon', false); ?>
</form>
</td>
</tr>
<?php endforeach; else: ?>
<tr>
<td colspan="4">No icons found.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
<?php
}
?>
Step 3: How It Works
Once activated, the plugin creates a custom database table to store icons and their HTML entities.
- Adding Icons: You can add new icons and their corresponding HTML entities via the admin page. Enter the name and entity code in the provided form and click “Add Icon.”
- Using Icons in Posts: To use an icon in your post or page, simply use the shortcode format:
[icon name="icon_name"]
, replacing icon_name
with the name of the icon you want to insert (e.g., [icon name="heart"]
). - Managing Icons: The admin page displays a list of existing icons with options to delete them if needed.
Step 4: Final Check
- Save all your code changes in easycoding-custom-icon-shortcodes.php.
- Upload the easycoding-custom-icon-shortcodes folder to the
/wp-content/plugins/
directory in WordPress. - Go to the Plugins page in WordPress, find EasyCodingWithAI – Custom Icon Shortcodes, and click Activate.
- Visit Settings > Icon Shortcodes to ensure the admin page is functional.
Step 5: Using the Shortcode in Posts and Templates
To use the custom icon shortcode in your WordPress posts or templates, follow these simple steps:
- In Posts or Pages:
- Edit the post or page where you want to insert an icon.
- In the content editor, type the shortcode in the format: [icon name= “icon_name”]
- Replace icon_name with the name of the icon you want to display.
- In Templates:
- You can use the shortcode within your theme templates if you want to use the
do_shortcode()
function. <? echo do_shortcode('[icon name="star"]'); ?>
- This will render the icon in the specified location within your template file.
- Preview Changes:
- After inserting the shortcode, preview your post or page to see the icon displayed as intended.
Following these steps, you can easily add icons to your content, enhancing your posts with visual elements that engage your audience.
Disclaimer
The coding tips and guides provided on this website are intended for informational and educational purposes only. While we strive to offer accurate and helpful content, these tips are meant as a starting point for your own coding projects and should not be considered professional advice.
We do not guarantee the effectiveness, security, or safety of any code or techniques discussed on this site. Implementing these tips is done at your own risk, and we encourage you to thoroughly test and evaluate any code before deploying it on your own website or application.
By using this site, you acknowledge that we are not responsible for any issues, damages, or losses that may arise from your use of the information provided herein.