Title: Custom Fields to variable in PHP
Last modified: March 30, 2018

---

# Custom Fields to variable in PHP

 *  [pd5dj](https://wordpress.org/support/users/erazotropa/)
 * (@erazotropa)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/custom-fields-to-variable-in-php/)
 * Hi, All
 * I have a question regarding the custom fields function when creating a Page.
 * I have created a custom template page.
 * This cointains:
 *     ```
       <?php /* Template Name: Digital_Log */
       get_header(); ?>
       	<html>
       		<iframe src="/logbook/log.php" width="100%" height="6500" style="border:none" scrolling="no";></iframe>
       	</html>
       <?php get_footer(); ?>
       ```
   
 * This works, I get the header, footer and the redirected page in the middle of
   it.
 * Now I want to create a Custom Field named: “url-name”
    The value of the field
   is: “/logbook/log.php”
 * I want to pass this to a variable in to the custom template file in PHP.
 * I have tried this example:
    `<?php $key="mykey"; echo get_post_meta($post->ID,
   $key, true); ?>` Where $key = “url-name”
 * But I get nothing.
 * Any Advice would be apreciated.
 * Bjorn
    -  This topic was modified 8 years, 2 months ago by [pd5dj](https://wordpress.org/support/users/erazotropa/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fcustom-fields-to-variable-in-php%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 1 replies (of 1 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/custom-fields-to-variable-in-php/#post-10132881)
 * Well, your code says $key = “mykey”, not “url-name”. Easily fixed. Your other
   problem is probably $post->ID. $post is often not defined within the template’s
   scope. Generally, declaring `global $post;` fixes the scoping issue, but I think
   even better when you need a post ID is to use `get_the_ID()`. It works out all
   the vagaries of determining the post ID for you. I assume you wish to get the
   return value from get_post_meta() into the iframe src attribute. So in summary,
   try this:
 *     ```
       <?php $key="url-name";
       $url = get_post_meta( get_the_ID(), $key, true);
       ?>
       <iframe src="<?php echo &url; ?>" width="100%" height="6500" style="border:none" scrolling="no";></iframe>
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Custom Fields to variable in PHP’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 1 reply
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [8 years, 2 months ago](https://wordpress.org/support/topic/custom-fields-to-variable-in-php/#post-10132881)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
