I’m new to bricks and wordpress so I apologize if I am doing something wrong. I am trying to load different sections of my website dynamically using AJAX. It’s a customer portal using the wordpress my account pages. I’ve also created some custom endpoints. I have 4 buttons and when one is pressed, the content is displayed underneath properly but the styles do not apply when loaded via ajax. Also it seems like other shortcodes from other plugins seem to be loading in fine with styles.
Here is my AJAX JavaScript code:
jQuery(document).ready(function($) {
$('.dynamic-content-button').on('click', function() {
var contentId = $(this).data('content-id');
$.ajax({
url: ajax_object.ajax_url,
type: 'GET',
data: {
action: 'load_dynamic_content',
content_id: contentId
},
success: function(response) {
$('#dynamic-content-area').html(response);
if (window.bricksFrontend) {
window.bricksFrontend.init();
}
},
error: function() {
alert('Sorry, there was an error loading the content.');
}
});
});
});
This is what I have in my functions.php
function enqueue_dynamic_content_script() {
wp_enqueue_script( 'dynamic-content', get_template_directory_uri() . '/js/dynamic-content.js', array('jquery'), null, true );
wp_localize_script( 'dynamic-content', 'ajax_object', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
));
}
add_action( 'wp_enqueue_scripts', 'enqueue_dynamic_content_script' );
function load_dynamic_content() {
if ( isset( $_GET['content_id'] ) ) {
$content_id = sanitize_text_field( $_GET['content_id'] );
switch ( $content_id ) {
case 'edit-address':
echo do_shortcode( '[bricks_template id="285"]' ); // Replace with your template ID for Edit Address page
break;
case 'marketplace':
echo do_shortcode( '[bricks_template id="414"]' ); // Replace with your template ID for Marketplace page
break;
case 'orders':
echo do_shortcode( '[bricks_template id="281"]' );
break;
case 'aktualnosci':
echo do_shortcode( '[bricks_template id="430"]' );
break;
case 'edit-profile':
echo do_shortcode( '[bricks_template id="1270"]' );
break;
default:
echo 'Content not found.';
}
}
wp_die();
}
add_action( 'wp_ajax_load_dynamic_content', 'load_dynamic_content' );
add_action( 'wp_ajax_nopriv_load_dynamic_content', 'load_dynamic_content' );
It shows up properly when visiting the page directly:
But this is what happens when it’s loaded dynamically.