ulc_load_settings
Filters the settings loaded for a specific post before they are applied.
add_filter( 'ulc_load_settings', $callback, 10, 2 );
Description
Allows developers to conditionally control whether plugin settings are loaded for a specific post. Modify the `$load_settings` boolean to prevent settings loading, useful for optimizing performance on posts without specific shortcodes. The current post object is also provided for context.
Usage
add_filter( 'ulc_load_settings', 'your_function_name', 10, 2 );
Parameters
-
$load_settings(mixed) - This parameter is a boolean flag that determines whether plugin settings should be loaded, defaulting to `false`.
-
$post(mixed) - This parameter determines whether plugin settings should be loaded, defaulting to `false` unless overridden.
Return Value
The filtered value.
Examples
add_filter( 'ulc_load_settings', 'my_custom_ulc_load_settings', 10, 2 );
/**
* Conditionally load plugin settings based on specific conditions.
*
* This filter allows developers to override the default logic for determining
* when the plugin settings should be loaded.
*
* @param mixed $load_settings The current value determining if settings should load.
* @param mixed $post The current post object, if available.
*
* @return bool True if settings should be loaded, false otherwise.
*/
function my_custom_ulc_load_settings( $load_settings, $post ) {
// If settings are already set to load, no need to do anything further.
if ( true === $load_settings ) {
return true;
}
// Example: Force settings to load if we are on a specific custom post type
// and a specific custom field is present.
if ( $post instanceof WP_Post && 'my_special_cpt' === $post->post_type ) {
$custom_field_value = get_post_meta( $post->ID, '_my_plugin_enabled', true );
if ( ! empty( $custom_field_value ) && 'yes' === $custom_field_value ) {
return true; // Load settings for this specific post.
}
}
// Example: If we are in the admin area and on the "UncannyCodes" settings page,
// ensure settings are loaded. This might be redundant with existing checks
// but demonstrates how to target specific admin pages.
if ( is_admin() && isset( $_GET['page'] ) && 'uncannycodes' === $_GET['page'] ) {
return true;
}
// If none of the custom conditions are met, return the original $load_settings value.
// This ensures that the plugin's default logic still applies if this filter
// doesn't explicitly return true.
return $load_settings;
}
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/config.php:195
public static function load_plugin_settings( $override = false ) {
if ( $override ) {
return true;
}
$load_settings = false;
global $post;
if ( $post instanceof WP_Post &&
( has_shortcode( $post->post_content, 'gravityform' ) ||
has_shortcode( $post->post_content, 'wpforms' ) ||
has_shortcode( $post->post_content, 'theme-my-login' ) ||
has_shortcode( $post->post_content, 'uo_user_redeem_code' ) ||
has_shortcode( $post->post_content, 'uo_self_remove_access' ) ||
has_shortcode( $post->post_content, 'uo_code_registration' ) ) ) {
$load_settings = true;
} elseif ( SharedFunctionality::ulc_filter_has_var( 'code_registration', INPUT_POST ) ) {
// TML Registration Field.
$load_settings = true;
} elseif ( SharedFunctionality::ulc_filter_has_var( 'gform_submit', INPUT_POST ) ) {
// GF Registration Field.
$load_settings = true;
} elseif ( SharedFunctionality::ulc_filter_has_var( 'uncanny-learndash-codes-code_registration', INPUT_POST ) ) {
// Native Registration Field.
$load_settings = true;
} elseif ( SharedFunctionality::ulc_filter_has_var( 'coupon_code_only', INPUT_POST ) ) {
// Code Only Field.
$load_settings = true;
} elseif ( function_exists( 'WC' ) && ( is_checkout() || is_cart() || is_woocommerce() ) ) {
// Woocommerce pages only.
$load_settings = true;
} elseif ( SharedFunctionality::ulc_filter_has_var( 'wc-ajax' ) ) {
// Woocommerce pages only.
$load_settings = true;
}
return apply_filters( 'ulc_load_settings', $load_settings, $post );
}