Support » Plugin: Block Lab » Rendering programmatically created blocks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Ryan Kienstra

    (@ryankienstra)

    Hi @teemberland,
    Thanks for letting us know about that.

    However, the block is not showing on the page/post once published.

    1. Where is the block not showing? In the block editor, the front-end, or both?

    https://cldup.com/tJ_oPVKYTM.png

    2. Could you please paste the PHP here that programmatically created the block?

    • This reply was modified 11 months, 3 weeks ago by Ryan Kienstra.
    Thread Starter teemberland

    (@teemberland)

    Hi @ryankienstra , I was able to figure it out after a few google search. I had an issue with the naming convention of the blocks. I didn’t know that I had to prepend my php file with ‘block-‘.

    I have a question about rendering values on the front-end from a loop. See my code below:

    <?php
      $json = '{"surveys":[{"name":"Dev Survey","description":"Dev Survey","links":[{"name":"Test Survey","url":"https://www.yahoo.com/?"}]},{"name":"Dev Survey 2","description":"Dev Survey 2","links":[{"name":"Dev Survey 2","url":"https://www.google.com/"}]}]}';
      $surveys = json_decode($json, true);
      foreach ($surveys['surveys'] as $key => $value) {
    		foreach ($value['links'] as $key1 => $value1) {
          $fields = array(
    				'survey-links' => array(
    					'label' => $value1['name'],
              'control' => 'text',
              'default' => $value1['url'],
    				),
    				'survey-button' => array(
    					'label'   => 'Button',
              'control' => 'text',
              'default' => $value1['name'],
    				)
    			);
    		}
    	}
    ?>
    

    With the code above, only the last item in survey-links array renders on the page, but what if I wanted to render all urls? is there a way to loop from block_field(‘survey-links’); function? Ideally I want to display the urls in separate div on the page/post. I hope my my inquiry make sense. Thank you in advance!

    • This reply was modified 11 months, 3 weeks ago by teemberland.
    Plugin Author Ryan Kienstra

    (@ryankienstra)

    Hi @teemberland,
    Maybe you figured this out now, but here’s an idea.

    Maybe change:

    
    		$fields = array(
    			'survey-links' => array(
    

    …to:

    
    		$fields[] = array(
    			'survey-links' => array(
    

    …and add this right above the foreach loop:

    
    $fields = array();
    

    The value of $fields was getting overwritten in each iteration.

    Does that help? With that, the final value of $fields would be:

    https://cldup.com/i1BHXa7xDb.png

    • This reply was modified 11 months, 3 weeks ago by Ryan Kienstra.
    Thread Starter teemberland

    (@teemberland)

    Hi @ryankienstra , I just figured this out. You’re right, the array was getting overwritten each time. Thank you for your input!

    Plugin Author Ryan Kienstra

    (@ryankienstra)

    Sure, good to hear you got it!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Rendering programmatically created blocks’ is closed to new replies.