Support

Account

Home Forums General Issues Using True / False ACF in functions.php

Solving

Using True / False ACF in functions.php

  • Hello,

    I would like to be able to send the contents of a new post to my email, if i where to check a “send me this post on email”-box. I would like to have a True/False ACF checkbox on my posts, and if it is checked when i publish the post, the following code from the functions.php would be run (causing me to get an email with the post conents). If the box is not checked, i do not want the code to run. I found the code in the WordPress Codex

    <?php
    
    // SEND EMAIL ONCE POST IS PUBLISHED
    
    function notify_new_post($post_id) {
        if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {
            $post = get_post($post_id);
            $author = get_userdata($post->post_author);
            $author_email = $author->user_email;
            $email_subject = "Your post has been published.";
    
            ob_start(); ?>
    
            <html>
                <head>
                    <title>New post at <?php bloginfo( 'name' ) ?></title>
                </head>
                <body>
                    <p>
                        Hi <?php echo $author->user_firstname ?>,
                    </p>
                    <p>
                        Your post <a href="<?php echo get_permalink($post->ID) ?>"><?php the_title_attribute() ?></a> has been published.
                    </p>
                </body>
            </html>
    
            <?php
    
            $message = ob_get_contents();
    
            ob_end_clean();
    
            wp_mail( $author_email, $email_subject, $message );
        }
    }
    
    add_action( 'publish_post', 'notify_new_post' ); ?>
  • change the opening if statement in your function (assuming your checkbox field has a key of field_asd856f4a8sdf

    
     if( ( $_POST['post_status'] == 'publish' ) && 
         ( $_POST['original_post_status'] != 'publish' ) 
         ( !empty( $_POST['fields']['field_asd856f4a8sdf'] ) 
       ) {
    

    check the source code after your page loads to determine the acf key. it will look like that one i’ve put there, even though i made that one up.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Using True / False ACF in functions.php’ is closed to new replies.