Search code examples
phpwordpressfunctionadvanced-custom-fields

Call ACF field in PHP function


I have this PHP function to add a scheduled banner to my website. I want to make it workable with ACF fields.

The function:

function holiday_site_banner() {
    
    $from_time = new DateTime( 'get_field('banner_start_date')' );
    $to_time = new DateTime( 'get_field('banner_end_date')' );

    if ( new DateTime() > $from_time && new DateTime() < $to_time ) {
        ?><div class="site-banner">
            <span>
                <strong style="">&#9432; LET OP:</strong> <?php get_field('banner_message')?>
            </span>
        </div><?php
    }
}
add_action( 'wp_footer', 'holiday_site_banner' );

I tried to use the get_field for implementing the ACF values in $from_time and $to_time but it doens't work. I set the ACF field for this as a datepicker with output Y-m-d H:i, named [banner_start_date] and [banner_end_date].

Then I also have a ACF textarea field named [banner_message] to place in between the <span>tags as you can see which also not works.

The ACF fields are added to a custom option page created with this code:

if( function_exists('acf_add_options_page') ) {
    
    acf_add_options_page(array(
        'page_title'    => 'DWD Vakantiesluiting',
        'menu_title'    => 'DWD Vakantie',
        'menu_slug'     => 'dwd-vakantiesluiting',
        'capability'    => 'edit_posts',
        'icon_url'      => 'dashicons-lock',
        'redirect'      => true,
        'updated_message' => __("Vakantiemodus ingesteld", 'acf'),
    ));
    
}

I modified the code to this but that still doesn't work:

function scheduled_site_banner() {
    // Get the start and end date values from ACF fields
    $start_date = get_field('banner_start_date', 'option');
    $end_date = get_field('banner_end_date', 'option');
    $banner_message = get_field('banner_message', 'option');

    // Create DateTime objects for the start and end date
    $start_time = new DateTime($start_date);
    $end_time = new DateTime($end_date);

    // Format the start and end date
    $formatted_start_date = date("F j, Y",strtotime($start_date));
    $formatted_end_date = date("F j, Y",strtotime($end_date));

    // Check if the current date and time falls between the start and end dates
    if ( new DateTime() > $start_time && new DateTime() < $end_time ) {
        ?>
        <div class="site-banner">
            <span>
                <strong>Important Notice:</strong> <?php echo $banner_message; ?>  Our site-wide banner is scheduled to display from <?php echo $formatted_start_date ?> to <?php echo $formatted_end_date ?>
            </span>
        </div>
        <?php
    }
}
add_action( 'wp_footer', 'scheduled_site_banner' );

Also using double quotations marks in // Get the start and end date values from ACF fields doesn't work.

Is there anyone who could help me with this?

Thanks in advance!


Solution

  • I got it done, thanks for the help. The problem was the date format among some other things. I also added Europe/Amsterdam Timezone.

    This is the correct code:

    function scheduled_site_banner() {
        // Get the start and end date values from ACF fields
        $start_date = get_field("banner_start_date", "option");
        $end_date = get_field("banner_end_date", "option");
        $banner_message = get_field("banner_message", "option");
    
        // Create DateTime objects for the start and end date and set Amsterdam timezone
        $start_time = DateTime::createFromFormat("Y-m-d H:i:s", $start_date, new DateTimeZone('Europe/Amsterdam'));
        $end_time = DateTime::createFromFormat("Y-m-d H:i:s", $end_date, new DateTimeZone('Europe/Amsterdam'));
    
        if($start_time === false || $end_time === false) {
            // date is not in correct format
            return;
        }
    
        // Check if the current date and time falls between the start and end dates
        if ( new DateTime('Europe/Amsterdam') > $start_time && new DateTime('Europe/Amsterdam') < $end_time ) {
            ?>
            <div class="site-banner">
                <span>
                    <strong>Important:</strong> <?php echo $banner_message; ?>
                </span>
            </div>
            <?php
        }
    }
    add_action( 'wp_footer', 'scheduled_site_banner' );
    add_filter('acf/format_value', 'do_shortcode');
    

    I also added 2 shortcodes which can be used in the message to display the start and enddates which are set in the acf fields. I used this code for that:

    function register_acf_shortcodes() {
       add_shortcode('begin_vakantie', 'acf_banner_start_date_shortcode');
       add_shortcode('einde_vakantie', 'acf_banner_end_date_shortcode');
    }
    add_action( 'init', 'register_acf_shortcodes');
    
    function acf_banner_start_date_shortcode() {
       $start_date = get_field('banner_start_date', 'option');
       return date('d-m-Y', strtotime($start_date));
    }
    
    function acf_banner_end_date_shortcode() {
       $end_date = get_field('banner_end_date', 'option');
       return date('d-m-Y', strtotime($end_date));
    }
    

    And added this filter to the function scheduled_site_banner() as you can see:

    add_filter('acf/format_value', 'do_shortcode');

    Last but not least I used this CSS for styling the banner and positioned it at the bottom of the page:

    .site-banner {
        position: fixed;
        bottom: 0;
        width: 100%;
        background-color: #cf2e2e;
        padding: 1em;
        z-index: 99999;
        text-align: center;
          color: #ffffff;
    }