Support

Account

Home Forums Gutenberg redering block via ajax not working multisite

Solved

redering block via ajax not working multisite

  • Hello,
    I’m having trouble rendering an ACF block via ajax on a multisite. I have a custom post type that has one acf block on it. then I have an acf options page with a post_object selector on it, where you can select these posts.

    I’m trying to have a preview of the post on the options page so when the page loads it takes the value of the post_object selector and does an ajax call to try and get the content.

    I switch to the correct blog in my php code but I’m still having trouble displaying the_content. see comments in code below:

    <?php
    
    require '../../../../../wp-load.php';
    
    switch_to_blog( 2 );
    
    $ad = ( is_numeric( $_REQUEST['p'] ) ) ? $_REQUEST['p'] : '';
    
    if ( ! empty( $ad ) ) {
    	$ad = new WP_Query( array( 'p' => $ad, 'post_type' => 'ad' ) );
    
    	// $return = apply_filters( 'the_content', $ad->posts[0]->post_content ); // returns empty
    	// $return = $ad->posts[0]->post_content; // returns the <!-- wp:acf/mypostype { ... } object
    	// $return = parse_blocks( $ad->posts[0]->post_content )[0]; // returns block array
    	// $return = render_block( parse_blocks( $ad->posts[0]->post_content )[0] ); // returns nothing
    	// $return = acf_rendered_block( parse_blocks( $ad->posts[0]->post_content )[0]['attrs'] ); // returns empty
    	// 
    	// so at this point I starting debugging in the acf source and I made it up to the point
    	// where acf checks to see if the "name" is in the acf_get_store. when I call that it returns a 
    	// list of acf blocks in the 1st blog not in the blog that I'm currently in. (get_current_blog_id() confirms I'm in the correct blog).
    	// echo "<pre>"; print_r(acf_get_store( 'block-types' )); exit; // returns a list of acf blocks in blog #1
    }
    
    echo $return;

    Is there a better way to do what I’m trying to do? I feel like it should be pretty easy to call get_the_content() (I’ve also tried “the loop” with no luck) from inside this script and return the HTML. pretty sure the issue has to do with the fact that acf doesn’t seem to check the current blog id. Thanks for your time.

  • I ended up just adding the block to blog 1 and then apply_filters( 'the_content', $ad->posts[0]->post_content ); worked perfectly. So I guess this is more of a bugfix. will report. thanks.

    edit: actually, this did not work. I just noticed that during debugging I added switch_to_blog( 2 ); to acf_register_store() which was why it was working. So I guess I’m still not sure how to proceed, any help would be appreciated. thanks!

  • anyone else having issues with this, I managed to solve this by rewriting the acf_rendered_block() – so not an ideal solution but it works for now.

    <?php
    
    require '../../../../../wp-load.php';
    
    switch_to_blog( 2 );
    
    $ad = ( is_numeric( $_REQUEST['p'] ) ) ? $_REQUEST['p'] : '';
    
    if ( ! empty( $ad ) ) {
    	$ad = new WP_Query(
    		array(
    			'p'         => $ad,
    			'post_type' => 'ad',
    		)
    	);
    
    	$blocks = parse_blocks( $ad->posts[0]->post_content );
    	foreach ( $blocks as $block ) {
    		if ( 'acf/lunchrush-ad' == $block['blockName'] ) {
    			ob_start();
    
    			acf_setup_meta( $block['attrs']['data'], $block['attrs']['id'], true );
    
    			if ( file_exists( __DIR__ . '/block-ad.php' ) ) {
    				$path = __DIR__ . '/block-ad.php';
    			}
    
    			// Include template.
    			if ( file_exists( $path ) ) {
    				include( $path );
    			}
    
    			// Reset postdata.
    			acf_reset_meta( $block['attrs']['id'] );
    
    			$return = ob_get_clean();
    
    		}
    	}
    
    	if ( ! empty( $return ) ) {
    		$return = '<div class="ad-preview">' . $return . '</div>';
    	}
    }
    
    echo $return;
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘redering block via ajax not working multisite’ is closed to new replies.