0

I am working on a WordPress theme. We implemented "One Click Demo Import" for the theme users to quickly populate their new blank theme with some dummy data.

My question is, how can I set the logo of a WordPress site with PHP? This needs to be done inside an action. I call add_action( 'pt-ocdi/after_import', 'kinsley_ocdi_after_import_setup' ); and in the kinsley_ocdi_after_import_setup I need to run this processing code.

This seems like it should not be hard to do, but I cannot find a "set_logo" function. Is this possible using just WordPress core? None of the *_custom_logo() functions allow data to be altered.

What I am looking for inside the action function:

// set logo
if (!has_custom_logo()) {
    // set logo of site to an image located at http://www.example.com/myimage.png
}
1

The logo is a theme mod, so you can set it with set_theme_mod( 'custom_logo', $value );, but the value needs to be an attachment ID, so you'll need to upload it first.

If you don't want to upload anything, you can use the get_custom_logo() filter to provide your own HTML for the logo. If you want to mimic the way WordPress does it, just make sure to include the link custom-logo classes:

function wpse_304465_custom_logo( $html ) {
    if ( $html == '' ) {
        $html = sprintf( 
            '<a href="%1$s" class="custom-logo-link" rel="home" itemprop="url">%2$s</a>',
            esc_url( home_url( '/' ) ),
            '<img src="http://www.example.com/myimage.png" class="custom-logo" itemprop="logo">'
        );
    }

    return $html;
}
add_filter( 'get_custom_logo', 'wpse_304465_custom_logo' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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