Home > Net >  add_menu_page() | Function from another Class and file
add_menu_page() | Function from another Class and file

Time:11-30

I'm trying to call functions from another PHP file inside a Class. The function is to create a menu. I can't find an answer to what's wrong. I don't get any error information.

I'm learning to create a Wordpress plugin.

I've tried many things, but each one ignores "add_action".

1file.php

defined( 'ABSPATH' ) or die('Nie masz dostępu');


class PluginRzeczoznawcy{
    public function activate(){
        if ( is_admin() )
        {
            require_once( 'admin/admin.php' );
            add_action('admin_menu', ['Mango\Admin', 'adminMenu'] );
        }
    }
    function deactivate(){
        //FLUSH
    }
    function uninstall(){
        //remove
    }

    function CPT_rzeczoznawcy(){
       
    }
}

if( class_exists( 'PluginRzeczoznawcy' )){
    $rzeczoznawcyPlugin = new PluginRzeczoznawcy();
}
// activation
register_activation_hook(__FILE__, array($rzeczoznawcyPlugin,'activate'));
// deactivation
register_deactivation_hook(__FILE__, array($rzeczoznawcyPlugin,'deactivate'));

2file.php

<?php namespace Mango;

class Admin
{
    public function adminMenu()
    {
     add_menu_page(
        'MenuTest',
        'MenuTest',
        'manage_options',
        'menutest-settings',
        array($this, 'settingsPage')
    );
    }

    public function settingsPage()
    {
        echo 'Test';
    }
}

CodePudding user response:

If you are going to create a class-based plugin then PHP classes have a constructor function, __construct, which is executed as soon as a new instance of a class is instantiated. All WordPress hooks and filters will be registered under the constructor of our plugin class.

For example...

if ( ! defined( 'ABSPATH' ) ) {
    // Exit if accessed directly.

    exit;
}

class PluginRzeczoznawcy {

    public function __construct() {
        /* Use admin_menu hook for adding custom admin menu */
        add_action( 'admin_menu', array( $this, 'custom_admin_menu' ) );

        /* Activation hook fires when plugin activate */
        register_activation_hook( __FILE__, array( $rzeczoznawcyPlugin, 'activate' ) );

        /* Deactivate hook fires when plugin deactivate */
        register_deactivation_hook( __FILE__, array( $rzeczoznawcyPlugin, 'deactivate' ) );
    }

    /**
     * Register admin menu page
     */
    public function custom_admin_menu() {
        if ( is_admin() ) {
            add_menu_page(
                'MenuTest',
                'MenuTest',
                'manage_options',
                'menutest-settings',
                array( $this, 'settingsPage' )
            );
        }
    }

    /**
     * Callback function for admin menu
     */
    public function settingsPage() {
        echo 'Test';
    }

    public function activate() {
        // Callback for activate.
    }

    public function deactivate() {
        // Callback for deactivate.
    }
}


if ( !class_exists( 'PluginRzeczoznawcy' ) ) {
    $rzeczoznawcyPlugin = new PluginRzeczoznawcy();
}

Also, refer to WordPress plugin documentation: https://developer.wordpress.org/plugins/intro/

  • Related