Skip to content

Instantly share code, notes, and snippets.

@jamesckemp
Created April 24, 2023 06:44
Show Gist options
  • Save jamesckemp/fea2e9dceaf97cde55566d26a1fdfbb1 to your computer and use it in GitHub Desktop.
Save jamesckemp/fea2e9dceaf97cde55566d26a1fdfbb1 to your computer and use it in GitHub Desktop.
<?php
/**
* Filter the list of active plugins for custom endpoint requests.
*
* @param array $active_plugins The list of active plugins.
*
* @return array The filtered list of active plugins.
*/
function filter_active_plugins( $active_plugins ) {
if ( ! is_custom_endpoint_request() || ! is_array( $active_plugins ) ) {
return $active_plugins;
}
$allowed_plugin_files = array( 'setary.php', 'woocommerce.php' );
$filtered_plugins = array();
foreach ( $active_plugins as $plugin ) {
foreach ( $allowed_plugin_files as $allowed_plugin_file ) {
if ( strpos( $plugin, $allowed_plugin_file ) !== false ) {
$filtered_plugins[] = $plugin;
break;
}
}
}
return $filtered_plugins;
}
add_filter( 'option_active_plugins', 'filter_active_plugins' );
@Dan0sz
Copy link

Dan0sz commented Apr 24, 2023

Thanks for sharing! One last question I have, how do you decide when it's a custom endpoint request? Do you simply check the endpoint that's being called?

@jamesckemp
Copy link
Author

@Dan0sz I just check if the path contains wc/setary, but you could also pass an additional get parameter ?setary=1

@Dan0sz
Copy link

Dan0sz commented Apr 24, 2023

Man, this is interesting. And so simple! Thanks a lot, man!

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