Wp activate php error

Php on activate plugin hook wordpress

Solution 1: You should use the activation hook for plugins in order to make any actions upon the plugin activation. I discovered the problem, but didn’t understand it: I’m trying to check for plugins activation (others and my own) but inside an WordPress hook ( , for instance)

Advanced WordPress plugin activation detection

I’m testing the plugin activation detection because something went wrong in my plugin. I discovered the problem, but didn’t understand it:

I’m trying to check for plugins activation (others and my own) but inside an WordPress hook ( init , for instance)

In the first example i will listen to the «Hello Dolly» plugin activation:

add_action('init', function()< register_activation_hook('hello.php', function()< echo 'Hello was activated.'; >); do_action('wordpress_initialized'); >); add_action('wordpress_initialized', function()< register_activation_hook('hello.php', function()< echo 'Hello is not allowed.'; die; >); >); 
  1. listening to the init hook
  2. Inside it, listening to the hello.php plugin activation to say it was activated.
  3. Inside the init hook, triggering an custom hook to say wordpress is loaded
  4. Outside, i wait for this another custom hook and then listen to the hello activation
  5. When activated, i block the activation saying it’s not allowed.
Читайте также:  Unit testing with python

This is an example, and it works, when the plugin i’m listening is another plugin.

The problem: If i apply this code to listen MY plugin, it will not work. (content below is inside myplugin.php )

add_action('init', function()< register_activation_hook('myplugin.php', function()< echo 'myplugin was activated.'; >); do_action('wordpress_initialized'); >); add_action('wordpress_initialized', function()< register_activation_hook('myplugin.php', function()< echo 'myplugin is not allowed.'; die; >); >); 

And this happens because, i wait until the init hook to register the activation hook . If i register the activation hook outside any hook, it will work.

If i trigger an custom action inside the activation hook, this action can be listen.

register_activation_hook('myplugin.php', function()< echo 'myplugin was activated.'; do_action('myplugin_activate'); >); add_action('myplugin_activate', function()< echo 'myplugin is not allowed.'; die; >); 

So there is the problem and there is the solution, but what i don’t know is WHY, why i can listen to other plugins activation, but can’t listen to my own this way? It’s because the activation process? I read about this in the docs and an redirection happens, how this affect the working flow?

The last example isn’t exactly an solution. What i need is add an listener to my own plugin inside the init hook like this:

The init and plugins_loaded hooks are already run before a plugin is activated. That’s why your first code doesn’t work but the second does.

