Title: Custom Fields in a dynamic css file using .php
Last modified: August 19, 2016

---

# Custom Fields in a dynamic css file using .php

 *  [Matthew Simo](https://wordpress.org/support/users/mattsimo/)
 * (@mattsimo)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/custom-fields-in-a-dynamic-css-file-using-php/)
 * Hi, I’m doing an experiment.
 * I’m trying to pass the custom fields of a post to a post-css.php file that uses
   header(“Content-type: text/css”); that lists the content as css but seem to be
   having a problem somewhere. I’m linking to this file in my header.php as an external
   style sheet link but I’m not getting any results.
 * The custom fields I’m working with specifically hold valid css syntax.
 * Whenever I just plop the custom fields into the head it works but I really wanted
   to keep my head tidy.
 * The best I can figure, the issue has to do with the get_post_meta not having 
   a proper $post_id to pull the $key & $value from since it is it’s own secluded
   page that dynamically creates the content.
 * I’m sure I did a terrible job explaining my problem, but if anyone has any ideas
   of how to implement a quick, easy, and effective solution then I’d be all ears.
   I’m not all that good at php so if it gets deep can you explain the process/logic
   please?
 * Thanks a load.
    Matthew Simo

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

 *  [EMG](https://wordpress.org/support/users/emg/)
 * (@emg)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/custom-fields-in-a-dynamic-css-file-using-php/#post-1138967)
 * You should be able to simply link to your stylesheet in the head?
 * Are your links to the stylesheet(s) valid? Do you use pretty permalinks? What
   does your actual CSS link look like in the theme file? Are you using relative
   or absolute paths?
 *  Thread Starter [Matthew Simo](https://wordpress.org/support/users/mattsimo/)
 * (@mattsimo)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/custom-fields-in-a-dynamic-css-file-using-php/#post-1139014)
 * I’m using absolute paths as this is a test on a local install. The CSS link looks
   like a normal css <link>: I basically check to see if there is a $key “post_css”
   and if there is I assign the value to $css. If $css is NOT empty I put in the
   <link> to the post-css.php file that is in my child theme directory.
 *     ```
       $css = get_post_meta($post->ID, 'post_css', true);
       if(!empty($css)){?>
   
       <link type="text/css" rel="stylesheet" href="http://localhost:8888/wordpress/wp-content/themes/simo-portfolio&blog/post-css.php" media="screen" />
       ```
   
 * In the post-css.php file I’ve got the following code:
 *     ```
       <?php header("Content-type: text/css");
   
       $css = get_post_meta($post->ID, 'post_css', true);
        echo $css;
       }
       ?>
       ```
   
 * Any ideas?
 *  Thread Starter [Matthew Simo](https://wordpress.org/support/users/mattsimo/)
 * (@mattsimo)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/custom-fields-in-a-dynamic-css-file-using-php/#post-1139077)
 * bump
 *  Thread Starter [Matthew Simo](https://wordpress.org/support/users/mattsimo/)
 * (@mattsimo)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/custom-fields-in-a-dynamic-css-file-using-php/#post-1139130)
 * So, after some extensive testing I’ve pinpointed where my code is breaking down.
   I must say, I didn’t expect it at all (it was the last thing I tested..)
 * So in the header I’m using:
 *     ```
       $css = get_post_meta($post->ID, 'post_css', true);
       if(!empty($css)){?>
   
       <link type="text/css" rel="stylesheet" href="http://localhost:8888/wordpress/wp-content/themes/simo-portfolio&blog/post-css.php?post-id=<?php echo $post->ID; ?>" media="screen" />
   
       <?php } ?>
       ```
   
 * And the code I’m using in post-css.php is:
 *     ```
       <?php header('Content-type: text/css'); ?>
   
       <?php
         $id = $_GET['post-id'];
         echo get_post_meta($id, 'post_css', true);
       ?>
       ```
   
 * As far as I can tell, the get_post_meta function isn’t working. Like, just flat
   out isn’t working. I tested each line prior to this and the values were being
   passed successfully. I used a constant in place of $id, and nothing. I assigned
   get_post_meta to another variable, attempted to echo the new variable, and nothing.
 * So, my new question, is there a function call I need to make to gain access to
   this function? I don’t consider myself a php developer so I’m halfway clueless
   here. The post-csss.php file is sitting in my theme folder, I assumed that is
   all I needed to do to gain access to all of the lovely WP functions. Anyone know
   why the get_post_meta stopped working? Or an idea on how to gain access to WP
   functions that aren’t really part of the WP architecture? Or even better, how
   to get this working?
 * Sorry for the super long post, but I do appreciate any insight!
    Thanks, Matthew
   Simo
 *  [randyhoyt](https://wordpress.org/support/users/randyhoyt/)
 * (@randyhoyt)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/custom-fields-in-a-dynamic-css-file-using-php/#post-1139136)
 * You can’t get access to the WordPress functions in that file because you are 
   not running it through the framework. If you hit a URL like this directly …
 * > `http://localhost:8888/wordpress/wp-content/themes/simo-portfolio&blog/post-
   > css.php?post-id=1`
 * … it doesn’t run through WordPress at all so it has no access to the functions.
   Just like if you hit …
 * > `http://localhost:8888/wordpress/wp-content/themes/simo-portfolio&blog/single.
   > php`
 * … directly, nothing will happen. It has to run through a WordPress URL.
 * I’m still not sure why you are not wanting to add the styles to the HEAD of the
   document. That is a completely valid, standards-based approach. If you have custom
   CSS that applies to one single page of the site, abstracting that out into a 
   separate file (that isn’t really a file) just does not make any sense: creating
   a fake re-usable file that won’t ever be re-used is horribly inefficient. (One
   extra unnecessary server request, too.) Especially if you have to query the database
   to figure out if it has custom values in the header.php and then have to query
   the database again in the post-css.php to output it.
 * Here’s the tutorial that walks through how to do it this way …
 * > `http://www.wprecipes.com/how-to-embed-css-in-your-posts-with-a-custom-field`
 * … though I would recommend adding it to functions.php and wp_head() instead of
   directly in header.php.
 *  [Matt Cohen](https://wordpress.org/support/users/mattyza/)
 * (@mattyza)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/custom-fields-in-a-dynamic-css-file-using-php/#post-1139271)
 * Hey mattsimo,
    I’m doing something similar to your post. In my research, I came
   across your post. I have a solution for you.
 * In your PHP file that you’re using as a CSS file, add the following after your
   header() declaration:
 *     ```
       require_once('../../../../wp-load.php');
       require_once('../../../../wp-includes/post.php');
       ```
   
 * The backslashes aren’t great, but can be neatened up at a later stage. The only
   requirement is that you make sure the number of ‘../’ is relative to the position
   of your PHP file in the filesystem.
 * The above lines performs the general WordPress load, allowing for access to various
   variables, etc, some of which are required by the second line, which is the location
   of the `get_post_meta()` function.
 * The rest, as you’ve written it using `get_post_meta()`, should be cool.
 * I hope this helps. 🙂
 * Cheers,
    Matt.
 *  [stfalx](https://wordpress.org/support/users/stfalx/)
 * (@stfalx)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/custom-fields-in-a-dynamic-css-file-using-php/#post-1139318)
 * Trying to do the same thing. Mattyza’s post was really helpful. The only thing
   keeping my get_post_meta() from working is the fact that it doesn’t have access
   to the ID of the post that’s currently being displayed. This happens only because
   the PHP file that i’m using as a CSS is running outside the wordpress system /
   framework. How do i give it access to the ID? Tried a lot of different things
   but nothing worked. 😐

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

The topic ‘Custom Fields in a dynamic css file using .php’ is closed to new replies.

## Tags

 * [css](https://wordpress.org/support/topic-tag/css/)
 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [dynamic CSS](https://wordpress.org/support/topic-tag/dynamic-css/)
 * [experiment](https://wordpress.org/support/topic-tag/experiment/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 7 replies
 * 5 participants
 * Last reply from: [stfalx](https://wordpress.org/support/users/stfalx/)
 * Last activity: [16 years, 2 months ago](https://wordpress.org/support/topic/custom-fields-in-a-dynamic-css-file-using-php/#post-1139318)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
