1

I have a plugin in which I'm trying to allow users to create their own variants of the styles, and upload them as custom "templates". Essentially these templates are php files with some HTML in them.

I'm trying to add a template tag to the top of each files, so I can extract certain data such as the template name and maybe an author.

I've been doing a bit of research and came across a function I haven't used before get_file_data();. There doesn't seem to be too much information regarding how to use the function to extract data from a template file.

Each time I try and run this function to return some data it either returns empty or with an array with one key and no value assigned. I must be doing something wrong. Unfortunately their is not much in the codex http://codex.wordpress.org/Function_Reference/get_file_data https://developer.wordpress.org/reference/functions/get_file_data/

$template_data = get_file_data( $content_path . 'template_1.php' , array( 'Template Name' => 'Template Name' ) );
print_r($template_data );

I can't seem to figure out what the default headers argument (2nd argument) is doing.

The contents of the template_1.php file is :

<?php
  /*
  Template Name: Cool Template File
  Template Author: Evan
  */
?>

I'm not sure what it is I'm doing wrong, or what I should be doing to extract the data at the top of this file properly.

Thanks

  • 1
    I've tested your implementation of get_file_data() and it works correctly (I've just changed the template path with a correct value in my Wordpress instalation). Maybe you are not passing the correct path. Can you provide how do you set the value for $content_paht variable? Also, you are printing the value of $title, try to print the value of $template_data instead, which is the variable where you are storing the value returned by get_file_data(). About the default headers argument it does what it says: set a default value to be used if the specified header is not found in the file. – cybmeta Sep 10 '14 at 19:49
  • that was my mistake on the print_r($title); my code is actually print_r($template_data);. It does return something but not what I am after. When you used get_file_data, were you able to successfuly extract the Template Name from the file? That is what I was trying to pass into the $title variable. I am currently hardcoding in the path to the file while I test this out. I am using it inside of a loop after using scandir to retreive all files within a directory. This is all that is returned from the above Array ( [Template Name] => ) – EHerman Sep 10 '14 at 19:53
  • Thank you for pointing me in the right direction. I was originally using file_get_contents with a URL to the file, which worked. When switching over to get_file_data I left the path as a URL instead of switching it to a relative path. Once I did that, my headers appeared as they should. Thanks a bunch. – EHerman Sep 10 '14 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.