make_site_theme_from_oldschool() WordPress Function

The make_site_theme_from_oldschool() Wordpress function allows you to quickly create a new theme for your site based on an existing theme. This function is especially useful if you want to create a child theme or if you want to create a new theme based on an existing one. All you need to do is specify the path to the existing theme and the path to the new theme, and the function will do the rest.

make_site_theme_from_oldschool( string $theme_name, string $template ) #

Creates a site theme from an existing theme.


Description


Top ↑

Parameters

$theme_name

(string)(Required)The name of the theme.

$template

(string)(Required)The directory name of the theme.


Top ↑

Return

(bool)


Top ↑

Source

File: wp-admin/includes/upgrade.php

3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
function make_site_theme_from_oldschool( $theme_name, $template ) {
    $home_path = get_home_path();
    $site_dir  = WP_CONTENT_DIR . "/themes/$template";
 
    if ( ! file_exists( "$home_path/index.php" ) ) {
        return false;
    }
 
    /*
     * Copy files from the old locations to the site theme.
     * TODO: This does not copy arbitrary include dependencies. Only the standard WP files are copied.
     */
    $files = array(
        'index.php'             => 'index.php',
        'wp-layout.css'         => 'style.css',
        'wp-comments.php'       => 'comments.php',
        'wp-comments-popup.php' => 'comments-popup.php',
    );
 
    foreach ( $files as $oldfile => $newfile ) {
        if ( 'index.php' === $oldfile ) {
            $oldpath = $home_path;
        } else {
            $oldpath = ABSPATH;
        }
 
        // Check to make sure it's not a new index.
        if ( 'index.php' === $oldfile ) {
            $index = implode( '', file( "$oldpath/$oldfile" ) );
            if ( strpos( $index, 'WP_USE_THEMES' ) !== false ) {
                if ( ! copy( WP_CONTENT_DIR . '/themes/' . WP_DEFAULT_THEME . '/index.php', "$site_dir/$newfile" ) ) {
                    return false;
                }
 
                // Don't copy anything.
                continue;
            }
        }
 
        if ( ! copy( "$oldpath/$oldfile", "$site_dir/$newfile" ) ) {
            return false;
        }
 
        chmod( "$site_dir/$newfile", 0777 );
 
        // Update the blog header include in each file.
        $lines = explode( "\n", implode( '', file( "$site_dir/$newfile" ) ) );
        if ( $lines ) {
            $f = fopen( "$site_dir/$newfile", 'w' );
 
            foreach ( $lines as $line ) {
                if ( preg_match( '/require.*wp-blog-header/', $line ) ) {
                    $line = '//' . $line;
                }
 
                // Update stylesheet references.
                $line = str_replace( "<?php echo __get_option('siteurl'); ?>/wp-layout.css", "<?php bloginfo('stylesheet_url'); ?>", $line );
 
                // Update comments template inclusion.
                $line = str_replace( "<?php include(ABSPATH . 'wp-comments.php'); ?>", '<?php comments_template(); ?>', $line );
 
                fwrite( $f, "{$line}\n" );
            }
            fclose( $f );
        }
    }
 
    // Add a theme header.
    $header = "/*\nTheme Name: $theme_name\nTheme URI: " . __get_option( 'siteurl' ) . "\nDescription: A theme automatically created by the update.\nVersion: 1.0\nAuthor: Moi\n*/\n";
 
    $stylelines = file_get_contents( "$site_dir/style.css" );
    if ( $stylelines ) {
        $f = fopen( "$site_dir/style.css", 'w' );
 
        fwrite( $f, $header );
        fwrite( $f, $stylelines );
        fclose( $f );
    }
 
    return true;
}


Top ↑

Changelog

Changelog
VersionDescription
1.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.