Copy a file from a plugin into my theme directory

To answer your question, you have specified the paths incorrectly: plugin_dir_path( __FILE__ ) already has a trailing slash at the end (having two trailing slashes should not be a problem, but safer is to have one) and get_stylesheet_directory() comes with no trailing slash at the end, so you have to add one before adding the filename. Your final code should be like this:

<?php
function file_replace() {

    $plugin_dir = plugin_dir_path( __FILE__ ) . 'library/front-page.php';
    $theme_dir = get_stylesheet_directory() . '/front-page.php';

    if (!copy($plugin_dir, $theme_dir)) {
        echo "failed to copy $plugin_dir to $theme_dir...\n";
    }
}

add_action( 'wp_head', 'file_replace' );

Leave a Comment