0

I know I am missing something simple. I am trying to use html to log to footer to learn wp. In theme functions.php:

function myLog() {
    echo 'test';    
}

In theme footer within the .siteInfo div:

<?php add_action( 'wpmu_new_blog', 'myLog' );?>

when a new user creates a new site myLog should output html 'test'?

Please help, save me many more hours of reading and trials.

2
  • sorry, the function within the footer is <?php add_action( 'wpmu_new_blog', 'myLog' );?>
    – Gordon
    Commented Dec 4, 2013 at 3:02
  • I was calling the function without the (). Should have been <?php add_action( 'wpmu_new_blog', myLog() );?> without the quotes and with the ().
    – Gordon
    Commented Dec 4, 2013 at 3:56

2 Answers 2

1

If you want something to display in the footer, you first need to hook it into the footer to display it. So that would look like this:

function myLog() {
    echo 'test';
}
add_action('wp_footer','myLog');

However, this is not at all connected to the data you want, which is when a new user creates a new site. The wpmu_new_blog action is deprecated so use the wp_initialize_site hook instead.

When this hook fires, it will not output information into the footer because it's processing information. So really, this step is just saving the information somewhere.

/**
 * Function for `wp_initialize_site` action-hook.
 * 
 * @param WP_Site $new_site New site object.
 * @param array   $args     Arguments for the initialization.
 *
 * @return void
 */
function saveLog( $new_site, $args ) {
    // Do something
}
add_action('wp_initialize_site','saveLog',20,2);

Since there are two events happening, that's why you need to access two hooks. So to put it all together (this is untested, I'm literally just writing this code here)

/**
 * Function for `wp_initialize_site` action-hook.
 * 
 * @param WP_Site $new_site New site object.
 * @param array   $args     Arguments for the initialization.
 *
 * @return void
 */
function saveLog( $new_site, $args ) {

    // Get the info from the newly created site
    $newInfo = array(array(
        'blog_id' => $new_site->get('id'),
        'domain' => $new_site->get('domain')
    ));

    //Get previous log info from the Transients API
    $log = get_transient('new_site_log');
    if(is_array($log)) {
        //Merge if it exists
        $log = array_merge($log,$newInfo); 
    } else {
        //Set new if it doesn't
        $log = $newInfo;
    }
    
    //Save the log to the Transients API
    set_transient('new_site_log',$log,DAY_IN_SECONDS);
}
add_action('wp_initialize_site','saveLog',20,2);

function myLog() {
    //Get the log info from the Transients API
    $log = get_transient('new_site_log');

    //If an array with stuff, loop through it
    if(is_array($log) && count($log)) {
        echo '<ol>';
        foreach($log as i => $siteInfo) {
            // Echo the info we saved
            printf(
                '<li>Blog ID: %s<br />Domain: %s</li>', 
                $siteInfo['blog_id'], 
                $siteInfo['domain']
            );   
        }
        echo '</ol>';
    } else {
        echo 'No new sites were created';
    }
}
add_action('wp_footer','myLog');

This example uses the Transients API to save the info and transients expire, but you can use whatever.

1
  • omg, these old questions keep showing up in my feed to answer and I don't realize how old they are!!!! So sorry
    – AuRise
    Commented Oct 5, 2022 at 21:17
0

Write myLog() in theme footer within the .siteInfo div. It will work.

1
  • Please explain how it will work and add some detail so that somebody with the same problem can get more detail debugging technique to solve such problem. Commented Dec 4, 2013 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.