How to insert a REQUIRE in this function?

Asked

Viewed 84 times

1

How do I insert a require in this Wordpress function for menu in administration?

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:

Screenshot

  • 4

    You have been here for a long time, please take a look at these links http://meta.pt.stackoverflow.com/questions/1084/como-devemos-formatr-questions-e-respostas?lq=1 and http://meta.pt.stackoverflow.com/questions/297/quando-se-deve-colocao-name-nameg_no-t%C3%adtulo

2 answers

4

Complementing Bruno Augusto’s response. It is not recommended to use global, but it is also not the end of the world (the WP itself is full of them), so in the plugin 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 it with OOP, here is an example. With that, inside the file included you would use:

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

In advance, you will certainly need to load JS and CSS on 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' );  
}
  • 1

    The WP is so full of global that one time I saw this failure of its design to fit an MVC with URL routing inside its Administrative Panel. XD

2

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

You chose a Closure, an internal object representing an anonymous function. When the item added to the menu is clicked, this invocable argument, if defined, 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, just include/require is done within this anonymous function:

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 of your problems, here, is not even where to invoke include/require and find the file. For this get_template_directory() can help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.