Skip to main content
added 1 character in body
Source Link
dingo_d
  • 1.9k
  • 1
  • 25
  • 45

I decided to namespace my plugin, and I got it all to work, except in my main plugin file.

I added two actions in that plugin, before calling the init method of the plugin.

<?php
namespace myPlugin;

add_action( 'admin_init', 'add_activation_notice' );

function add_activation_notice() {
    add_action( 'admin_notice''admin_notices', 'activation_notice' );
}

function activation_notice() {
    echo 'Notice!';
}

But this throws error

Uncaught Error: Undefined constant 'myPlugin\myPlugin\add_activation_notice'

I'm rather new to namespacing, so I don't know why this happens.

The two functions add_activation_notice and activation_notice are in the myPlugin namespace. Most of the code I found online reference methods inside classes, but I need these to be outside of a class so that I can have plugin activation notice.

EDIT:

When I try with

add_action( 'admin_init', \add_activation_notice() );

I get

Fatal error: Uncaught Error: Call to undefined function add_activation_notice()

I decided to namespace my plugin, and I got it all to work, except in my main plugin file.

I added two actions in that plugin, before calling the init method of the plugin.

<?php
namespace myPlugin;

add_action( 'admin_init', 'add_activation_notice' );

function add_activation_notice() {
    add_action( 'admin_notice', 'activation_notice' );
}

function activation_notice() {
    echo 'Notice!';
}

But this throws error

Uncaught Error: Undefined constant 'myPlugin\myPlugin\add_activation_notice'

I'm rather new to namespacing, so I don't know why this happens.

The two functions add_activation_notice and activation_notice are in the myPlugin namespace. Most of the code I found online reference methods inside classes, but I need these to be outside of a class so that I can have plugin activation notice.

EDIT:

When I try with

add_action( 'admin_init', \add_activation_notice() );

I get

Fatal error: Uncaught Error: Call to undefined function add_activation_notice()

I decided to namespace my plugin, and I got it all to work, except in my main plugin file.

I added two actions in that plugin, before calling the init method of the plugin.

<?php
namespace myPlugin;

add_action( 'admin_init', 'add_activation_notice' );

function add_activation_notice() {
    add_action( 'admin_notices', 'activation_notice' );
}

function activation_notice() {
    echo 'Notice!';
}

But this throws error

Uncaught Error: Undefined constant 'myPlugin\myPlugin\add_activation_notice'

I'm rather new to namespacing, so I don't know why this happens.

The two functions add_activation_notice and activation_notice are in the myPlugin namespace. Most of the code I found online reference methods inside classes, but I need these to be outside of a class so that I can have plugin activation notice.

EDIT:

When I try with

add_action( 'admin_init', \add_activation_notice() );

I get

Fatal error: Uncaught Error: Call to undefined function add_activation_notice()

Source Link
dingo_d
  • 1.9k
  • 1
  • 25
  • 45

add_action in namespace not working

I decided to namespace my plugin, and I got it all to work, except in my main plugin file.

I added two actions in that plugin, before calling the init method of the plugin.

<?php
namespace myPlugin;

add_action( 'admin_init', 'add_activation_notice' );

function add_activation_notice() {
    add_action( 'admin_notice', 'activation_notice' );
}

function activation_notice() {
    echo 'Notice!';
}

But this throws error

Uncaught Error: Undefined constant 'myPlugin\myPlugin\add_activation_notice'

I'm rather new to namespacing, so I don't know why this happens.

The two functions add_activation_notice and activation_notice are in the myPlugin namespace. Most of the code I found online reference methods inside classes, but I need these to be outside of a class so that I can have plugin activation notice.

EDIT:

When I try with

add_action( 'admin_init', \add_activation_notice() );

I get

Fatal error: Uncaught Error: Call to undefined function add_activation_notice()