• Hello,
    I’m probably getting this all confused but is there any way to get a variable from outside of the functions.php file (say in single.php) and feed it into a function?

    I’ve got a function which adds a field to a custom form and I want to feed a variable into the value of an input. The form is placed at the bottom of a post and when submitted it creates a custom post type post that gets displayed at the bottom of the regular post in a list format. I want to get the ID of the parent post. This will then feed into the value of the hidden input of the custom post type form, then I can do a query to get all custom post types with the meta_value of this ID.

    So I’ve got this:

    function feed_the_form_hook($form_id, $post_id, $form_settings) {
       $value = '';
    
       if ($post_id) {
          $value = get_post_meta($post_id, 'parent_post_id', true);
       }
       ?>
    
       <div class="wpuf-label">
          <label>Hidden Chalet Review ID</label>
       </div>
    
       <div class="wpuf-fields">
          <input type="text" name="parent_post_id" value="<?php echo esc_attr($parentPostID); ?>">
       </div>
    
       <?php
    
       echo $parentPostID; //Delete this once tested output
    }
    
    add_action('parent_post_id', 'feed_the_form_hook', 10, 3);

    The $parentPostID variable is getting the ID from the single.php file. I know this won’t work as is but is there any way of doing this or am I going about it in the wrong way?

    I did post something similar a while back but was asked to post in the plugin forum. I didn’t get an answer from there but this is actually a different problem. I’m simply wondering if this is possible within WordPress, regardless of plugins used etc?

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Why can’t you use $post->ID inside the main Loop?

    Thread Starter kieranmcclung

    (@kieranmcclung)

    Please excuse my ignorance.

    I’ve got the ID stored into the $parentPostID variable just fine, it’s trying to get that into the function so that when this custom field is added to the form it will contain that variable value.

    I don’t mean this to sound arsey, because it isn’t meant to, but how will $post->ID help me in this situation? It could be that I’m overcomplicating things, as usual.

    Thank you ??

    Can’t you call the function using feed_the_form_hook( $post->ID, xx)? Or if it’s being called outside of the Loop, save $post->ID to a variable within the Loop and then pass that variable via the function call elsewhere?

    Thread Starter kieranmcclung

    (@kieranmcclung)

    Ahh! Sorry, I understand now.

    The function is being called outside the loop unfortunately. I will try locate where it is actually being called. If I was to do this would the variable need to be declared as global?

    Again, apologies for my lack of knowledge.

    Edit: The function was taken from documentation and simply adds this field to a form that is generated elsewhere, so I just need to find where the form is generated.

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Calling variables into functions.php’ is closed to new replies.