Forums

[resolved] Creating a plugin, getting Warning: call_user_func_array() expects parameter 1 (4 posts)

  1. tykho
    Member
    Posted 7 months ago #

    I'm trying to put together my own little plugin. Everything went fine and I decided I'd try breaking the code up into several different files. Bear with me as I'm fairly new to making plugins, I've only made very simple personal ones before.

    I started getting this error after dividing it into multiple files, is it something obvious I've missed? I looked at what other plugins did to see how it's done (or how I think it is).

    Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'create_fvmenu' not found or invalid function name in /Users/name/Sites/wordpress/wp-includes/plugin.php on line 405

    fvshop.php:

    <?php
    /*
    Plugin Name: FVshop
    Version: 0.1
    */
    
    define( 'FVSHOP_URL', WP_PLUGIN_URL.'/fvshop' );
    define( 'FVSHOP_DIR', WP_PLUGIN_URL.'/fvshop' );
    
    if (is_admin()) {
        require_once( FVSHOP_DIR . '/includes/fvshop-admin-core.php');
    }
    
    if (is_admin()) {
        add_action('admin_init', 'create_fvmenu');
    }

    /includes/fvshop-admin-core.php:

    <?php
    function create_fvmenu() {
        add_action('admin_menu', 'fvshop_menu');
    
        function fvshop_menu() {
    
            $args = array(
                'page_title'    =>  'FV Shop',
                'menu_title'    =>  'FV Shop',
                'capability'    =>  'manage_options',
                'menu_slug'     =>  'fv_shop',
                'function'      =>  '',
                'icon_url'      =>  '',
                'position'      =>  ''
            );
            add_menu_page('FV Shop', 'FV Shop', 'manage_options', 'fv_shop', 'fv_shop_options', '', '3');
    
            add_submenu_page( 'fv_shop', 'Options', 'Options', 'manage_options', 'opt', 'fv_shop_options');
        //    add_options_page('FV Shop Options', 'FV Shop', 'manage_options', 'fvshopbutik', 'fv_shop_options');
        }
    
        function fv_shop_options() {
            if (!current_user_can('manage_options')) {
                wp_die(__('You do not have sufficient permissions to access this page.') );
            }
            echo '<div class="wrap">';
            echo '<p>FVshoppens options.</p>';
            echo '</div>';
        }
    }

    Doesn't really matter what kind of function I put in the second file, I still get the same error.

    Could someone shine some light on what I'm doing wrong, or what it is that could be happening?

  2. Rebecca Mastey
    Member
    Posted 7 months ago #

    Why do you have functions within the main function? It almost looks like you're trying to create a class.

    Generally speaking, I try to keep all my controller functions in the main plugin file. Anything that generates a menu, adds a hook, etc, goes in that file. Everything else can be scattered around in whatever order makes the most sense to me.

    So, short answer: try separating out those two other functions and moving everything into the main file.

  3. tykho
    Member
    Posted 7 months ago #

    Thanks a lot for the reply.

    Yeah, I don't know why there's a function inside a function, got angry and tired, rewriting a lot of it over and over, trying to figure out what the problem was. Blaming it on that. :)

    I tried making all kinds of different "test" functions just to see if they worked, and I got the same error. It does load the fvshop-admin-core.php file, but still gives me that error about the functions. So if I keep those in the main file, I fear I'll still get the same error from the other ones.

  4. tykho
    Member
    Posted 7 months ago #

    Oh boy, look at this

    define( 'FVSHOP_URL', WP_PLUGIN_URL.'/fvshop' );
    define( 'FVSHOP_DIR', WP_PLUGIN_URL.'/fvshop' );

    Had WP_PLUGIN_URL on both *facepalm*

    Fixed that, now I don't get the error anymore.

Reply

You must log in to post.

About this Topic