When managing WordPress plugins, it's crucial to identify and remove any lingering custom post meta keys and options created by plugins you no longer use. This custom shell function, scan-plugin-meta-options-keys()
, simplifies the process of searching for and identifying these meta keys and options within your WordPress database.
You can add the scan-plugin-meta-keys()
function to your shell configuration files (e.g., .bashrc
, .bash_profile
, or .zshrc
) to make it available each time you start a new terminal session. Here's how to add and utilize the function in your shell configuration:
-
Open Visual Studio Code or your preferred code editor.
-
For Bash (
.bashrc
or.bash_profile
) and Zsh (.zshrc)
:a. Bash Configuration: If you're using Bash, open your Bash configuration file. Depending on your system, it might be either
~/.bashrc
or~/.bash_profile
.b. Zsh Configuration: If you're using Zsh, open your Zsh configuration file, typically
~/.zshrc
. -
Copy and Paste: Copy and paste the following shell function at the end of your configuration file:
# Function: scan-plugin-meta-keys # Description: This function searches for specified meta key-related terms in PHP files # within the current directory and its subdirectories and saves the raw # results to a file named "scan-results-raw.txt." It then extracts and # saves matching content to a separate file named "scan-results.txt." scan-plugin-meta-keys() { # Define an array of search terms related to meta keys in plugins local search_terms=("update_post_meta" "get_post_meta" "update_meta" "get_meta" "update_option" "get_option") # Define the output file where the raw scan results will be saved local output_file="scan-results-raw.txt" # Remove the existing raw output file if it exists rm -f "$output_file" # Loop through each search term in the array for term in "${search_terms[@]}"; do # Execute a recursive case-insensitive grep search for the current term # within '*.php' files in the current directory and its subdirectories # Append the results to the raw output file grep -iRn --include='*.php' "$term" * >> "$output_file" done # Display a completion message with the path to the raw output file echo "Scan completed. Raw results are in $output_file" # Open the raw result file in Visual Studio Code code "$output_file" # Define the output file where matching content will be saved local matched_output_file="scan-results.txt" # Remove the existing matched output file if it exists rm -f "$matched_output_file" # Loop through the patterns and extract matching content for pattern in "${search_terms[@]}"; do grep -F "$pattern" "$output_file" | sed -n 's/.*\('"$pattern"'([^)]*)\).*/\1/p' >> "$matched_output_file" done # Display a completion message with the path to the matched output file echo "Matching content has been extracted to $matched_output_file" # Open the matched result file in Visual Studio Code code "$matched_output_file" }
-
Save the Changes: Save the changes to your configuration file.
-
Reload the Configuration:
-
For Bash: In your terminal, either restart your terminal or run:
source ~/.bashrc
-
For Zsh: In your terminal, either restart your terminal or run:
source ~/.zshrc
-
-
Usage: You can now use the
scan-plugin-meta-keys
function in your Bash or Zsh shell. Simply run it in any directory, and it will scan for and extract meta key-related code from PHP files within that directory and its subdirectories.scan-plugin-meta-keys
-
Raw Results: The raw scan results are saved in a file named "scan-results-raw.txt." This file contains all occurrences of the specified search terms in your PHP files.
-
Matched Content: The matching content is extracted from the raw results and saved in a separate file named "scan-results.txt." This file provides a cleaner view of the relevant code snippets.
You can use this function to quickly identify and extract code related to common meta key operations such as update_post_meta
, get_post_meta
, update_meta
, get_meta
, update_option
, and get_option
.
The scan-plugin-meta-keys
function provides a convenient way to locate and identify custom post meta keys and options created by WordPress plugins. By integrating this function into your workflow, you can ensure efficient database cleanup when you stop using a plugin, helping you maintain a well-organized and optimized WordPress site.
I scanned Split Backorder for Woocommerce plugin and I got these results in
scan-results.txt
From these results, I can quickly manually review the custom option and post meta from this plugin and then clean my database.
Note: I have removed duplicates using an online Duplicate Remove Tool