Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Custom Welcome Widget in all themes

Hello,

I'm using a custom welcome widget. I have added the code to my themes function.php. I noticed that when I change the theme the welcome widget doesn't appear - obviously as the active function.php file has changed. Is there a way to make the widget appear always no matter which theme is activated?

<?php

// DASHBOARD WELCOME WIDGET

function register_raccoonbox_ohjauspaneeli_vimpain() {
    global $wp_meta_boxes;

    wp_add_dashboard_widget(
        'raccoonbox_ohjauspaneeli_vimpain',
        'Tervetuloa mahtavalle sivullesi!',
        'raccoonbox_ohjauspaneeli_vimpain_display'
    );

    $dashboard = $wp_meta_boxes['dashboard']['normal']['core'];

    $my_widget = array( 'raccoonbox_ohjauspaneeli_vimpain' => $dashboard['raccoonbox_ohjauspaneeli_vimpain'] );
    unset( $dashboard['raccoonbox_ohjauspaneeli_vimpain'] );

    $sorted_dashboard = array_merge( $my_widget, $dashboard );
    $wp_meta_boxes['dashboard']['normal']['core'] = $sorted_dashboard;
}
add_action( 'wp_dashboard_setup', 'register_raccoonbox_ohjauspaneeli_vimpain' );

function raccoonbox_ohjauspaneeli_vimpain_display() {
    ?>

  <a href="http://raccoonbox.com" target="_blank"><img src="http://raccoonbox.com/raccoonbox-img/RACCOONBOX-logo_200.png" alt="Raccoonbox logo" width="100px" height="100px"></a>

    <p>
       Lorem
    </p>
    <p>
       Lorem
    </p>

    <?php
}

// EDIT ADMIN FOOTER

add_filter('admin_footer_text', 'remove_footer_admin'); //change admin footer text
function remove_footer_admin () {
echo 'Kiitos kun valitsit <a href="http://raccoonbox.com">Raccoonboxin</a> | Muotoilija <a href="http://raccoonbox.com/muotoilijat">Konstantin Nikkari</a';
}

?>

1 Answer

Yes, you most certainly can. The best way to do that is to create your own plugin that uses the code you have for the widget. There is a WordPress feature called Must Use Plugins, which are installed into a different directory, and are available to all themes.

Must Use Plugins: https://codex.wordpress.org/Must_Use_Plugins

Here is a tutorial showing how to make a widget plugin, you can probably just take the code you have an wrap it in the plugin functionality and get it going fairly easily:

http://www.wpexplorer.com/create-widget-plugin-wordpress/

Marvellous! Big thanks to you Luke.