Forums

Take 2 Minutes And Help Me Out (4 posts)

  1. Seans0n
    Member
    Posted 3 years ago #

    So yesterday I read through the WordPress 'How to create a plugin' tutorials (http://codex.wordpress.org/Writing_a_Plugin) as well as some of the external links.

    It was really useful and I understand how everything works, but I'm having trouble with the activation hook, ie the one which launches when a plugin is activated for the first time. I have a single PHP file at wp-content/plugins/wp-dux/dux.php and my code looks like this:

    <?php
    /*
    Plugin Name: Dux0r Test Plugin
    Version: 1.0
    Plugin URI: http://www.seanbluestone.com
    Author: Dux0r
    Author URI: http://www.seanbluestone.com
    Description: Testing plugin creation
    */
    
    add_action('activate_wp-dux/dux.php', 'wpdux_init');
    
    function wpdux_testfunction(){
    	global $id;
    
    	echo 'It appears to be working captain.';
    }
    
    function wpdux_init(){
    	echo 'I work.';
    	add_management_page('Dux0r', 'Dux0r', 8, __FILE__, 'wpdux_adminpage');
    }
    
    function wpdux_adminpage(){
    	echo 'I am some content on your admin page.<br />';
    }
    
    ?>

    When I activate the script, for some reason it doesn't run the wpdux_init() function, and I don't know why. I played around with adding other hooks, and they seem to work fine, but the activate hook just doesn't seem to work or I'm doing something wrong. I've also tried using
    register_activation_hook(__FILE__, 'wpdux_init'); but no joy there either.

    What am I doing wrong?

  2. Otto
    Tech Ninja
    Posted 3 years ago #

    I've also tried using
    register_activation_hook(__FILE__, 'wpdux_init'); but no joy there either.

    That code is the correct way to do it. The "add_action" you're using is not.

    However, I notice that you're adding the management page in your activation function. That's incorrect, as that means that the management page will only be added on the one hit immediately after activation.

    Activation happens ONCE. If you want a management page, you have to add it every time. Move the call to add_management_page outside of the init function.

  3. Seans0n
    Member
    Posted 3 years ago #

    When I moved the add_management_page outside of the init function the script returned:

    Fatal error: Call to undefined function add_management_page() in C:\WebServ\wwwroot\htdocs\wordpress\wp-content\plugins\wp-dux\dux.php on line 14

    So instead I added this code:

    add_action('admin_head', 'wpdux_menu');
    
    function wpdux_menu(){
    	add_management_page('Dux0r', 'Dux0r', 8, __FILE__, 'wpdux_adminpage');
    }

    and it worked fine. Thank you =)

    I also replaced the activation add_action(); with register_activation_hook(__FILE__, 'wpdux_init'); again, but it still refuses to run and display the echo. Any thoughts?

  4. Otto
    Tech Ninja
    Posted 3 years ago #

    An echo like that won't exactly be displayed on your admin page. After a plugin activates, the page is redirected back to the plugins.php page. So you'll never see any output there because it redirects.

    The function is running, you just can't make it produce output.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.