Viewing 15 replies - 1 through 15 (of 17 total)
  • I just went through something like this. I did it the very hard way and then, after some great feedback from the developer, did it a much easier way.

    My advice is DON’T add the information until you get to the “postie_post_before” filter. The initial documentation I read only had one input to your filter: $post. So I did a lot of manipulation with “postie_post_pre” where the same documentation said you have access to $email. I pulled the “from” header information and then added it to the content of the email for later processing with my filter for “postie_post_before”. Somewhere along the way before it got to “postie_post_before”, the email addresses became HTML and was way more complicated to parse.

    BUT as the developer pointed out, other (newer?) documentation for “postie_post_before” shows that you have access not only to $post, but to $email as well. So you can get the from information out of $headers[‘from’][‘mailbox’], $headers[‘from’][‘host’] and $headers[‘from’][‘personal’]. It’s very clean and can be added to $post[‘post_content’] wherever you want it.

    This is the documentation that describes calling your filter with both parameters: https://postieplugin.com/postie_post_before/. Not sure how I missed it, but there it was.

    Hope something in that helps.

    Plugin Author Wayne Allen

    (@wayneallen-1)

    What @dburk said.

    Thread Starter andybarabash

    (@andybarabash)

    Thanks for the info, but I’m afraid I’m still not getting this.
    I created a a file called FilterPostie.php, and added the filter there:

    <?php 
    function my_postie_post_function($post) {
        //do something here like update $post['post_content']
        return $post;
    }
    
    add_filter('postie_post_before', 'my_postie_post_before', 10, 2);
    
    function my_postie_post_before($post, $headers) {
        //Do something
        return $post;
    }
    
    ?>

    Seems like now I just need to (1) introduce $email parameter somewhere and (2) add it to the post content (so that the first line of the post is “Sent from: $email”, and starting from the second line there goes email content). Am I right? What should I do next?
    Sorry for all these questions, I’m not a programmer at all.

    • This reply was modified 7 years, 11 months ago by andybarabash.

    I’m not sure what you’re doing with the my_postie_post_function, so we’ll ignore that. I hope this helps:

    add_filter(‘postie_post_before’, ‘my_postie_post_before’, 10, 2);
    function my_postie_post_before($post,$headers) {
    $from = $headers[‘from’][‘mailbox’] . ‘@’ . $headers[‘from’][‘host’];
    // you also might want to do something with $headers[‘from’][‘personal’];
    $content = $post[‘post_content’];

    //This is where you would do all your manipulation
    //One very simple example might be:
    $newcontent = $fom . $content;

    //Then put it back in $post and you’re done
    $post[‘post_content’] = $newcontent;
    return $post;
    }

    sorry, typo…

    //One very simple example might be:
    $newcontent = $from . $content; //’from’ (not ‘fom’)

    Plugin Author Wayne Allen

    (@wayneallen-1)

    To add a little formatting you might do something like:

    $newcontent = "<p>$from</p> $content";

    Thread Starter andybarabash

    (@andybarabash)

    Thank you a lot! This really helps.
    I’ve updated my filterPostie.php file (and Postie sees it in debug mode), but unfortunately nothing changes when it comes to publishing new posts from emails ?? So, a line with ‘From’ email does not show up.

    My filterPostie.php now looks like this:

    <?php 
    add_filter(‘postie_post_before’, ‘my_postie_post_before’, 10, 2);
    function my_postie_post_before($post,$headers) {
    $from = $headers[‘from’][‘mailbox’];
    
    $content = $post[‘post_content’];
    
    $newcontent = "<p>Sent from: $from <br></p> $content"; 
    
    $post[‘post_content’] = $newcontent;
    return $post;
    }
    
    ?>

    Try:

    $newcontent = “<p>Sent from: ” . $from . “<br></p>”. $content;

    Thread Starter andybarabash

    (@andybarabash)

    I modified the code, here’s how it looks now:

    <?php 
    add_filter(‘postie_post_before’, ‘my_postie_post_before’, 10, 2);
    function my_postie_post_before($post,$headers) {
    $from = $headers[‘from’][‘mailbox’] . "@" . $headers[‘from’][‘host’];
    
    $content = $post[‘post_content’];
    
    $newcontent = "<p>Sent from: " . $from . "<br></p>". $content;
    
    $post[‘post_content’] = $newcontent;
    return $post;
    }
    
    ?>

    But still doesn’t work. Nothing is shown in posts except for email content.

    Plugin Author Wayne Allen

    (@wayneallen-1)

    I’d add:
    echo "in my_postie_post_before";
    in my_postie_post_before then click the debug button and look for that in the output to make sure the filter is getting called.

    Thread Starter andybarabash

    (@andybarabash)

    Checked this, the filter is being called:
    postie

    Plugin Author Wayne Allen

    (@wayneallen-1)

    please post the entire filterPostie.php, based on where the “in my_postie_post_before” showed up in the output something isn’t quite right.

    Thread Starter andybarabash

    (@andybarabash)

    Here’s what I have inside filterPostie.php

    <?php 
    add_filter(‘postie_post_before’, ‘my_postie_post_before’, 10, 2);
    
    function my_postie_post_before($post,$headers) {
    $from = $headers[‘from’][‘mailbox’] . "@" . $headers[‘from’][‘host’];
    
    $content = $post[‘post_content’];
    
    $newcontent = "<p>Sent from: " . $from . "<br></p>". $content;
    
    $post[‘post_content’] = $newcontent;
    return $post;
    }
    
    ?>

    I put echo “in my_postie_post_before”; instead of the function my_postie_post_before {…} part

    • This reply was modified 7 years, 11 months ago by andybarabash.
    Plugin Author Wayne Allen

    (@wayneallen-1)

    I’m not sure what is happening, when I put this in my filterPostie.php it works.

    <?php
    
    add_filter('postie_post_before', 'my_postie_post_before', 10, 2);
    
    function my_postie_post_before($post, $headers) {
        echo "my_postie_post_before";
        $from = $headers['from']['mailbox'] . "@" . $headers['from']['host'];
    
        $content = $post['post_content'];
    
        $newcontent = "<p>Sent from: " . $from . "</p>" . $content;
    
        $post['post_content'] = $newcontent;
        return $post;
    }
    
    ?>
    Plugin Author Wayne Allen

    (@wayneallen-1)

    I will say that I am running Postie 1.8.22 and you are running 1.7.32. Is there any reason you are on the older version?

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Add “From” field into post content’ is closed to new replies.