0

I'm trying to build my first plugin and building my plugin menu like this:

function group_panel(){
    include('html/example.html');   
    echo "<h1>{$_POST['data']}";
}

function groups_add_menu(){
    if (function_exists('add_menu_page')){
        add_menu_page('Group','Group',8,basename(__FILE__),'group_panel',plugins_url('/img/icon.png',__FILE__));
        add_submenu_page(basename(__FILE__),'Example','Example',8,'red.php');       
    }
}

And my folders are like this

I've the file red.php inside of /plugins/my-plugin/red.php but if I press in my "Example" menu, WordPress send my to the index but the first option ("Group") works.

How can put the URL for my menu?

1 Answer 1

0

You can not load external file in this way. This argument is for menu slug. If you load the external file it is useless, there will be nothing just except what you will print in file also there will be no WordPress functions at all (because WordPress is not loaded).

You can define a callback function for this then include your red.php in that function.

Firs define a constant for your plugin path in main file (where plugin header is defined) so you can use it later as well.

define( 'MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );

Then add submenu page with slug and callback function

add_submenu_page(basename(__FILE__),'Example','Example', 'manage_options', 'my-plugin-page-slug', 'include_plugin_file');

NOTE- User levels (from 1 to 10) are deprecated use capabilities instead. See Roles and Capabilities

Then in your callback function use the path constant and include the file

function include_plugin_file() {
    include_once MY_PLUGIN_PATH . 'red.php';
}
1
  • 1
    Thanks for the example, I'm try and finally it works! thanks!
    – Marcos
    May 17, 2016 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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