1

I have a bit of a unique situation that I haven't been able to find while Googling. Essentially I am creating a plugin and I created a meta box with the following code (this is the callback function):

// Callback function (creates meta box)
function perPageMeta($post) {
    echo 'Last Modified: ' . '<strong>' . $post->post_modified . '</strong>';
    $storedMetaData = get_post_meta($post->ID);
    ?>
    <form name="meta-box-<?php echo $post->ID; ?>" id="meta-box-field meta-box-<?php echo $post->ID; ?>">
        <input type="text" name="meta-text-businessName" id="meta-text-businessName" placeholder="Business Name"
    value="<?php if(isset ($storedMetaData['meta-text-businessName'])) echo $storedMetaData['meta-text-businessName'][0]; ?>"></input>
    </form>
<?php
wp_nonce_field(basename(__FILE__), 'meta-nonce'); // Security puproses

}

This is working. However, after I save the meta data (which is working) I want to retrieve the data in another function and then pass it to JavaScript so I can post it into the DOM of the website.

My problem is, I can't seem to retrieve the ID of the post in any way. I have tried passing it from the perPageMeta() function, but then it only runs the JS on the backend (in Wordpress admin), not on the actual site.

Here is my function to add the meta data:

 function addMetaData() {
 $value = get_post_meta($post->ID, 'meta-text-businessName', true);

  if(!empty ($value)) {
    $values = array();
    $values['name'] = $value;
    wp_enqueue_script('insert-meta', plugin_dir_url(__FILE__) . '/js/insert-metadata.js', array(jquery));
    wp_localize_script('insert-meta', 'settings', $values);     

   }
}

Currently I have these two functions disconnected, for testing purposes... But I am still struggling to find the solution.

So, how can I get the correct page ID to the addMetaData function? If I manually enter the correct ID the correct data is retrieved - but it needs to be dynamic. $post->ID returns null, apparently, if used in the addMetaData() function (although it works correctly in the perPageMeta function)

Both these functions are in the same script.

  • What hook are you attaching addMetaData() to? – Dave Romsey Sep 21 '16 at 20:24
  • In the past I have been using add_action('wp_enqueue_scripts', 'addMetaData'); but it wasn't getting me anywhere. – Namone Sep 21 '16 at 20:25
  • 1
    I just realized I'm an idiot...add_action('the_post', 'add'); solved my problem. Sorry for wasting your time. – Namone Sep 21 '16 at 20:27
  • All good! Please post your solution as an answer though, then drop by in a couple of days to accept it. It might help someone in the future and keeps the site's unanswered queue clean. – Dave Romsey Sep 21 '16 at 20:31
0

So, kind of a silly error on my part.

I needed to call my function from a place that had access to the variables I needed (such as the post ID). Using the following hook worked well:

add_action('the_post', 'add');

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.