• Resolved enriquerene

    (@enriquerene)


    Hi,

    I’m developing my first plugin. I created some code to test and verify how it works and now I’m putting all WP specifications. My question, what I can’t understand is: What does register_activation_hook() function really work for?

    Because if I don’t put this in my code the activation works well. If I throw just register_activation_hook( param, function_here ) directly, some errors appear (I’m debugging) in WP screen. I’d like, if possible, someone give me some real (as real as possible) and simple (as short as possible) example of this acti/deact_hook().

    I read codex but I think there is no good explanation about that.

    Thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • Mika

    (@blaueantilope)

    Hi enriquerene,

    if a user click the link to activate the plugin, the function on register_activation_hook() is fired.

    You can use this hook to add some stuff to your plugin (create your own database tables or insert some default values or something like that).

    Take care (described in the documentation) that after an activation a instant redirect is performed. Maybe a solution for your issue with your implementation of your hook…

    Thread Starter enriquerene

    (@enriquerene)

    ok. But I don’t understand how to use this in practice. I can’t use this when I’m coding, I don’t see any difference. I put some echos and JS alerts to tell me where and how this happens, but nothing happens.
    For example:

    define( ‘EASYDM__PLUGIN_URL’, plugin_dir_url( __FILE__ ) );

    function easydm_js_alert(){
    echo ‘<p>’.__FILE__.’
    ‘.__DIR__.'</p>’;
    }

    register_activation_hook( EASYDM__PLUGIN_URL, ‘easydm_js_alert’);

    This is in my ‘index’ plugin. Is it right? Is it working? How can I check these things?

    Moderator bcworkz

    (@bcworkz)

    Your browser isn’t expecting any content when this function is called, so simply echoing a message will have no effect. You need to do something that can be easily checked by alternate means, for example:
    error_log('Notice: Plugin Easy DM was activated');
    Then verify the entry is at the end of your error log file after you activate your plugin.

    Thread Starter enriquerene

    (@enriquerene)

    I tried and nothing happens :/
    I tried four ways:

    1)

    add_action( __FILE__, ‘easydm_activation’ );
    – – – – – – – –
    function easydm_activation_hook() {
    error_log(‘Notice: Plugin Easy DM was activated’);
    }

    function easydm_activation() {
    register_activation_hook( __FILE__, ‘easydm_activation_hook’);
    }

    2)

    register_activation_hook( __FILE__, ‘easydm_activation’);
    – – – – – – – – – – – –
    function easydm_activation() {
    error_log(‘Notice: Plugin Easy DM was activated’);
    }

    3)

    add_action( __FILE__, ‘easydm_activation’ );
    – – – – – – – – –
    function easydm_activation() {
    error_log(‘Notice: Plugin Easy DM was activated’);
    }

    4)

    add_action( ‘activate_’.__FILE__, ‘easydm_activation’ );
    – – – – – – – – – –
    function easydm_activation_hook() {
    error_log(‘Notice: Plugin Easy DM was activated’);
    }

    function easydm_activation() {
    register_activation_hook( __FILE__, ‘easydm_activation_hook’);
    }

    In none of them, nothing happens. I open my wp-content directory and error log isn’t there (I tried hidden files too).
    My wp-config has:

    define(‘WP_DEBUG’, true);
    define(‘WP_DEBUG_LOG’, true);
    define(‘SCRIPT_DEBUG’, true);
    define(‘SAVEQUERIES’, true);

    Moderator bcworkz

    (@bcworkz)

    It would be very unusual for the PHP error log to be in wp-content (or any public folder). Its location is defined in php.ini (which you may not have access to). Depending on your host configuration, you may not be able to get direct access. Sometimes the error log is not available until the following day, accessed through your cPanel. Other times it’s in a sub-folder of the parent of your public html. (with some hosts, you cannot access the parent)

    If you cannot easily access the error log, you could write data to a custom log file using normal PHP file functions. Another option is to use wp_mail() to e-mail yourself debug data.

    Thread Starter enriquerene

    (@enriquerene)

    Nice, you really help me to understand some stuff around this question. I’m doing some more tests but to my aim I got it.
    Your last answer assumes I’m developing in server, but I’m in localhost… after I’ll test in my own site and try publish to WP repositories.

    What I need that happens when someone activate my plugin is to create a directory to upload files. So in my main page of plugin I have

    define( ‘EASYDM_SETTINGS_PATH’, ‘settings/’ );

    require_once EASYDM_SETTINGS_PATH.’easydm-functions.php’;

    register_activation_hook( __FILE__, ‘easydm_activation’);

    Then inside my easydm_functions.php

    function easydm_activation() {
    $path = easydm_root_directory( EASYDM_PLUGIN_DIR );
    mkdir( $path, 0777, true );
    }

    Clearly my plugin prefix is easydm (Easy Downloader Manager) and mkdir is here “http://php.net/manual/pt_BR/function.mkdir.php&#8221;. (I’m saying this because some people who comes here may be novice in PHP too.)

    At the final of this discussion I’m still not sure why simple things like print_r() or echo don’t work. For me, it seems like register_activation_hook() just accept activate back-end stuff.

    Thanks for everything

    Thread Starter enriquerene

    (@enriquerene)

    I forgot at the above post…I’d like to share a video what helped me to solve this:

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘register_activation_hook’ is closed to new replies.