Filter
uncanny-automator
uap_option_get_wpcode_snippets
Filters the option for retrieving WPCode snippets, allowing modification before the snippets are returned.
add_filter( 'uap_option_get_wpcode_snippets', $callback, 10, 1 );
Description
This filter hook allows developers to modify the array of WPCode snippets available for selection. It fires after snippets are retrieved and before they are presented to the user. Developers can add, remove, or alter snippet options, providing custom snippet lists or filtering existing ones based on specific criteria.
Usage
add_filter( 'uap_option_get_wpcode_snippets', 'your_function_name', 10, 1 );
Parameters
-
$option(mixed) - This parameter contains an array of arguments used to configure how the WordPress code snippets are retrieved and displayed.
Return Value
The filtered value.
Examples
/**
* Filters the WPCode snippets option to add a specific snippet based on user role.
*
* This function checks if the current user has the 'administrator' role.
* If they do, it adds a predefined snippet (e.g., for debugging or custom admin functionality)
* to the list of available WPCode snippets that will be presented in a form or settings page.
*
* @param array $option The original array of options, potentially containing WPCode snippets.
* @return array The modified array of options, including the added snippet if applicable.
*/
add_filter( 'uap_option_get_wpcode_snippets', function( $option ) {
// Check if the current user is an administrator.
if ( current_user_can( 'manage_options' ) ) {
// Define a custom snippet for administrators.
$admin_specific_snippet = array(
'id' => 'admin_custom_script',
'title' => 'Admin Custom Script (For Debugging)',
'description' => 'A custom script loaded only for administrators.',
'code' => 'console.log("Administrator custom script loaded!");',
'type' => 'php', // Or 'html', 'css', 'js'
'location' => 'admin_head', // Or 'frontend_head', 'admin_footer', etc.
);
// Ensure the 'options' key exists and is an array before merging.
if ( ! isset( $option['options'] ) || ! is_array( $option['options'] ) ) {
$option['options'] = array();
}
// Add the administrator-specific snippet to the existing options.
$option['options'][] = $admin_specific_snippet;
}
// Return the modified option array.
return $option;
}, 10, 1 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/integrations/wpcode/helpers/wpcode-helpers.php:62
public function get_wpcode_snippets( $args = array() ) {
$defaults = array(
'option_code' => 'WP_CODE_SNIPPETS',
'label' => esc_attr__( 'Snippet', 'uncanny-automator' ),
'is_any' => false,
'is_all' => false,
'supports_custom_value' => false,
'relevant_tokens' => array(),
'snippet_status' => 'publish',
);
$args = wp_parse_args( $args, $defaults );
$query_args = array(
// phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page
'posts_per_page' => apply_filters( 'automator_select_all_posts_limit', 9999, 'post' ),
'orderby' => 'title',
'order' => 'DESC',
'post_type' => 'wpcode',
'post_status' => $args['snippet_status'],
);
$snippets = Automator()->helpers->recipe->options->wp_query( $query_args );
if ( true === $args['is_any'] ) {
$snippets = array( '-1' => esc_html__( 'Any snippet', 'uncanny-automator' ) ) + $snippets;
}
if ( true === $args['is_all'] ) {
$snippets = array( '-1' => esc_html__( 'All snippets', 'uncanny-automator' ) ) + $snippets;
}
$option = array(
'option_code' => $args['option_code'],
'label' => $args['label'],
'input_type' => 'select',
'required' => true,
'options_show_id' => false,
'relevant_tokens' => $args['relevant_tokens'],
'options' => $snippets,
'supports_custom_value' => $args['supports_custom_value'],
);
return apply_filters( 'uap_option_get_wpcode_snippets', $option );
}