0

TY for opening my question! Im trying to update the meta key 'license' in a custom post that I have, with the ID of an existing WP post. So Im using a global variable to store the ID of the WP post within the loop in the single.php file:

$GLOBALS['greenhorn']= get_the_ID();

Now, I am trying to pass it to a function in a plugin, to update the meta as follows (I pasted my function, as underneath, in the functions.php of the child theme):

function the_some_meta_update( $post_id ) 
{
update_post_meta( $post_id, 'license', $greenhorn );
}

add_action('fes_submission_form_save_custom_fields', 'the_some_meta_update', 10, 3);

The keen reader has figured that the hook name - add action was used to trigger the function each time a new custom post was created in a plugin to create these custom posts (EasyDigitalDownloads FES). I keep getting the value of the meta key 'license' as null at each attempt. Am I writing my hook wrong? or is my global variable malfunctioning?

These are the plugin instructions:

This is for developers to add dynamic elements as they want. It provides the chance to add whatever input type you want to add in this form. You can bind your own functions to render the form to this action hook. You'll be given 3 parameters to play with: $form_id, $post_id, $form_settings.

add_action('{hookname}', 'my_function_name}', 10, 3 );
// first param: Form Object
// second param: Save ID of post/user/custom
// third param: Field Object
function my_function_name( $form, $save_id, $field ) {
    // Do whatever you want here
}

THANKS.
EDIT: added 2 pictures. what Im doingwhat is also an option

EDIT: 1 picture showing DB update and working of the functionenter image description here

  • The first problem I'm seeing is that you're not accessing the global $greenhorn in the_some_meta_update(). You need to add global $greenhorn to tell the function to use the global variable. However I think a bigger problem is that the global variable won't be set when the submission is processed, because it will be handled on a separate request. You probably need to create a hidden input, store the post ID in that, then look for that field when saving. This is all stuff that's purely about the 3rd-party plugin. You should consult its support resources for further help. – Jacob Peattie Mar 2 '18 at 2:07
  • TY @JacobPeattie - much appreciated. – Ebe Mar 2 '18 at 2:26
  • I added the line >global $greenhorn; within the definition of the function so the global variable was referenced. But still NULL :) So I did what you were referencing, Im a PHP novice (1 Udemy course) - learning ... I have this I believe you were referring to: <form class="still-figuring-which-class" action="still-not-sure" method="post"> <!-- getting the post ID into "postid" and pass it to somewhere Im still wondering--> <input type="hidden" name="postid" value="<?php the_ID(); ?>" /> <input type="submit" value="Edit" /> </form> – Ebe Mar 2 '18 at 2:32
  • That's the idea, but presumably this plugin already has a form that you need to add an input to. They would need to provide a hook for you to do that. Check its developer documentation, if it has any. – Jacob Peattie Mar 2 '18 at 2:34
  • Indeed this form plugin has an action hook. As you see in the 1st image, I have added the hook add_action('fes_submission_form_save_custom_fields', 'the_some_meta_update', 10, 3); sadly/unfortunately there is no other documentation. In the second image we can see I can directly enter the updated value for license but the meta cannot be updated directly with my tests. I believe my hook function works because if I contain the global greenhorn in quotes - the database shows the meta value of license key as updated with the string '$greenhorn' – Ebe Mar 2 '18 at 2:44

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.