Plugin add_action and add_menu_page

The main difference is the location where the style (link) or script tag be in the source, or whether the resource is loaded in head or the footer. With a add_menu_page()/add_submenu_page() callback like your foo_settings_page(), the style/script file would be loaded in the body (before the closing body tag), because the head has already been … Read more

enqueue script with get_shortcode_regex

There’s already a function for checking if a post has a shortcode: has_shortcode(): function enqueue_script_if_shortcode_is_detected() { global $post; wp_register_script( ‘leweb-widgets’, get_stylesheet_directory_uri() . ‘/includes/leweb-widgets.php’, ‘1.0’, true ); if ( has_shortcode( $post->post_content, ‘leweb_suggested_events’ ) ) { wp_enqueue_script(‘leweb-widgets’); } } add_action( ‘wp_enqueue_scripts’, ‘enqueue_script_if_shortcode_is_detected’ ); Just keep in mind that checking the post this way: Will not know if … Read more

How to use wordpress function wp_enqueue_script() in php?

I am wondering if that is the right way to use wp_enqueue_script() function in php. Do I need to place wp_enqueue_script() somewhere else ? Not really, here’s what the official docs say in the notes: https://developer.wordpress.org/reference/functions/wp_enqueue_script/ The function should be called using the wp_enqueue_scripts action hook if you want to call it on the front-end … Read more

how to enqueue javascript to manipulate acf input field in admin?

Maybe an easier approach would be to remove the values before the input is presented in the admin? That would circumvent having to do things in JavaScript after the field is already displayed. Take a look at acf/prepare_field hook: function my_acf_prepare_field( $field ) { unset($field[‘choices’][‘custom’]); return $field; } // Apply to all fields. // add_filter(‘acf/prepare_field’, … Read more

How can I force Dynamic CSS via ACF values to update on page load?

Per Tony’s suggestion, adding a random string to the CSS version did the trick. Here’s the code I used to do that. First this code to generate the random number. // get theme version function cd_get_version() { $theme_data = wp_get_theme(); return $theme_data->Version; } $theme_version = cd_get_version(); global $theme_version; // get random number function cd_get_random() { … Read more