1

I'm getting the PHP error "call to undefined function get_field" once in awhile, but not consistently.

Advanced Custom Fields says to prevent this, wrap my function in acf/init to be sure ACF is fully loaded before get_field is called.

What I have is below, but the problem is now that the output from the shortcode bulletins does not show on the front end.

I'm using ACF Pro 6.4.2 and that has the feature of adding Options Pages from the GUI admin, and I don't have to init options pages separately in functions.php

https://www.advancedcustomfields.com/resources/options-page/

In my code below:

Am I using acf/init correctly in functions.php?

Are options fields handled differently by acf/init?

What about using the shortcode? Is that a problem?

Is using a global i.e., global $bulletin_text a bad idea?

add_action('acf/init', function() {

        $bulletin_text = get_field('bulletin_text', 'option');

            function show_shortcode_bulletin(){
            global $bulletin_text;
            return $bulletin_text;
        }
    add_shortcode('bulletins', 'show_shortcode_bulletin');

});

1 Answer 1

-1

show_shortcode_bulletin becomes a local function. It's only accessible within that specific acf/init execution. add_shortcode needs a globally accessible function.

$bulletin_content = '';

add_action('acf/init', function() {
    global $bulletin_content; 

    $bulletin_content = get_field('bulletin_text', 'option');

    if ( !$bulletin_content ) {
        $bulletin_content = '';
    }
});

function show_shortcode_bulletin() {
    global $bulletin_content;

    return $bulletin_content;
}

add_shortcode('bulletins', 'show_shortcode_bulletin');
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, why are you using $bulletin_content = ''; and the if conditional?
to define a global variable to store the bulletin text. and the if conditional for ensure it's an empty string if no content is found.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.