Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vijayhardaha/e629614276e69139a125b5b181a8b6fb to your computer and use it in GitHub Desktop.
Save vijayhardaha/e629614276e69139a125b5b181a8b6fb to your computer and use it in GitHub Desktop.
Discover and Clean Up Plugin-Generated Custom Post Meta Keys and Options

Discover and Clean Up Plugin-Generated Custom Post Meta Keys and Options

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.

How to Use the Function in Your Shell Configuration Files

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:

  1. Open Visual Studio Code or your preferred code editor.

  2. 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.

  3. 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"
    }
  4. Save the Changes: Save the changes to your configuration file.

  5. 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
  6. 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

Scan Results

  • 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.

Example Usage

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.

Conclusion

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.

@vijayhardaha
Copy link
Author

I scanned Split Backorder for Woocommerce plugin and I got these results in scan-results.txt

get_option( 'enable_split_back_order' )
get_option( 'split_back_order' )
get_option( 'split_back_order_instock' )
get_option( 'split_back_order_salable' )
get_option('active_plugins')
get_option('enable_split_back_order')
get_option('split_back_order')
get_option('split_back_order_instock')
get_option('split_back_order_salable')
get_option('woo_sunarc_cart_notices', true)
get_option('woo_sunarc_shipping_cost', 0)
get_option('woocommerce_currency')
get_option('woocommerce_price_num_decimals')
get_option('woocommerce_prices_include_tax')
get_post_meta( $order_id, '_payment_method', true )
get_post_meta($order_id, 'order_email_sent', true)
get_post_meta($order_id, 'order_ids', true)
get_post_meta($post->ID,'in_stock_status',true)
get_post_meta($productids, '_stock', true )
get_post_meta($valData['product_id'], '_price', true)
get_post_meta($values['product_id'], '_price', true)
update_option( 'enable_split_back_order', $enable_split_back_order )
update_option( 'enable_split_back_order', '' )
update_option( 'enable_split_back_order', 'no' )
update_option( 'split_back_order', $split_back_order )
update_option( 'split_back_order', '' )
update_option( 'split_back_order', 'no' )
update_option( 'split_back_order_instock', $split_back_order_instock )
update_option( 'split_back_order_instock', '')
update_option( 'split_back_order_instock', 'no')
update_option( 'split_back_order_salable', $split_back_order_salable )
update_option( 'split_back_order_salable', '' )
update_option( 'split_back_order_salable', 'no' )
update_option($option_name, $new_value)
update_post_meta($order->id, '_cart_discount', wc_format_decimal(WC()
update_post_meta($order->id, '_customer_ip_address', isset($_SERVER['HTTP_X_FORWARDED_FOR'])
update_post_meta($order->id, '_customer_user', $user_id)
update_post_meta($order->id, '_customer_user_agent', isset($_SERVER['HTTP_USER_AGENT'])
update_post_meta($order->id, '_order_currency', get_woocommerce_currency()
update_post_meta($order->id, '_order_discount', wc_format_decimal(WC()
update_post_meta($order->id, '_order_ispliter', 'yes')
update_post_meta($order->id, '_order_key', 'wc_' . apply_filters('woocommerce_generate_order_key', uniqid('order_')
update_post_meta($order->id, '_order_shipping', wc_format_decimal(WC()
update_post_meta($order->id, '_order_shipping_tax', wc_format_decimal(WC()
update_post_meta($order->id, '_order_tax', wc_format_decimal(WC()
update_post_meta($order->id, '_order_total', wc_format_decimal($sum, get_option('woocommerce_price_num_decimals')
update_post_meta($order->id, '_payment_method', $method)
update_post_meta($order->id, '_payment_method_title', $paymentMethod)
update_post_meta($order->id, '_prices_include_tax', get_option('woocommerce_prices_include_tax')
update_post_meta($order->id, 'in_stock_status', 'In Stock')
update_post_meta($order->id, 'in_stock_status', 'Out Stock')
update_post_meta($order_id, 'order_email_sent', 'yes')
update_post_meta($order_id, 'order_ids', serialize($orderIds)
update_post_meta($order_id,'_order_total',0)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment