php - Access variables stored in Wordpress Plugin -
within wordpress plugin, storing variables:
$results = get_aws_details($reg, $asin); $price = $results[0][0]; $url = $results[1][0]; $wishlist = $results[2][0];
essentially, plugin grabbing field each post, , returning amazon data on it, being stored in variables above.
what need though access these variables in loop on theme.
i've had working when hardcoded theme, not know how access these variables.
simply trying add echo $price in loop doesn't work
any suggestions? know use global variables, isn't ideal
you use custom wordpress hook. here example:
<?php // loop while (have_posts()): the_post(); ?> <div> <?= apply_filters("get_my_price"); ?> </div> <?php endwhile; wp_reset_postdata(); ?>
in plugin now:
<?php // plugin $results = get_aws_details($reg, $asin); $price = $results[0][0]; add_filter("get_my_price", function() use ($price) { return $price; });
note: style of coding can change want be. wanted demonstrate how can accomplish want without using global state. you'll need add filter rest of data. alternatively can use 1 filter use switch match data want.
<?php // theme echo apply_filters("get_result", "price"); // plugin $results = get_aws_details($reg, $asin); add_filter("get_result", function($type) use($results) { switch ($type) { case "price": return $results[0][0]; } });
Comments
Post a Comment