• Resolved chcw

    (@chcw)


    Hi,

    To prevent name collision, I want to use namespace instead of adding prefix to each function:

    Below is my code:

    <?php
    
    /*
     * Plugin Name: My Test Plugin
     */
    
    
    namespace dnip_add_image_links;
    
    if (!defined('ABSPATH')) 
    	exit; // Exit if accessed directly
    
    add_action('admin_post_dnip_add_image_links', 'dnip_add_image_links');
    add_action('admin_post_nopriv_dnip_add_image_links', 'dnip_add_image_links');
    
    function dnip_add_image_links() 
    {
    }

    But when calling with the following URL:

    http://127.0.0.2/wp-admin/admin-post.php?action=dnip_add_image_links&nocache=123

    I get the following error:

    Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback, function "dnip_add_image_links" not found or invalid function name in C:\wamp64\www\blogs\wp-includes\class-wp-hook.php:324 Stack trace: #0 C:\wamp64\www\blogs\wp-includes\class-wp-hook.php(348): WP_Hook->apply_filters('', Array) #1 C:\wamp64\www\blogs\wp-includes\plugin.php(517): WP_Hook->do_action(Array) #2 C:\wamp64\www\blogs\wp-admin\admin-post.php(85): do_action('admin_post_dnip...') #3 {main} thrown in C:\wamp64\www\blogs\wp-includes\class-wp-hook.php on line 324
    There has been a critical error on this website. Please check your site admin email inbox for instructions.

    So, how to implement the plugin in the namespace?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Try this:

    add_action('admin_post_dnip_add_image_links', 'dnip_add_image_links\dnip_add_image_links');

    And better use a namespace matching your plugin-slug or text-domain.

    Thread Starter chcw

    (@chcw)

    Hi, @threadi

    Thank you very much. It works. I will change the namespace with a more appropriate name.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to call plugin function in namespace?’ is closed to new replies.