Title: Rendering programmatically created blocks
Last modified: October 28, 2020

---

# Rendering programmatically created blocks

 *  Resolved [teemberland](https://wordpress.org/support/users/teemberland/)
 * (@teemberland)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/rendering-programmatically-created-blocks/)
 * Hi, I programmatically created a custom block. I can see it on the blocks menu
   and I’m able to add it to post editor. However, the block is not showing on the
   page/post once published. How do I render and style these blocks? Thank you!
 * ref: [https://getblocklab.com/building-custom-blocks-programmatically/](https://getblocklab.com/building-custom-blocks-programmatically/)

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Plugin Author [Ryan Kienstra](https://wordpress.org/support/users/ryankienstra/)
 * (@ryankienstra)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/rendering-programmatically-created-blocks/#post-13598308)
 * Hi [@teemberland](https://wordpress.org/support/users/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](https://cldup.com/tJ_oPVKYTM.png)
 * 2. Could you please paste the PHP here that programmatically created the block?
    -  This reply was modified 5 years, 7 months ago by [Ryan Kienstra](https://wordpress.org/support/users/ryankienstra/).
 *  Thread Starter [teemberland](https://wordpress.org/support/users/teemberland/)
 * (@teemberland)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/rendering-programmatically-created-blocks/#post-13599357)
 * Hi [@ryankienstra](https://wordpress.org/support/users/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 5 years, 7 months ago by [teemberland](https://wordpress.org/support/users/teemberland/).
 *  Plugin Author [Ryan Kienstra](https://wordpress.org/support/users/ryankienstra/)
 * (@ryankienstra)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/rendering-programmatically-created-blocks/#post-13603762)
 * Hi [@teemberland](https://wordpress.org/support/users/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](https://cldup.com/i1BHXa7xDb.png)
    -  This reply was modified 5 years, 7 months ago by [Ryan Kienstra](https://wordpress.org/support/users/ryankienstra/).
 *  Thread Starter [teemberland](https://wordpress.org/support/users/teemberland/)
 * (@teemberland)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/rendering-programmatically-created-blocks/#post-13603880)
 * Hi [@ryankienstra](https://wordpress.org/support/users/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](https://wordpress.org/support/users/ryankienstra/)
 * (@ryankienstra)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/rendering-programmatically-created-blocks/#post-13603915)
 * 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.

 * ![](https://s.w.org/plugins/geopattern-icon/block-lab_5c34e8.svg)
 * [Block Lab](https://wordpress.org/plugins/block-lab/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/block-lab/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/block-lab/)
 * [Active Topics](https://wordpress.org/support/plugin/block-lab/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/block-lab/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/block-lab/reviews/)

 * 5 replies
 * 2 participants
 * Last reply from: [Ryan Kienstra](https://wordpress.org/support/users/ryankienstra/)
 * Last activity: [5 years, 7 months ago](https://wordpress.org/support/topic/rendering-programmatically-created-blocks/#post-13603915)
 * Status: resolved