1

I am using acfvalue variable to get value and use it in more than one shortcode, let me explain i want to store ACF value in one variable and then use that variable in 3 function and create 3 shortcode.

$acfvalue = get_field( 'short_title' );

above code is getting ACF value and storing in $acfvalue, now i want to use this variable in 3 different functions and create shortcode. i can declare and define 3 times $acfvalue and its working but i dont to fetch same value from post meta.

function hprice(){
    $acfvalue = get_field( 'short_title' );
    return '<h2>'. $acfvalue . " Price list in India" . '</h2>';
}
add_shortcode( 'pprice', 'hprice' );

function hspecs(){
    $acfvalue = get_field( 'short_title' );
    return '<h2>'. $acfvalue . " Full Specification" . '</h2>';
}
add_shortcode( 'pspecs', 'hspecs' );

function hreview(){
    $acfvalue = get_field( 'short_title' );
    return '<h2>'. $acfvalue . " Review" . '</h2>';
}
add_shortcode( 'preview', 'hreview' );

so above code is working.. problem is i want to move $acfvalue = get_field( 'short_title' ); outside of function.

2
  • Which one of the 3 is the one that will set the value for the others? because all 3 have that parameter and what happens if they set different values?
    – David Lee
    Commented Feb 14, 2017 at 4:47
  • each one will set the same value as the value is from advanced custom field of post.. and all 3 shortcode will be used in post. so everytime i set $acfvalue it get data from post meta..
    – Mangal
    Commented Feb 14, 2017 at 4:59

2 Answers 2

0

use function to call variable

   function give_value(){
      $acfvalue = get_field( 'short_title' );
     return $acfvalue;
   }
   function hprice(){
      return '<h2>'. give_value() . " Price list in India" .'</h2>';
   }
   add_shortcode( 'pprice', 'hprice' );

   function hspecs(){
     return '<h2>'.  give_value() . " Full Specification" . '</h2>';
  }
   add_shortcode( 'pspecs', 'hspecs' );
   function hreview(){
     return '<h2>'.  give_value() . " Review" . '</h2>';
   }
   add_shortcode( 'preview', 'hreview' );
2
  • thanks.. it it possible to execute $acfvalue = get_field( 'short_title' ); only once and use it in all the 3 function.. i want to reduce db calls as all the 3 shortcode will be used in post and all will have same value. i tried switch case but i got my website 500 error i am not a core programmer.
    – Mangal
    Commented Feb 14, 2017 at 5:03
  • Yes you can call this variable only once by creating class and put all functions inside class and use get_field as class poperty.
    – Sonali
    Commented Feb 14, 2017 at 5:07
0

There are multiple ways to call get_field( 'short_title' ) only once:

Using global variable:

You can declare the $acfvalue variable global and then use it inside your function. Since global scope may have the same variable name, it's better to prefix it so that it doesn't create conflicts:

function hprice() {
    global $wpse256370_acfvalue;
    if( ! isset( $wpse256370_acfvalue ) ) {
        $wpse256370_acfvalue = get_field( 'short_title' );
    }
    return '<h2>'. $wpse256370_acfvalue . " Price list in India" . '</h2>';
}
add_shortcode( 'pprice', 'hprice' );

function hspecs() {
    global $wpse256370_acfvalue;
    if( ! isset( $wpse256370_acfvalue ) ) {
        $wpse256370_acfvalue = get_field( 'short_title' );
    }
    return '<h2>'. $wpse256370_acfvalue . " Full Specification" . '</h2>';
}
add_shortcode( 'pspecs', 'hspecs' );

function hreview() {
    global $wpse256370_acfvalue;
    if( ! isset( $wpse256370_acfvalue ) ) {
        $wpse256370_acfvalue = get_field( 'short_title' );
    }
    return '<h2>'. $wpse256370_acfvalue . " Review" . '</h2>';
}
add_shortcode( 'preview', 'hreview' );

Using static variable:

Using static variable inside a function will be better than the global variable way. Since it'll not pollute the global scope, so there is no need to prefix the variable name in this case:

function wpse256370_acfvalue() {
    static $acfvalue;
    if( ! isset( $acfvalue ) ) {
        $acfvalue = get_field( 'short_title' );
    }
    return $acfvalue;
}

function hprice() {
    return '<h2>'. wpse256370_acfvalue() . " Price list in India" . '</h2>';
}
add_shortcode( 'pprice', 'hprice' );

function hspecs() {
    return '<h2>'. wpse256370_acfvalue() . " Full Specification" . '</h2>';
}
add_shortcode( 'pspecs', 'hspecs' );

function hreview() {
    return '<h2>'. wpse256370_acfvalue() . " Review" . '</h2>';
}
add_shortcode( 'preview', 'hreview' );

Using class:

The best way is to use a class, because by using a class you also avoid possible function name conflicts:

class WPSE_256370_Shortcode {
    private static $instance;
    private $acfvalue;

    public static function init() {
        self::getInstance();
        add_shortcode( 'pprice', array( self::$instance, 'pprice' ) );
        add_shortcode( 'pspecs', array( self::$instance, 'pspecs' ) );
        add_shortcode( 'preview', array( self::$instance, 'preview' ) );
    }

    public static function getInstance() {
        if( ! isset( self::$instance ) ) {
            self::$instance = new self;
        }
        return self::$instance;     
    }

    public function acfvalue() {
        if( ! isset( $this->acfvalue ) ) {
            $this->acfvalue = get_field( 'short_title' );
        }
        return $this->acfvalue;
    }

    public function pprice() {
        return '<h2>'. $this->acfvalue() . " Price list in India" . '</h2>';
    }

    public function pspecs() {
        return '<h2>'. $this->acfvalue() . " Full Specification" . '</h2>';
    }

    public function preview() {
        return '<h2>'. $this->acfvalue() . " Review" . '</h2>';
    }
}
WPSE_256370_Shortcode::init();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.