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
- Proper use of Output Buffer
- 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 remove hardcoded characters from playlists?
- Is it possible to disable a function of a parent theme?
- How to override an unpluggable parent theme function?
- function_exists call in function.php
- WordPress Infinite Scroll without using any plugin
- How to preserve PHP modifications while upgrading WordPress?
- Is it possible to get a theme customizer setting from wp.customize using jquery?
- Debugging an error: wp_enqueue_style was called incorrectly
- Conditionally loading JavaScript based on the Advanced Custom Fields in the post
- 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?
- Set the transport of the Customizer ‘header_image’ core setting to ‘postMessage’
- 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
- Hide a menu-item and its submenus and display a ‘Log in’ link if the user is logged out
- How to use WordPress (PHP) functions in AngularJS partials files?
- Copyright info change in Theme Child PHP
- The best way to customize “nav-menu-template.php” to add if the ‘link_before’ is “checkbox”
- Add footer.php to WordPress child theme
- Setting up the child theme so as to enable right-to-left WordPress?
- Design view breaking on Pages
- How to override a non pluggable and non hookable function in a WordPress parent theme?
- How can I loop into two different DIVS without repeating the DIVs
- Removing unnecessary wordpress files
- Is the regular ajax request method safe or I should use admin-ajax.php?
- Overriding a theme redux file in child theme
- How do I list the_tags() into HTML data-attribute
- Click loads template via ajax
- Can I change a variable in a content part while calling it?
- How to make thumbnail image fit into a div where image dimentions are completely different?
- javaScript in section of WP API
- 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?
- Slide in Panel WordPress Post
- Can’t load WP function into external function
- Set WordPress Default Template
- How do I display a user’s previous orders as a select box option on a product?
- WordPress child theme fails to override parent navigation menu in /inc/structure/header.php
- Where to edit the template that is generating the code for dynamic_sidebar left-sidebar in the Understrap theme? [closed]
- Use menu link or onClick to set a variable
- Create another “Display Site Title and Tagline” checkbox, “Header Text Color” setting and control
- How to style options page in dashboard?
- Converting HTML Template to WordPress Theme
- How to remove the cufon script from Dzonia Lite theme [closed]
- Populate dropdown with Child Pages based on Parent Page chosen
- 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
- JS file work only in index page
- Need Help Fixing My Iframes [closed]
- Error in custom php function doesn’t exist
- dynamic dependent select dropdown
- Ajax load more button for comments wordpress
- Populate editor with some content of a page with a page template
- 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
- Skt full width basic slideshow problem
- How to disable controls in theme customizer?
- How do I link a button I created in theme customizer to a function?
- Trying to retrieve post meta
- Is there a way to conditionally check whether a WordPress post title is empty?
- Is the “_s” on this `sprintf(__(‘Page %s’, ‘_s’), max($paged, $page))` just refer to a text domain?
- Redirect to another page using contact form 7? [closed]
- Adding php within a return statement [closed]
- Trouble figuring out how to get my button to submit comment
- 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 can I fix my pagination?
- 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
- Calling a function via a shortcode in javascript
- require used as a function to load theme dependencies?
- I got this error POST https://localhost/meraboilerwp/[object%20Object] 404 (Not Found)
- How do I create a single.php file as the default template for single posts in the twentytwentythree theme?
- 404 error on page reload
- Can’t make an external api call in php side of wordpress child theme
- Trying to insert widget code into theme function
- WP Bootstrap NavWaker: Dropdown Menu Opens All Dropdown Menus at Once
- Custom PHP contact form issue