WordPress.org

Ready to get started?Download WordPress

Forums

Subscribe2
Advanced Custom Fileds in mail body are empty (8 posts)

  1. dobst
    Member
    Posted 8 months ago #

    Hello,

    I'm running the latest version (10.10) of S2 and try to add custom fields (from the Advanced Custom Fields plugin) to the mail body.

    I'm using the s2_html_email and s2_plain_email filters (S2-API) and a custom function to modify the mail body. This works if the post is saved before it's published. If it's published directly without have been saved as draft the get_the_ID() and equivalents are still empty and I can't post the meta data with

    get_post_meta(get_the_ID(),'<advanced custom field>',true);

    This problem is very similar to
    http://wordpress.org/support/topic/subscribe2-and-custom-fields?replies=5 and
    http://wordpress.org/support/topic/include-custom-fields-in-post-e-mails?replies=4

  2. mattyrob
    Member
    Posted 8 months ago #

    @dobst,

    Subscribe2 uses the WordPress post transition hooks to trigger the email notification events. Unfortunately the calls to create or update post meta data happen after this so essentially if you post directly the Subscribe2 events are triggered before the custom fields exist. You need to save first to create the meta before hitting publish.

  3. dobst
    Member
    Posted 8 months ago #

    Hello Matty, thank you for your reply. How can I achieve this? Custom fields are "begin_date" and "end_date". I tried to force a "save" before publishing with the following, various actions in my functions.php:

    function save_details() {
     global $post;
     update_post_meta($post->ID, "begin_date", $_POST["begin_date"]);
     update_post_meta($post->ID, "end_date", $_POST["end_date"]);
     add_post_meta($post->ID, 'begin_date', $_POST["begin_date"], false);
     add_post_meta($post->ID, 'end_date', $_POST["end_date"], false);
    }
    add_action('pre_post_update', 'save_details');
    add_action('before_publish_post', 'save_details');

    But it doesn't work.

  4. mattyrob
    Member
    Posted 8 months ago #

    @dobst,

    The 'pre_post_update' hook only fires if content is being updated, if you are immediately publishing then it won't be an update.

    I cannot find a 'before_publish_post' hook at all.

    Maybe try using the 'transition_post_status' hook or have a look through the wp-includes/post.php file for a more appropriate hook.

  5. dobst
    Member
    Posted 8 months ago #

    Ok, thank you for the hint. There is a bunch of transiton_post_status hooks. But non works :-( Hope, the function save_details() is correct:

    function save_details() {
     global $post;
     update_post_meta($post->ID, "begin_date", $_POST["begin_date"]);
     update_post_meta($post->ID, "end_date", $_POST["end_date"]);
     add_post_meta($post->ID, 'begin_date', $_POST["begin_date"], false);
     add_post_meta($post->ID, 'end_date', $_POST["end_date"], false);
    }
    add_action('new_to_publish', 'save_details');
    add_action('pending_to_publish', 'save_details');
    add_action('draft_to_publish', 'save_details');
    add_action('auto-draft_to_publish', 'save_details');
    add_action('future_to_publish', 'save_details');
    add_action('private_to_publish', 'save_details');
    add_action('trash_to_publish', 'save_details');
  6. mattyrob
    Member
    Posted 8 months ago #

    @dobst,

    Those are the exact same hooks that Subscribe2 uses so your code may still get called after Subscribe2. The way to ensure your code is called first is by using the priority parameter of the add_action() function.

    Maybe try something like:
    add_action('new_to_publish', 'save_details', 5);

  7. dobst
    Member
    Posted 8 months ago #

    Mh, I tried to raise the priority (lower the value) without an effect.

  8. mattyrob
    Member
    Posted 8 months ago #

    @dobst,

    I think you may need to examine the contents of $_POST more closely. The meta files are contained in $_POST['meta'] and it's a nested array with 'key' and 'value' pairs. I suspect the issue is because variables like $_POST["begin_date"] will be undefined.

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.