# Impossible to call wordpress function inside an include?

In my Plugin, I have an object 'Settings' (to display stuff in Wordpress settings).

Settings.php

<?php

class Settings {

public $menu_slug; public function __construct(){$this->menu_slug = 'settings-'.PLUGIN_DOMAIN ;

// Initialize the component
$this->init(); } protected function init(){ // output method add_action( '_output_content_submenu_page_' .$this->menu_slug, array( $this, 'html_page_template' ) ); } public function html_page_template(){ include_once 'views/view-settings.php'; } }  In 'view-settings.php', I can normally use all basic wordpress functions. view-settings.php <div class="wrap"> <h1 class="wp-heading-inline"> <?php _e('Settings for '.PLUGIN_TITLE, PLUGIN_DOMAIN); ?> </h1> <hr wp-header-end /> <h2 class="nav-tab-wrapper"> ... </h2> <?php foreach ($tabs as $tab ){ ?> <?php include_once$tab["path"]; ?>
<?php } ?>

</div>


But in my included file ( include_once $tab["path"]; ), I cannot use the wordpress function like __() or _e(). Included file <form id="form-settings-recaptach" method="post"> <table class="widefat fixed"> <tbody> <tr> <td> <label class="label"><?php _e( "Foo", PLUGIN_DOMAIN ); ?></label> </td> </tr> </tbody> </table> </form>  So I get this error : Fatal error: Uncaught Error: Call to undefined function _e() Notice : if I include the child php file directly inside Settings.php, the functions are called. So It's a real problem of include inside an iclude and levels for Wordpress Why ? How can I debug this ? • The _e('Settings for '.PLUGIN_TITLE, PLUGIN_DOMAIN); working in the view-settings.php? another way, use require_once for include your file. Sep 25 '17 at 10:43 • where is called the action _output_content_submenu_page_... ? – mmm Sep 25 '17 at 10:47 • Yes, the _e('Settings for '.PLUGIN_TITLE, PLUGIN_DOMAIN) works. Ok, I will try with require_once. For the action '_output_content_submenu_page_', it's called in an other object which make the submenu page. Sep 25 '17 at 10:58 • Same problem with require_once. Sep 25 '17 at 11:00 ## 2 Answers I've had this problem in past try using include with locate_template: I don't exactly remember the reason but this code still works: Example: include(locate_template(YOUR_TEMPLATE_PATH)); Here's a complete demonstration too, using the same idea, if you need more help. For Admin Panel include simply works in admin panel. You might want to modify your code like: public function init(){ add_menu_page( 'Your Plugin Name' , 'Your Plugin Settings' , 'manage_options' , 'your_plugin_settings' , array($this, 'settings_page' ) );
}
public function html_page_template(){
ob_start();
include('views/view-settings.php');
$html = ob_get_clean(); echo$html;
}


If the init function is calling your custom hook, then please don't modify it and make sure that code is working properly. Your html_page_template function should now output the form.

• locate_template => "Retrieve the name of the highest priority template file that exists.". I am in admin > Settings > My PLugin Name, I do not use any template like 'page.php' at this location. So unfortunately, that does not work. I have any response from this function :/ Sep 25 '17 at 11:42
• I see that you're not using 'echo' to print the markup. Would you please post the updated code and the function hook '_output_content_submenu_page_' . $this->menu_slug. If still you think it's not helping then please check this template. Though I've tested my code and it works. There must be some error in other files which you haven't posted. Sep 25 '17 at 16:55 I found the problem and I have the solution : To include the child file, as I'm working on localhost I changed the path : My error :  plugin_dir_path( __FILE__ ).'my-child-template.php' //<-- \path\to\file/my-child-template.php //Changed by plugin_dir_url( __FILE__ ).'my-child-template.php' //<-- http://localhost/wordpress/wp-content/plugins/my-plugin/path/to/file/my-child-template.php  With the second path, it worked on localhost, it managed to include the template but impossible to call worpdress functions. So, I had to find a solution to include the path file instead of the url but with a localhost compatibility. The solution is : public function html_page_template(){ ob_start();$ipAddress = gethostbyname($_SERVER['SERVER_NAME']); if($ipAddress === "127.0.0.1"){
$base_path = dirname( __FILE__ )."\\templates\\"; }else{$base_path = plugin_dir_path( __FILE__ )."templates/";
}
$a_settings_tab = array( 0 => array( "key" => "foo", "path" =>$base_path ."foo.php"
),
1 => array(
"key" => "bar",
"path" =>  $base_path ."bar.php" ) ); include_once 'views/view-settings.php';$html = ob_get_clean();
echo \$html;
}

• sorry, but this doesn't make much sense, And if you are actually including a url, it is totally wrong. Sep 25 '17 at 15:50
• Yes, that was wrong to include an url, I had to include the path file. It's exactly I'm explaining in this solution. Sep 25 '17 at 16:58
• ok, so the problem was with your question, still trying to detect on which server the code run is wrong by itself Sep 25 '17 at 17:05
• How is someone supposed to know where the error was with a question like that? Oct 6 '17 at 20:22