This is the proper way to load a custom javascript in your child theme, wp_localize_script is only necessary if you need to exchange data between JS and PHP (Client and server).
function assets() {
wp_register_script('ajax_filter', get_stylesheet_directory_uri() . '/js/ajax-filter-posts.js', ['jquery'], '1.0', false);
wp_enqueue_script('ajax_filter');
wp_localize_script( 'ajax_filter', 'mdu', array(
'nonce' => wp_create_nonce( 'mdu' ),
'ajax_url' => admin_url( 'admin-ajax.php' )
));
}
add_action('wp_enqueue_scripts', 'assets');
So my issue as noticed by @KAGG was that i was loading this JS from another file…
Thanks for his help !
Matth
Related Posts:
- Adding tawk.to code just before body tag on functions.php file
- Trigger popup in a php if/else statement
- Removing “Powered by” footer using child theme PHP [closed]
- Need advice on theme customizer and child themes
- Function to return true if current page has child pages
- List of all theme customizer control types?
- Need help setting default setting value for radio button in theme customizer
- How to call a PHP function from Javascript in WordPress
- How to preserve PHP modifications while upgrading WordPress?
- Is it possible to get a theme customizer setting from wp.customize using jquery?
- How do I remove a require_once admin panel from the parent theme from the child theme functions.php?
- How to get the registered sidebar’s name by its id?
- is there a simple way to list every templates / php files used to generate a specific page?
- Dynamically change feature image in customiser
- One button to change all settings in theme customizer?
- Calling a method from functions.php on a click of a button
- How to use WordPress (PHP) functions in AngularJS partials files?
- The best way to customize “nav-menu-template.php” to add if the ‘link_before’ is “checkbox”
- Setting up the child theme so as to enable right-to-left WordPress?
- Design view breaking on Pages
- How can I loop into two different DIVS without repeating the DIVs
- Removing unnecessary wordpress files
- Remove css styles from specific page
- Overriding a theme redux file in child theme
- How do I list the_tags() into HTML data-attribute
- How to make thumbnail image fit into a div where image dimentions are completely different?
- Custom Logo URL | Help me print the URL of the custom logo I inserted into my theme
- What is the fastest way to load PHP functions that are only used in one theme template?
- How to speed up admin-ajax.php in wordpress
- My Own layout in WooCommerce pages [closed]
- Differences when using the the_time and the_date functions
- How do I fetch all comments per post via WP REST API?
- Can’t load WP function into external function
- How do I display a user’s previous orders as a select box option on a product?
- Where to edit the template that is generating the code for dynamic_sidebar left-sidebar in the Understrap theme? [closed]
- How to style options page in dashboard?
- remove wp floating submenu in wp dashboard
- How do I add custom bulk actions to multiple custom post types?
- How to complete two other input fields, completed the first
- what is the best practice to add new field to an api route
- I have a problem in the order of enqueues while enqueuing stylesheets and scripts for a specific page in my function.php
- Need Help Fixing My Iframes [closed]
- Catchable fatal error: Object of class stdClass could not be converted to string after WP 4.7
- Menu jumping when calling it via PHP
- Compare the old get_theme_mod($name) to the new get_theme_mod($name) return value
- How to disable controls in theme customizer?
- Noscript not working as it should in wordpress site
- How to call multiple functions from multiple files into a WordPress page template [closed]
- Theme not calling Jquery properly
- What is the best way to define constant options for a theme?
- How to specify the path for require_once in a child theme?
- Warning: printf(): Too few arguments in helpers.php file
- How do I check or test a WordPress Auto Year Change Script?
- How to have different site identity logos on each page on Astra Theme [closed]
- wp_nav_menu not working correctly in my underscores theme
- Overide Variable in Child Theme
- child parent styles enqueue order
- get_page_templates only return templates with “home” in the filename
- How to style injected code in header section?
- Is there a hack for using is_page() within the function.php file?
- Extract and display user infromation on an automatically created page
- Show different website layout if no sidebar added
- How can I assign separate stylesheets to different pages?
- How to make sure relative URL works when site is not on root domain?
- security concerns if using html data-* attribute for l10n?
- Dynamically switch file in get_template_directory_uri() | Function [closed]
- WordPress User Meta value variable into Google Gauge Chart
- How to extend SelectControl with data from my theme
- How to change basename url for wp-admin?
- Gutenberg block don’t save rich text content
- remove_action from parent theme using child theme functions.php
- require get_template_directory() . ‘path/to-my/file.php’ BREAKS customize > themes functionality
- How to change this ajax function to submit to the default wordpress content area instead of the custom field ‘seller notes’?
- Child theme overirde template-tags in a theme built on underscores in inc/template-tags
- Trying to add some custom text into WordPress Post title via function.php
- Update $wpdb query with AJAX
- Using Javascript On Page – Header and Footer Now Missing
- Adding extra data to shortcode attributes and pass it to JS with wp_localize_script
- How do I make comment-reply-button with that takes to comment reply form on click
- Custom Post type Ajax search results
- WordPress load javascript file if something… (after the page is loaded)
- Theme editing “post thumbnail” help
- Find the method which AJAX GET calls
- Display text of price (minus 20%) on every product page in a sentence.
- Where can I find the declaration of `$_wp_theme_features`?
- Calling PHP function doesn’t work in index.php
- Where to find the html for WordPress site? [closed]
- My customizer’s setting doesn’t set to the default and needed to click the control’s “Default” button before it’ll be set
- Save the outputted image into the Media Library, with a different filename and extension
- WordPress causing all code to be displayed on line 1. Receiving multiple errors after cleaning cookies and cache
- how can I make content from a plugin hidden when user is logged in? [duplicate]
- I need a button to appear when not loged in and another when logged in, I need help fixing code PLEASE!
- Load slideshow.css file only if Slideshow is checked / on
- How to hide .js files on wordpress website using php or wp plugins or any? [closed]
- Show Login Errors In WordPress/Elementor (Code “works”, but breaks site)
- How can I use AJAX in child theme template?
- hiddenQuestions.find is not a function
- How do I create a single.php file as the default template for single posts in the twentytwentythree theme?
- Can’t make an external api call in php side of wordpress child theme
- Hide or Show Read More Button by Content Area