0

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

Settings.php

<?php

namespace FooNamespace\Admin\Settings;

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 ?

4
  • The _e('Settings for '.PLUGIN_TITLE, PLUGIN_DOMAIN); working in the view-settings.php? another way, use require_once for include your file. – Mostafa Soufi 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. – J.BizMai Sep 25 '17 at 10:58
  • Same problem with require_once. – J.BizMai Sep 25 '17 at 11:00
0

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.

3
  • 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 :/ – J.BizMai Sep 25 '17 at 11:42
  • Thank you for your answer about Admin panel, but it's already like this ^^ – J.BizMai Sep 25 '17 at 14:55
  • 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. – Akash K. Sep 25 '17 at 16:55
-1

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;
}
4
  • sorry, but this doesn't make much sense, And if you are actually including a url, it is totally wrong. – Mark Kaplun 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. – J.BizMai 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 – Mark Kaplun Sep 25 '17 at 17:05
  • How is someone supposed to know where the error was with a question like that? – Akash K. Oct 6 '17 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.