# Passing Block Attribute Values to PHP Variables?

**URL:** <https://forum.pinegrow.com/t/passing-block-attribute-values-to-php-variables/7672>\
**Category:** Blocks\
**Tags:** gutenberg\
**Created:** [January 21, 2023, 12:00pm UTC](https://forum.pinegrow.com/t/passing-block-attribute-values-to-php-variables/7672 "2023-01-21T12:00:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonroc](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/jonroc/32/2805_2.png) [@jonroc](https://forum.pinegrow.com/u/jonroc)\
**Post date:** [January 21, 2023, 12:00pm UTC](https://forum.pinegrow.com/t/passing-block-attribute-values-to-php-variables/7672/1 "2023-01-21T12:00:32Z")

</div>

I have some PHP being pulled into my blocks via get\_template\_part() and I’d like to set up a block attribute that can update a variable in the PHP. Is this possible?

I’ve already tried including my PHP directly in the block and it seems like I could add attributes to the HTML elements in the PHP but for some reason the PHP loop does not work when I include the PHP directly in PG. It only executes properly if I call in the template.

---

<div class="post-metadata">

**Author:** ![jonroc](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/jonroc/32/2805_2.png) [@jonroc](https://forum.pinegrow.com/u/jonroc)\
**Post date:** [January 21, 2023, 3:44pm UTC](https://forum.pinegrow.com/t/passing-block-attribute-values-to-php-variables/7672/2 "2023-01-21T15:44:03Z")

</div>

I figured out a way to put the PHP into PG so I can assign actions to the non-php elements. It turns out that when you use `get_template_part()` you don’t need to include the `global $post;` but if you add the loop directly into PG then you have to add that. I also figured out how to use values for PHP if anyone ever needs to do that just assign an action to your block and use the `none` value for the “Use As” selector. Then in your code you can assign that value to a variable like I’ve done below:

```auto
<php>
$region_term = PG_Blocks::getAttribute( $args, 'region_slug' ); // this pulls the attribute to a variable so you can use it in your code
        $locations_region_filter_args = array(
            'tax_query' => array( 
                array(
                    'taxonomy' => 'regions', 
                    'field' => 'slug',
                    'terms' => $region_term //this will be populated by what the user selects in the WordPress Block from the admin page
                )
            ),
            'post_parent__not_in' => array(0),
            'post_type' => 'locations',
            'post_status' => 'publish',
            'order' => 'DESC',
            'orderby' => 'menu_order'
        )
</php>

```

---

<div class="post-metadata">

**Author:** ![matjaz](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/matjaz/32/5_2.png) [@matjaz](https://forum.pinegrow.com/u/matjaz)\
**Post date:** [January 21, 2023, 3:45pm UTC](https://forum.pinegrow.com/t/passing-block-attribute-values-to-php-variables/7672/3 "2023-01-21T15:45:50Z")

</div>

@jonroc just wanted to ask you for the code 🙂 Thanks for posting the solution.

---

<div class="post-metadata">

**Author:** ![adamslowe](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/adamslowe/32/4590_2.png) [@adamslowe](https://forum.pinegrow.com/u/adamslowe)\
**Post date:** [January 21, 2023, 5:25pm UTC](https://forum.pinegrow.com/t/passing-block-attribute-values-to-php-variables/7672/4 "2023-01-21T17:25:43Z")

</div>

@jonroc You are a scholar and a gentleman! I was going to work on this later this week for a project I’m wrestling with. Thanks for saving me a few hours and brain cells.

---

<div class="post-metadata">

**Author:** ![matjaz](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/matjaz/32/5_2.png) [@matjaz](https://forum.pinegrow.com/u/matjaz)\
**Post date:** [January 22, 2023, 4:19pm UTC](https://forum.pinegrow.com/t/passing-block-attribute-values-to-php-variables/7672/5 "2023-01-22T16:19:53Z")

</div>

For reference, Block attributes can also be used as parameters for the Show posts action with “@block:attribute\_id”:

Example:

post\_type could be a dropdown attribute, categories a text field that understands cat,cat,cat and cat+cat+cat.

 ![useblockattrs](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/e/e83caded16257b070c4a2837d51b9b15085a7d76.jpeg)

This works similar to WooCommerce Show products:

> **[Tags and categories for the product list block | Pinegrow Web Editor](https://pinegrow.com/docs/woocommerce/creating-custom-woocommerce-themes-course/tags-and-categories-for-the-product-list-block/)**
>
> Using attributes for selecting products based on product tags and categories.

---

<div class="post-metadata">

**Author:** ![Wolfgang.Hartl](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/wolfgang.hartl/32/4621_2.png) [@Wolfgang.Hartl](https://forum.pinegrow.com/u/Wolfgang.Hartl)\
**Post date:** [May 16, 2023, 3:12pm UTC](https://forum.pinegrow.com/t/passing-block-attribute-values-to-php-variables/7672/6 "2023-05-16T15:12:44Z")

</div>

Thanks for that! This is really helpful, and saved me a loooooooot of time! 😉
