Overview
The EasyCodingWithAI – Exclude Pages from Search plugin enables you to exclude specific pages or hide all pages from WordPress search results.
Example Prompt.
Help me create a WordPress plugin called “EasyCodingWithAI – Exclude Pages from Search” that allows administrators to easily exclude specific pages from appearing in search results. I want the admin to have a straightforward settings page where they can select the pages to hide, along with an option to hide all pages if needed. The plugin should notify the admin when changes are saved and display a friendly message if a user searches for excluded content. Additionally, I’d like the plugin to have custom styling options for the settings page to match the WordPress admin dashboard.
Plugin Creation Steps
Step 1: Create the Plugin Folder
- Navigate to your WordPress installation directory, typically found at
wp-content/plugins/
.
- Create a new folder named easycoding-exclude-pages-search.
Step 2: Create the Main Plugin File
- Inside the easycoding-exclude-pages-search folder, create a PHP file named easycoding-exclude-pages-search.php.
- Open the file and add the plugin header information and initial setup code.
<?php
/*
Plugin Name: EasyCodingWithAI - Exclude Pages from Search
Description: Allows excluding specific pages or hiding all pages from WordPress search results.
Version: 1.0
Author: EasyCodingWithAI
*/
if (!defined('ABSPATH')) exit;
// Load the admin panel functions
require_once plugin_dir_path(__FILE__) . 'admin/easycoding-exclude-pages-search-admin.php';
// Add the admin menu
add_action('admin_menu', 'easycoding_exclude_pages_menu');
function easycoding_exclude_pages_menu() {
add_menu_page(
'Exclude Pages from Search',
'Exclude Pages',
'manage_options',
'exclude-pages-search',
'easycoding_exclude_pages_admin_panel',
'dashicons-hidden',
20
);
}
// Enqueue admin styles
add_action('admin_enqueue_scripts', 'easycoding_exclude_pages_styles');
function easycoding_exclude_pages_styles() {
wp_enqueue_style(
'easycoding-admin-style',
plugins_url('assets/css/easycoding-exclude-pages-search-admin.css', __FILE__)
);
}
// Exclude pages based on options
add_action('pre_get_posts', 'easycoding_exclude_pages_from_search');
function easycoding_exclude_pages_from_search($query) {
if ($query->is_search && !is_admin() && $query->is_main_query()) {
$exclude_pages = get_option('easycoding_exclude_pages', []);
if (!empty($exclude_pages)) {
$query->set('post__not_in', $exclude_pages);
}
}
}
?>
Step 3: Create the Admin Panel File
- Inside the easycoding-exclude-pages-search folder, create a subfolder named admin.
- Within the admin folder, create a PHP file named easycoding-exclude-pages-search-admin.php.
- Open the file and set up the admin panel functionality.
<?php
function easycoding_exclude_pages_admin_panel() {
if (!current_user_can('manage_options')) return;
// Process form submission
if (isset($_POST['save_exclusions'])) {
$exclude_pages = isset($_POST['exclude_pages']) ? array_map('intval', $_POST['exclude_pages']) : [];
update_option('easycoding_exclude_pages', $exclude_pages);
echo '<div class="notice notice-success"><p>Settings saved successfully.</p></div>';
}
$exclude_pages = get_option('easycoding_exclude_pages', []);
$pages = get_pages();
echo '<div class="wrap">';
echo '<h1>Exclude Pages from Search</h1>';
echo '<form method="post">';
echo '<p>Select pages to exclude from search:</p>';
echo '<ul>';
foreach ($pages as $page) {
$checked = in_array($page->ID, $exclude_pages) ? 'checked' : '';
echo '<li><label><input type="checkbox" name="exclude_pages[]" value="' . esc_attr($page->ID) . '" ' . $checked . '> ' . esc_html($page->post_title) . '</label></li>';
}
echo '</ul>';
echo '<p><input type="submit" name="save_exclusions" value="Save Exclusions" class="button button-primary"></p>';
echo '</form>';
echo '</div>';
}
?>
Step 4: Create the CSS File
- Inside the easycoding-exclude-pages-search folder, create a subfolder named assets.
- Within the assets folder, create another subfolder named css.
- Inside the css folder, create a CSS file named easycoding-exclude-pages-search-admin.css.
- Add your desired styles to the CSS file to style the admin panel.
.button-primary {
background-color: #0073aa;
color: #fff;
border-color: #0073aa;
box-shadow: 0 1px 0 #006799;
}
.button-primary:hover {
background-color: #005d99;
border-color: #005d99;
}
Step 5: Activate the Plugin
- Go to your WordPress admin dashboard.
- Navigate to Plugins.
- Find EasyCodingWithAI – Exclude Pages from Search in the list and click Activate.
Step 6: Configure the Plugin
- After activation, you will see a new menu item labeled Exclude Pages in the admin dashboard.
- Click on Exclude Pages to access the settings panel.
- Select the pages you wish to exclude from search results and click Save Changes.
Step 7: Testing
- Perform a search on your website to verify that the selected pages are excluded from the results.
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.