如何使用会话/函数或JavaScript插件PHP在WordPress的另一个PHP文件传递变量?(

2019-11-01 23:48发布

我有oneplugin文件testplugin.php..It包含变量$ ABC;

//主plugin.php

if(!is_admin()){
new Funtion_Button();
}
class Function_Button
{

if(is_single() || is_page() || is_home() ){  
      global $post; 
      global $wpdb; 

 $query_images_args = array(
     'post_type' => 'attachment' , 'post_mime_type' =>'image','post_status' => 'published', 'posts_per_page' => -1,'numberposts' => 1

$query_images = new WP_Query( $query_images_args );
         $images = array();
         foreach ( $query_images->posts as $image) {
         $images[]= wp_get_attachment_url( $image->ID );

         }
                 $abc[]=0;
         $abc= $abc.http_build_query($images);
                 $_SESSION['arrayImg']=$abc;
 );
}

/// recieving文件

include ('testplugin.php'); 
session_start();
$array1[]=$abc;

现在这个$ ABC是主要的插件页面

但我得到这个错误

致命错误:调用未定义的函数is_admin()在C:\瓦帕\ WWW \ WordPress的\可湿性粉剂内容\插件\上线98 testplugin.php

在98行,我有if(!is_admin()){

Answer 1:

使用会话:

//在testplugin.php

session_start();
$_SESSION['varname'] = $var_value;

//在另一个文件

session_start();
$var_value = $_SESSION['varname'];

使用Cookie:

//在testplugin.php

$_COOKIE['varname'] = $var_value;

//在第2页

$var_value = $_COOKIE['varname'];


文章来源: how to pass variable from plugin php to another php file in wordpress using session/function or javascript?