• Hello.

    I’m trying to access theme.json ‘s contentSize from functions.php.

    I couldn’t find a way.

    Is it possible?

    Thanks.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hello,

    you can open the themes theme.json with php and then decode the json into an array and try to access the contentSize property like this:

    function get_theme_content_size() {
        // Get the URL of the theme.json file
        $theme_json_url = get_theme_file_uri( 'theme.json' );
    
        // Fetch the contents of the theme.json file
        $theme_json = wp_remote_get( $theme_json_url );
    
        // Check if there was an error retrieving the file
        if ( is_wp_error( $theme_json ) ) {
            return false;
        }
    
        // Retrieve the body of the response
        $theme_json_body = wp_remote_retrieve_body( $theme_json );
    
        // Decode the JSON data into an associative array
        $theme_json_data = json_decode( $theme_json_body, true );
    
        // Check if the contentSize value is present in the theme.json data
        if ( ! isset( $theme_json_data['settings']['layout']['contentSize'] ) ) {
            return false;
        }
    
        // Return the contentSize value
        return $theme_json_data['settings']['layout']['contentSize'];
    }
    

    If you need a different value from the theme.json you can just alter this path, note the path has to be changed on 2 different places in the code.

    $theme_json_data[‘settings’][‘layout’][‘contentSize’]

    Paths to any theme.json settings can be found here: https://developer.wordpress.org/block-editor/how-to-guides/themes/theme-json/

    • This reply was modified 1 year, 1 month ago by !Benni.
    Thread Starter asafm7

    (@asafm7)

    Thanks @benniledl .

    It is rather complex (and maybe slow?).

    I don’t know if it is the place to suggest it, but maybe structuring a more straightforward way of accessing theme.json values would be beneficial?

    Thanks again.

    Hi, since WordPress 6.1 there seems to be some method that directly retrieves the theme.json as an array. You can probably use that instead of my code.

    https://developer.wordpress.org/reference/classes/wp_theme_json_data/get_data/

    It would probably work something like this:

    function get_theme_content_size() {
        // Get the theme.json as an array:
    	$theme_json_data = WP_Theme_JSON_Data::get_data();
    
        // Check if the contentSize value is present in the theme.json data
        if ( ! isset( $theme_json_data['settings']['layout']['contentSize'] ) ) {
            return false;
        }
    
        // Return the contentSize value
        return $theme_json_data['settings']['layout']['contentSize'];
    }
    

    I don’t know how this method handles the situation of no theme.json being present.

    Note that I haven’t tested this method please send me any error if there is one so I can help you with fixing that.

    Edit: I used the wrong path for contentSize

    • This reply was modified 1 year, 1 month ago by !Benni.
    • This reply was modified 1 year, 1 month ago by !Benni.
    Thread Starter asafm7

    (@asafm7)

    Thanks @asafm7 .

    I get an error saying it can’t be called statically.

    When I try to use (new WP_Theme_JSON_Data)->get_data() I get:

    Array ( [version] => 2 )

    Probably because I use a child-theme.

    Any thoughts on how to overcome this?

    Thanks again.

    Okay, sorry didn’t realize we had to inizialize it.

    Can you confirm that the theme.json from the child theme only contains version => 2, so I know that it really uses the chily themes theme.json?

    Thread Starter asafm7

    (@asafm7)

    Thanks @benniledl.

    Yes, I’ve just checked and confirmed it. It only uses version => 2.

    You could use the first approach that I posted but with

    get_template_directory()

    This returns the path for the parent theme and not the child theme. Also it doesn’t use http so it is probably faster.

    Like this:

    function get_theme_content_size() {
        // Get the URL of the theme.json file
        $theme_json_url = get_template_directory() . '/theme.json';
    
        // Fetch the contents of the theme.json file
        $theme_json = file_get_contents( $theme_json_url );
    
        // Decode the JSON data into an associative array
        $theme_json_data = json_decode( $theme_json, true );
    
        // Check if the contentSize value is present in the theme.json data
        if ( ! isset( $theme_json_data['settings']['layout']['contentSize'] ) ) {
            return false;
        }
    
        // Return the contentSize value
        return $theme_json_data['settings']['layout']['contentSize'];
    }
    Thread Starter asafm7

    (@asafm7)

    Thanks @benniledl

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Accessing theme.json contentSize from functions.php’ is closed to new replies.