how to pass parameter in wordpress plugin

落爺英雄遲暮 提交于 2019-12-12 06:06:57

问题


I am trying to pass argument in function and print the output in wordpress plugin i was not able to pass parameter here's my code

global $postidd;
$postidd=$_REQUEST["postid"];
function getcontent($postidd)
{
   // do something with the args


if($postidd)
{

      $args12 = array(
                    'p'=>15,
                    'post_type' => 'offers',
                    'orderby' => 'title',
                    'order' => 'ASC'
                );
      $the_query12 = new WP_Query( $args12 );           
      if ( $the_query12->have_posts() ) : while ( $the_query12->have_posts() ) : $the_query12->the_post(); 

     return   $postidd;

      endwhile;endif;     

         return   $postidd;

}
}

回答1:


If you want any variable outside your function available to your function then use closures by using 'use' keyword like this:

$postidd=$_REQUEST["postid"];
function getcontent($somevariable) use ($postidd) {
....

or 
declare it global inside the function like this:
function getcontent($somevariable) {
global $postidd


but using global is considered a bad practice


来源:https://stackoverflow.com/questions/34367209/how-to-pass-parameter-in-wordpress-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!