Support » Fixing WordPress » call_user_func_array() expects parameter 1 to be valid.. Custom Function

  • Resolved Devin Blewitt

    (@devinblewitt)


    Full Warning Message:
    mod_fcgid: stderr: PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘course’ not found or invalid function name in /var/www/vhosts/itonlinelearning.com/httpdocs/wp-includes/plugin.php on line 525, referer: http://www.itonlinelearning.com/landing/xxxx-xxxxx/

    We have 2 custom post types, ‘course’ and ‘landing’ which have been pushing out the above error. We still use these post types so am being super careful of any changes.

    After reading a few threads it seems this may be the code that needs amending:

    add_action( 'admin_init', 'course' );
    register_post_type( 'course',
        array(
            'labels' => array(
                'name' => __( 'Courses' ),
                'singular_name' => __( 'Courses' )
            ),
            'public' => true,
            'has_archive' => true,
            'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail'),
        )
    );

    This code snippet is from our course_taxonomy.php file found in /admin/, the function.php file imports the course_taxonomy.php file.

    Any feedback would be greatly appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Hi Devin!

    Hope you’re having a good day!

    The error message is letting you what is wrong:

    PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘course’ not found or invalid function name

    I did add some emphasis to the part that matters most. What that means is that the function course is not defined anywhere. Part of that reason is because the function call_use_func_array() is looking for this function to run. The reason this function is looking for that is because when you add a filter or an action you are trying to link a function to that.

    So, in this case admin_init is looking for a function called course and because it cannot find it, it gives that error.

    Generally you want to avoid registering shortcodes, post types, or taxonomies in a theme. The best way is using a plugin so it won’t be lost when the theme is switched.

    To give a code example, if you wanted to do the above you would do something like:

    add_action( 'admin_init', 'jc_reg_posttype' );
    function jc_reg_posttype() {
      // run my needed code here
    }

    Hope that helps!

    Thanks Jose for the feedback.

    I completely understand, but want to make changes to the code to stop the errors. So, would you suggest that I empty the function to;

    add_action( 'admin_init', '' );
    register_post_type( 'course',
        array(
            'labels' => array(
                'name' => __( 'Courses' ),
                'singular_name' => __( 'Courses' )
            ),
            'public' => true,
            'has_archive' => true,
            'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail'),
        )
    );

    If because the call can’t see the function, can I confirm that no code is running from that function. If that is the case, removing the function call syntax would have no effect on the rest of the theme as it is already not being used.

    Or do you have other ideas?

    thanks again for your help.

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Happy to help Devin!

    So, would you suggest that I empty the function to;

    Not sure if that will work seeing how you are not passing it a callback function. The second part needs to be a valid function that you create in order to register your post type.

    I’ll leave you with a few helpful links:

    https://codex.wordpress.org/Function_Reference/register_post_type
    https://developer.wordpress.org/reference/functions/add_action/
    http://docs.presscustomizr.com/article/26-wordpress-actions-filters-and-hooks-a-guide-for-non-developers

    OK, I think I have it.

    I will need to create a function in the function.php file that will register the post type.

    This should resolve my error issue, correct ?

    Thanks.

    Adding the function worked !! Thanks Jose

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘call_user_func_array() expects parameter 1 to be valid.. Custom Function’ is closed to new replies.