Home Forums ACF PRO Question set Global variable ACF


Question set Global variable ACF

  • Hi guys, I’m building a theme with which I need to call many times some options ACF. Not to rewrite them every time I thought of creating a global variable to retrieve them more easily in all fields. It all works fine but I want from you an opinion whether this approach can be corrected and sustainable over time. I ask this because I’m new in PHP.

    function wedding_gloabal_cf() {
     	global $weddingopt;
     	$weddingopt = array(
     		// GALLERY ACF
    		'images_slide' 		=> get_field('gallery_wedding','option'),
    		'name_groom'		=> get_field('name_groom','option'),
    		'surname_groom' 	=> get_field('surname_groom','option'),
    		'name_bride'		=> get_field('name_bride','option'),
    		'surname_bride' 	=> get_field('surname_bride','option'),
    		'groom_full' 		=> get_field('name_groom','option') . ' ' . get_field('surname_groom','option'),
    		'bride_full' 		=> get_field('name_bride','option') . ' ' . get_field('surname_bride','option'),
    		'facebook_groom' 	=> get_field('facebook_groom','option'),
    		'twitter_groom' 	=> get_field('twitter_groom','option'),
    		'instagram_groom' 	=> get_field('instagram_groom','option'),
    		'facebook_bride' 	=> get_field('facebook_bride','option'),
    		'twitter_bride' 	=> get_field('twitter_bride','option'),
    		'instagram_bride' 	=> get_field('instagram_bride','option'),
    		'mike' 				=> __('Mike','italian-wedding-day'),
    		'mike_full' 		=> __('Mike Callagher','italian-wedding-day'),
    		'lisa' 				=> __('Lisa','italian-wedding-day'),
    		'lisa_full' 		=> __('Lisa Connor','italian-wedding-day'),
    		'photo_groom' 		=> get_field('photo_groom','option'),
    		'photo_bride' 		=> get_field('photo_bride','option'),
    		'biography_groom' 	=> get_field('biography_groom','option'),
    		'biography_bride' 	=> get_field('biography_bride','option'),
    add_action( 'init', 'wedding_gloabal_cf' );

    in my file functions.php of my theme i insert this line:

    require_once get_template_directory() . '/inc/builder/globalcf.php';

    Thank you very much guys,

  • If you haven’t already I would set the options page to autoload. This causes WP to cache the values so that extra DB calls are not needed to get them, just in case that’s your concern.

    As far as opinion on using a global variable. Global variables, in my opinion, should only be used when there isn’t a better way to do it.

    I personally don’t see much difference between the following.

    // ACF get_field()
    $value = get_field('gallery_wedding','option');
    // using a global variable
    global $weddingopt;
    $value = $weddingopt['gallery_wedding'];

    Of the two I would use the first. Why, because at does not depend on a global variable and because it does not create a dependency and require editing the function that creates the global variable every time I add a new field. The global variable option simply increases the work and over-complicates the theme without adding much in the way of a benefit over just using the ACF function where you need the value.

  • Thank you @hube2, last question.Since I often use combinations combined with fields acf which often are repeated in various parts of the template (such as the name of the couple, and if this alternative is shown as an example), what do you think the use of functions like this?

    function my_function(){
      $biography_groom = get_field('field','option');
        echo $biography_groom;
      } else {
        _e('Here to tell a brief description of the bride. You can put whatever you like, even your intentions towards the groom.','italian-wedding-day');
  • If you’re going to use alternates when no value is entered then you need to do something. Most of the time I just code it where I’m getting the field, but a function like you show does the same thing. Actually, with a function, if you have all of these default values functions in one file it will make edits easier, so it’s not a bad idea. I’m probably a bit different than other developers, when I always try to do things in ways that will make it easier to maintain or change in the future and it’s what I use to decide what to do.

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

The topic ‘Question set Global variable ACF’ is closed to new replies.