PHP包含require文件


php include require file

我需要加载一个php文件来声明一些数组:

主要conf-file.php

/* Defined main names, php files and action */
function __construct() {
    define( 'TINYMCE_DIR', plugin_dir_path( __FILE__ ) .'tinymce' );
    define( 'VISUAL_DIR', plugin_dir_path( __FILE__ ) .'visual' );  
    include( TINYMCE_DIR . '/shortcodes-config.php' );
    require_once( TINYMCE_DIR . '/shortcodes-popup.php' );
    require_once( VISUAL_DIR . '/visual.php' );   
}

shortcodes-config.php:

$shortcodes['video_section'] = array(
    'no_preview' => true,
    'params' => 'xxx',
    'shortcode' => '[sc1][/sc1]',
    'popup_title' => __('Video Section', THEME_NAME),
    'shortcode_icon' => __('li_video')
);
$shortcodes['image_section'] = array(
    'no_preview' => true,
    'params' => 'yyy',
    'shortcode' => '[sc2][/sc2]',
    'popup_title' => __('Image Section', THEME_NAME),
    'shortcode_icon' => __('li_image')
);

然后在其他一些文件中,我需要使用$shortcodes从数组中获取一些值。

visual.php:

require( TINYMCE_DIR . '/shortcodes-config.php' );// if don't requiered it's no working?
$icon  = $shortcodes['video_section']['shortcode_icon'];
$title = $shortcodes['video_section']['popup_title'];
$icon  = "<i class='fa ". $icon ."'></i>";
$icon .= "<span class='element-title'>". $title ."</span>";
return $icon;

shortcodes-popup.php:

add_action('wp_ajax_display', 'display_shortcode_list_callback');
function display_shortcode_list_callback() {
  require( TINYMCE_DIR . '/shortcodes-config.php' );
  $title = $shortcodes['video_section']['popup_title'];
}

我不明白如何正确加载我的php文件,然后使用我的数组值之后。事实上,如果我加载我的shortcode -config.php不止一次,它就不能工作了…

我可以让它只工作一次file1或file2。

什么是正确的方式来包括一个php文件来声明一些变量,而不是得到它之后?

由于您最初在main conf-file.php中使用了require_once,因此不允许再次要求它:

require_once( TINYMCE_DIR . '/shortcodes-popup.php' );

解决方案是使用requireinclude