Support » Developing with WordPress » How to get the attributes of the core/image block via a php function

  • Hi,
    I am new to the whole block thing and react js.

    This is a two part question. Firstly I would like to know how to access all attributes belonging to
    the core/image block using php.

    I have tried this function which I found in the Developers guide but it doesn’t return any of the attributes. The function is defined in functions.php of the child theme and is called in a page template.

    function check_attrs() {
    if ( $block['blockName'] === 'core/image' ) {
    		print_r( $block );
    	}
     }	
    
    

    Secondly, please can someone tell me when I add an additional class via the Block Editor like so
    Block –> Advanced –> Additional CSS Class(es) –> members, does the members class attribute get added to the core/image block automatically (assuming the block is an image block)?

    Thank you.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Firstly I would like to know how to access all attributes belonging to
    the core/image block using php

    At which point are you wanting to access the attributes, eg. do you want to check the attributes for each image in a post before it is displayed on the front end?

    so
    Block –> Advanced –> Additional CSS Class(es) –> members, does the members class attribute get added to the core/image block automatically (assuming the block is an image block)?

    Yes, correct. In the case of the image block this will add the classname members to the wrapping figure element on the frontend.

    Thread Starter Zephyrinus Rodrigues

    (@zephyrr)

    Hi glendaviesnz!

    Thank you very much for your reply. I am using WordPress 6.1.1 and PHP 8.1.2-1ubuntu2.11

    I am trying to use the render_block filter to display an image via the image block depending on a condition. At the moment I am trying to get a block to display only if a user is logged in. Hence the additional class.

    In this particular image block for additional CSS Class(ed) I have “is-style-default members-intro”
    members-intro is the class I have added to this particular image block. This is my code.

    // display block for logged in user only
    add_filter( 'render_block', 'logged_in_only_block', 10, 2 );
    function logged_in_only_block( $block_content, $block ) {
      if ( strpos( $block['attrs']['className'], 'members-intro') && 
      ( is_user_logged_in() === true ) ) {
          return $block_content;
        } else {
          return '';
        }
      return $block_content;
    }
    
    // end display block for logged in user only 

    It is certainly picking up the additional class and rendering the image block when the user is logged in. However it it not displaying my other image blocks like a reusable image block I have. I need some time to test further as I have an Apache web server permission issue uploading new image content. Please give me a couple of days to resolve this. Can you please tell me if the last statement ” return $block_content; ” is necessary?

    A question regarding the render_block filter. Can you please tell me what the “10, 2” parameters signify?

    Thank you.

    I think you may need to change your function to something like:

    function logged_in_only_block( $block_content, $block ) {
    	if ( strpos( $block['attrs']['className'], 'members-intro') ) {
      	    if ( is_user_logged_in() === true )  {
          	        return $block_content;
        	    } else {
          	        return '';
    	    }
    	}
      	return $block_content;
    }

    As it seems like you only want to check for the logged-in status if it is the members-intro block. The way you have it, unless it is a members-intro block and the user is logged in an empty string will be returned which might be why your other blocks are not appearing.

    Can you please tell me if the last statement ” return $block_content; ” is necessary?

    It wasn’t needed in your original code as that return would never have been hit. It is needed in my revised version as for anything other than a members-intro block this will return the block content. Hopefully that makes sense, let me know if not.

     Can you please tell me what the “10, 2” parameters signify?

    You can find details about this here https://developer.wordpress.org/reference/functions/add_filter/

    • This reply was modified 10 months, 3 weeks ago by Glen Davies.
    Thread Starter Zephyrinus Rodrigues

    (@zephyrr)

    Hi Glen,

    Thanks very much for your reply and code. And the add filter reference link.

    Initially I did not have much joy with your code as all blocks get rendered irrespective if the user is logged in or not. I think the problem lies with the additional class name. The filter is not picking it up.

    I used this code to test

    add_filter( 'render_block', 'logged_in_only_block', 10, 2 );  function logged_in_only_block( $block_content, $block ) {	if ( strpos( $block['attrs']['className'], 'members-intro') ) {  	          	        
    return $block_content;    	    
    }    	    
    else {      	        
    return '';	    	
    }  	
    return $block_content;  
    }    

    With this, none of the image blocks got rendered irrespective of the stipulated ‘members-intro’. I tried it on two different machines just to be sure it wasn’t a bug/or theme files corruption.

    By trial and error, I managed to get it to work by adding ‘is-style-default’ to Additional CSS Class(es) in the Block Editor. So I have ‘is-style-default members-intro’ for the image block I want to display to logged in users.

    Is there a way to output on the screen all Class Names from $block[‘attrs’][‘className’] via php?

    Thank you very much once again for your help with this.

    Is there a way to output on the screen all Class Names from $block[‘attrs’][‘className’] via php?

    Are you just wanting to output it to the screen for debugging? If so you should be able to use echo var_dump( $block[‘attrs’][‘className’] );

    Thread Starter Zephyrinus Rodrigues

    (@zephyrr)

    Hi Glen!

    Thanks for your reply and suggestion to try.

    Firstly, I managed to get the ‘members-intro’ class conditional to work as it should with just the ‘members-intro’ in the Additional CSS Class(es) in the Block Editor. I am not sure what it was. I decided to test in a different block theme ans used 2023 WordPress block theme. I created a functions.php here. I also had an orphaned block in the 2021 WordPress theme where the Block Editor said something like the block is unavailable or has been moved. After switching themes this orphaned block disappeared. But I don’t think it was the orphaned block as I had the same issue on another system running PHP 7.4 . I haven’t had the chance as yet to test on this setup.

    I am afraid echo var_dump( $block[‘attrs’][‘className’] ); does not work. If enabled in functions.php I get the “There has been a critical error on this website.” and if put in the page template php file nothing outputs and no other code runs after it.

    Thread Starter Zephyrinus Rodrigues

    (@zephyrr)

    Just to confirm @glendaviesnz your code for displaying a block based on only if a user is logged in using the Class Name works 100%. I tested it on my PHP 7.4 dev box with both the twenty twenty-one and the twenty twenty three themes on WordPress 6.1.1 and it worked as expected. Thank you very much!

    // display block for logged in user only
    add_filter( 'render_block', 'logged_in_only_block', 10, 2 );
    function logged_in_only_block( $block_content, $block ) {
    	if ( strpos( $block['attrs']['className'], 'members-intro') ) {
      	    if ( is_user_logged_in() === true )  {
          	        return $block_content;
        	    } else {
          	        return '';
    	    }
    	}
      	return $block_content;
    }

    I am now hoping someone will be able to tell me how I can access the Class Names from the block attributes via php for testing/development purposes. Thank you.

    You can’t echo the results of var_dump. Just use var_dump. Also, you’ll find the results of an error more quickly by turning on error reporting with WP_DEBUG.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to get the attributes of the core/image block via a php function’ is closed to new replies.