How to insert a REQUIRE in this function?

1

How do I insert a require into this Wordpress function for admin menu?

add_action('admin_menu', function() {
    # $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null
    add_menu_page (
        'Gerência de dados',
        'Gerência',
        'manage_options',
        'gerencia_de_dados',
        function(){ 
            echo '<h2>Sistema de gerenciamento de traduções</h2>';
            echo '<form><input type="text"></form>'; 
        },
        'dashicons-welcome-learn-more',
        6
    );
});

The current result for learning purposes is this:

    
asked by anonymous 02.10.2014 / 13:34

2 answers

4

Complementing Bruno Augusto's answer. It is not recommended to use globals, but it is not the end of the world either (the WP itself is full of them), then the plugin's main file:

$my_plugin_url    = plugins_url( '/', __FILE__ );
$my_plugin_path   = plugin_dir_path( __FILE__ );

add_action('admin_menu', function() {
    add_menu_page (
        [...]
        function(){ 
            global $my_plugin_path;
            include $my_plugin_path . '/folder/file.php'; 
        },
        [...]
    );
});

And within file.php :

 global $my_plugin_url;
 $imagem = $my_plugin_url . 'img/file.png';

If you want to do with OOP, here is an example . With this, within the included file you would use:

$imagem = B5F_Nivo_Slider::get_instance()->plugin_url . 'img/file.png';

Already advancing, you will certainly need to load JS and CSS into your page:

add_action('admin_menu', function() {
    $hook = add_menu_page ( [...] );
    add_action( "admin_print_styles-$hook", 'admin_jscss_sopt_34639');
});

function admin_jscss_sopt_34639() {
    global $my_plugin_url;
    wp_enqueue_style( [...] );
    wp_enqueue_script( 'my-custom-js', $my_plugin_url . 'js/file.js' );  
}
    
02.10.2014 / 17:52
2

The fifth argument of add_menu_page () , although defined as a function, is actually a callable , that is, any function or class method invoked by PHP in the context of the WordPress API.

You have opted for a Closure , an internal object that represents an anonymous function. When the item added to the menu is clicked, this invokable argument, if set, will be executed.

This means that if the content to be displayed by this menu entry is in an external file, or other more complex things that would escape the scope of the question, it suffices to include / require within that anonymous function: / p>

add_action('admin_menu', function() {
    # $page_title, $menu_title, $capability, $menu_slug, $function = '', $icon_url = '', $position = null
    add_menu_page (
        'Gerência de dados',
        'Gerência',
        'manage_options',
        'gerencia_de_dados',
        function(){ 
            include 'path/to/file.php'; 
        },
        'dashicons-welcome-learn-more',
        6
    );
});

The biggest problem here is not even where to invoke include / require, but to find the file. To do this get_template_directory () can help.

    
02.10.2014 / 15:56