Regarding your third code: there’s no need to run add_action(‘myplugin_activate’ … inside init . Not everything needs to be hooked to init . Just use

add_action('myplugin_activate', function()< echo 'myplugin is not allowed.'; die; >); 

without any init stuff. Although I probably wouldn’t exactly use die 🙂

Check out the Codex article on register_activation_hook() which has many valuable examples.

WordPress — Add a page upon activation of plugin

I am currently making a WordPress plugin, but I couldn’t find an answer to this.

How do you add a page upon activation of the plugin?

I have added posts upon activation earlier with the wp_insert_post function, but I can’t find a way to insert a page.

You should use the activation hook for plugins in order to make any actions upon the plugin activation.

register_activation_hook( __FILE__, 'activation_hook_callback'); function activation_hook_callback() < //add the post type and other options in the array for the query $page = array( 'post_status' =>'publish' , 'post_title' => 'Page name', 'post_type' => 'page', ); //add the page and ID will be saved. $the_page_itself = wp_insert_post( $page ); > 

I solved it. WordPress has several post-types :

Post (Post Type: 'post') Page (Post Type: 'page') Attachment (Post Type: 'attachment') Revision (Post Type: 'revision') Navigation menu (Post Type: 'nav_menu_item') 

To add a post upon activation of my plugin:

function add_page_upon_activation() < $arr = array( 'post_title' =>'title', 'post_name' => 'slug', 'post_status' => 'publish', 'post_type' => 'page', 'post_content' => 'yes, a nice page', ); wp_insert_post($arr); > add_action( 'activated_plugin', 'add_page_upon_activation' ); 

Plugin activation hook not working in wordpress, If calling a function from a file that is outside of main plugin file, then the hook will not work as it is not pointing to the right file. FILE will point to

WordPress Error when Activating Plugin

I was try many answred question here. Like remove space before

not working at all Iam still stucked..

add_action( 'admin_notices', 'fii' ); function fii()< // parent plugin if ( ! is_plugin_active( 'xxx/aaa.php' ) and current_user_can( 'activate_plugins' ) ) < echo '

You need install xxxx plugin

'; > > register_activation_hook( __FILE__, 'fii' );

The problem is when active my plugin, wordpress give me error info like this..

«The plugin generated 281 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.»

But when i close my echo inside fii function, its fine»

 add_action( 'admin_notices', 'fii' ); function fii()< // parent plugin if ( ! is_plugin_active( 'xxx/aaa.php' ) and current_user_can( 'activate_plugins' ) ) < //echo '

You need install xxxx plugin

'; > > register_activation_hook( __FILE__, 'fii');

My guess is that since you are calling this plugin both on the activation hook and as admin notice s, that as soon as the plugin is activated the admin_notices hook is called (well in the same cycle). As a result your plugin is throwing out the echo at a point during the activation process. As such this is unexpected behaviour since an activation isnt expecting to echo out anything.

I also don’t see any need for this function to run on activation. Only call it on admin_notices and if necessary adapt your logic in the function so that this notice only shows once the plugin has been activated.

Additionally, at present your code isn’t taking account of whether or not the 3rd party plugin is active or even installed. So, users would get that message even if they had this other plugin installed and activated.

There is already a great library for adding required and recommended plugins to WordPress installation called TGM Plugin Activation. I’d strongly recommend that you use that instead of creating your own logic.

The most common causes are:

  1. A white space before or after the PHP opening or closing tags
  2. A file encoded in UTF-8
  3. Another issue when something is called at the wrong time, or a call that cannot be resolved without intervention
  4. Using the wordpress add_option function. Switching to update_option instead can resolve the problem.

I believe it might be the UTF-8 encoding try changing it to ANSI.

Catch event of activation of the plugin, Solved. The problem was that one can’t call register_activation_hook() inside a function hooked to the ‘init’.

In a WordPress plugin, why doesn’t `register_rest_route()` work when called in the activation hook?

I have this (example) plugin:

 function my_great_plugin_init() < add_action( 'rest_api_init', function() < register_rest_route( 'great-plugin/v1', '/hello', array( 'methods' =>'GET', 'callback' => 'hello', ) ); > ); > register_activation_hook( __FILE__, 'my_great_plugin_init'); ?> 

When I activate this plugin, the /wp-json/great-plugin/v1/hello route does not exist. However, if I move the add_action call to the top level, like so:

 add_action( 'rest_api_init', function() < register_rest_route( 'great-plugin/v1', '/hello', array( 'methods' =>'GET', 'callback' => 'hello', ) ); > ); ?> 

then the /wp-json/great-plugin/v1/hello route exists and responds to GET requests with «Hello, world!» . Why doesn’t the endpoint get registered when the registration happens during the activation hook?

if you check the WordPress Codex your will find that register_activation_hook run only when the plugin is activated

When a plugin is activated, the action ‘activate_PLUGINNAME’ hook is called.

Using register_rest_route inside that hook will not work because register_rest_route is called when the rest_api_init as mentioned in WP REST API Docs

We do this through a function called register_rest_route , which should be called in a callback on rest_api_init to avoid doing extra work when the API isn’t loaded.

so basically when you put rest_api_init inside register_activation_hook that will not triggered after the plugin is activated.

in another word when the rest_api_init is initiated it will not detect your hook

Actions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur. Plugins can specify that one or more of its PHP functions are executed at these points, using the Action API.

If you need an more explanation let me know.

How to run an activation function when plugin is network activated, function my_plugin_new_blog($blog_id) < //replace with your base plugin path E.g. dirname/filename.php if ( is_plugin_active_for_network( 'my-

Источник

WordPress Problem: Can Not Activate a Plugin

A client came to me recently with a broken site problem they simply couldn’t fix. They had de-activated a plugin that was so heavily ingrained into the site that all site functionality had ceased to work, meaning they could not access the site via the Dashboard (domain.com/wp-admin) to reactivate it.

Fortunately, there are a few options available to you in this instance.

1. Attempt to reactivate using functions.php

The first thing you may try is adding a function to your theme’s functions.php via FTP.

// Add this code to your theme's functions.php function activate_plugin_manually() < $active_plugins = get_option( 'active_plugins' ); array_push($active_plugins, 'plugin-directory/filename.php'); /* Replace the plugin-directory and filename.php to match your main plugin file */ update_option( 'active_plugins', $active_plugins ); >add_action( 'init', 'activate_plugin_manually' );

2. Add your plugin to the WordPress “Must Use” folder.

Many people don’t realise that the mu-plugins folder is not a multi-user or multi-site folder, and in fact a “Must Use” folder. Plugins in this folder are loaded before all other plugins, so the simple fix in this case is to move the plugin out of your plugins folder and into wp-content/mu-plugins. If this folder doesn’t exist, create it with the permissions 755 and all files 644.

3. Activate your plugin via PHPMyAdmin

This method is by far the most complicated since you will need to find the name and value in your wp_options table of your database.

Locate the row with the option_name of “active_plugins” and you will see the value looks something like the following:

1. The initial a:4 value has been increased to a:5 (the number of plugins)
2. The i:4 value must be increased from the previous row (the order in which the plugins load)
3. The s:39 is the number of characters in that filename string (count the number of characters in the line)

Hopefully, one of these methods will solve your problem.

Источник

Оцените статью