freaky
freaky

Reputation: 1010

php include require file

I have a php file that I need to load in order to declare some array:

main 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')
);

Then in some other files I need to use $shortcodes to get some values from array.

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'];
}

I don't understand how to correctly load my php file then to use my array value after. In fact If I load my shortcodes-config.php more than one time it's not working...

I can make it works only one times with file1 or file2.

What is the correct way to include a php file to declare some variable than to get it after?

Upvotes: 0

Views: 117

Answers (1)

meda
meda

Reputation: 45490

Because you have initialy used require_once in main conf-file.php you are not allowed to require it again:

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

Solution is to use require or include

Upvotes: 1

Related Questions