1

Let my explain the scenario first:

I have a very custom page template. That is use in combination with a CPT and a few Custom fields.

The CPT is called "werklijsten" (it's in dutch so bare with me) and supports the title and the editor. Then I have connected a few custom fields to this CPT: Date, shift, team, and 4 text area boxes. The idea is that a Teamleader will preffil the CPT first with the following basic info: The title, Some text in the editor (the content) to inform them about stuff that the workers need to pay attention too, The Date, The Shift and what Team it's for. Then the Post is published. The other 4 text area are for the workers to fill out during there shift. I don't want to give the workers access to the WP backend (only the teamleader can publish new Custom posts). So I have created a front end (archive) page where the workers can edit the custom posts (and edit only the 4 text fields).

Now the workers have the option to fill out the rest of the post (form) and save the posts so they can enter more stuff later. There is also an Email button in the form. This form looks like this:

At the end of the shift they want the option to send out an email to the managers with the complete content, including any images inserted by the teamleader in the content editor.

Here is the code I used to do all this:

// Template Name: Page werklijst edit 

// Declare static variables. 

$lijst_id = get_query_var('lijst_id'); // allert! this must be in functions.php!
$post_id = $lijst_id;
$url = site_url();
if ($id == "") {
    header("Location: ".$url."/werklijst");
}
$current_user = wp_get_current_user();
$username = $current_user->display_name;

// Get lists where postid = url id
$args = array (
  'post_type'              => array( 'werklijst' ),
    'p'                      => $lijst_id,
);

$lijsten = new WP_Query( $args ); 

wp_reset_postdata();


// Save post when button save is clicked
if (isset($_POST['save'])  && (isset($_POST['form1']) )) {
        global $wpdb;
        // Declare form variables
      $FormNaam = sanitize_text_field($_POST['Naam']);
      $FormOps = sanitize_text_field($_POST['Ops']);
        $FormAdmin = sanitize_text_field($_POST['Admin']);
      $FormPrep = sanitize_text_field($_POST['Prep']);
      $FormNotPrep = sanitize_text_field($_POST['NotPrep']);
        $table = $wpdb->postmeta;

    // update fields
        $result = $wpdb->query("
            UPDATE $table
            SET meta_value = '$FormOps' 
            WHERE post_id = $lijst_id AND meta_key = 'operationeel'");
      $result = $wpdb->query("
            UPDATE $table
            SET meta_value = '$FormAdmin'   
            WHERE post_id = $lijst_id AND meta_key = 'administratief'");
      $result = $wpdb->query("
            UPDATE $table
            SET meta_value = '$FormPrep'    
            WHERE post_id = $lijst_id AND meta_key = 'voorbereid'");
      $result = $wpdb->query("
            UPDATE $table
            SET meta_value = '$FormNotPrep' 
            WHERE post_id = $lijst_id AND meta_key = 'niet_voorbereid'");

            header("Location: ".$url."/werklijst");
        $wpdb->flush();
}

//  Send Email and put post in trash after success and update is clicked
if (isset($_POST['update'])  && (isset($_POST['form1']) )) {
        global $wpdb;
        // Declare form variables
      $FormDePloeg = ($_POST['DePloeg']);
      $FormDeDatum = ($_POST['DeDatum']);
      $FormDeDienst = ($_POST['DeDienst']);
      $FormNaam = ($_POST['Naam']);
      $FormDeContent = ($_POST['DeContent']); /* get the content from this post*/ 
      $FormOps = sanitize_text_field($_POST['Ops']);
      $FormAdmin = sanitize_text_field($_POST['Admin']);
      $FormPrep = sanitize_text_field($_POST['Prep']);
      $FormNotPrep = sanitize_text_field($_POST['NotPrep']);    
      $table = $wpdb->postmeta;
      $table2 = $wpdb->posts;
      $FormDeContent = the_content();

        // create email
        $to = "myemail@email.com";
        $subject = "Werklijst - " . $FormDeDatum . " - Ploeg: " . $FormDePloeg . " - " . $FormDeDienst ;
        $body = "<body style=\"font-family:Verdana, Verdana, Geneva, sans-serif; font-size:14px; background-color:#fff; color:#000;\">\n";  
        $body .= "Geachte collega,<br \><br \>\n";
        $body .= "Dit is het verslag van de overdracht.<br \><br \><br \>\n";
      $body .= "<strong>Opmerkingen van de Teamleader:</strong><br \>\n";
      $body .= $FormDeContent . "<br \><br \><br \>\n";
      $body .= "<strong>Operationele opmerkingen:</strong><br \>\n";
      $body .= $FormOps . "<br \><br \><br \>\n";
      $body .= "<strong>Administratieve opmerkingen:</strong><br \>\n";
      $body .= $FormAdmin . "<br \><br \><br \>\n"; 
      $body .= "<strong>Voorbereid opmerkingen:</strong><br \>\n";
      $body .= $FormPrep . "<br \><br \><br \>\n"; 
      $body .= "<strong>Niet Voorbereid opmerkingen:</strong><br \>\n";
      $body .= $FormNotPrep . "<br \><br \><br \>\n"; 
        $body .= "Met vriendelijke groeten, <br \>\n";
        $body .= "\n";
      $body .= $FormNaam. "\n";
        $body .= "</body>\n";
        $headers = array(
            'Content-Type: text/html; charset=UTF-8',
            'From: Companyname <no-reply@email.coml>',
        );
        wp_mail( $to, $subject, $body, $headers );

 // update velden

        $result = $wpdb->query("
            UPDATE $table
            SET meta_value = '$FormOps' 
            WHERE post_id = $lijst_id AND meta_key = 'operationeel'");
      $result = $wpdb->query("
            UPDATE $table
            SET meta_value = '$FormAdmin'   
            WHERE post_id = $lijst_id AND meta_key = 'administratief'");
      $result = $wpdb->query("
            UPDATE $table
            SET meta_value = '$FormPrep'    
            WHERE post_id = $lijst_id AND meta_key = 'voorbereid'");
      $result = $wpdb->query("
            UPDATE $table
            SET meta_value = '$FormNotPrep' 
            WHERE post_id = $lijst_id AND meta_key = 'niet_voorbereid'");

       //put post in the trash after sending email 
       wp_trash_post( $lijst_id ); 

        header("Location: ".$url."/werklijst");
        $wpdb->flush();
        }

get_header(); ?>

<body>
   <div id="primary" class="site-content">
    <div id="content" role="main">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
      <div class="post" id="post-<?php the_ID(); ?>">
        <div class="entry-content hentry">
          <?php the_content(); ?>
        <?php endwhile; ?>
    <?php endif; wp_reset_postdata(); ?>
        </div>
    </div>
<!-- start loop hier -->
<?php 
while ($lijsten->have_posts()) : $lijsten->the_post(); 
    $werklijstid = get_the_ID();
    $title = get_the_title();
    $content = get_the_content();
    $date = get_the_date();
    $datum = get_field('datum');
    $dienst = get_field('dienst');
    $ploeg = get_field('ploeg');
    $ops = get_field('operationeel');
    $admin = get_field('administratief');
    $prep = get_field('voorbereid');
    $notprep = get_field('niet_voorbereid');    

endwhile ?> 
<!-- stop loop here -->

<!-- Custom form goes here-->
<div class="entry-content">

        <h3><?php echo $datum . " - " . $dienst . " - Ploeg: " . $ploeg; ?></h3>

    <?php if ($content != "") { ;?>
    <span class="rood">Opmerkingen van de Teamleader:</span> <br />
    <p class="rood"><strong><?php echo $content; ?></strong></p>
    <?php } else { ;?>
  <p class="rood">Er zijn (nog) geen opmerkingen van de Teamleader</p>
        <?php   }; ?>   

                <form action="<?php echo site_url(); ?>/edit-werklijst?lijst_id=<?php echo $lijst_id; ?>" method="POST" name="form1">
            <table class="kraan-table" width="100%" cellpadding="3" cellspacing="3">
                    <tr valign="baseline">
                        <th align="left">Operationeel:</th>
                        <td>
                            <textarea name="Ops" cols="45" rows="5" maxlength="2055"><?php echo $ops; ?></textarea>
                        </td>
                    </tr>
                    <tr valign="baseline">
                        <th align="left">Administratief:</th>
                        <td>
                                <textarea name="Admin" cols="45" rows="5" maxlength="2055"><?php echo $admin; ?></textarea> 
                        </td>
                    </tr>
                    <tr valign="baseline">
                        <th align="left">Voorbereid:</th>
                        <td>
                                <textarea name="Prep" cols="45" rows="5" maxlength="2055"><?php echo $prep; ?></textarea>
                        </td>
                    </tr>
                  <tr valign="baseline">
                        <th align="left">Niet Kunnen Voorbereid:</th>
                        <td>
                                <textarea name="NotPrep" cols="45" rows="5" maxlength="2055"><?php echo $notprep; ?></textarea>
                                (Reden erbij vermelden)
                        </td>
                    </tr>
                    <tr valign="baseline">
                        <td>&nbsp;</td>
                        <td>
                            <input type="submit" name="update" value="Verstuur werklijst!" />
                            <input type="submit" name="save" value="Opslaan" />
                            <input type="hidden" name="form1" id="form1" value="form1" />
                            <input type="hidden" name="Naam" id="Naam" value="<?php echo $username; ?>" />
                            <input type="hidden" name="DePloeg" id="DePloeg" value="<?php echo $ploeg; ?>" />
                            <input type="hidden" name="DeDatum" id="DeDatum" value="<?php echo $datum; ?>" />
                            <input type="hidden" name="DeContent" id="DeContent" value="<?php echo $content; ?>" />
                            <input type="hidden" name="DeDienst" id="DeDienst" value="<?php echo $dienst; ?>" />
                        </td>
                    </tr>
                </table>
        </form>
</div><!-- # entry content -->
</div><!-- #content -->
</div><!-- #primary -->

<?php get_footer(); ?>

Here is the problem: When the email is send I can't see the content in the email. If I use the_content or get_the_content it will not show up in the email. I have tried to put the_content or get_the_content in a hidden text field in the form. But that only works with just plain text in the content. If I use an image in the content then it breaks.

So my question in the end is: What is the best way to add the_content filled out by the teamleader to be in my email? That also includes any images or other html used in the editor?

Many thanks in advance, Richard

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.