Last active
November 2, 2020 15:15
-
-
Save georgestephanis/fe9913482b7b20e3f2a288d97061f25a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: Child Theme Settings Migrator | |
| */ | |
| add_action( 'admin_init', 'child_theme_settings_migrator' ); | |
| function child_theme_settings_migrator() { | |
| $theme = wp_get_theme(); | |
| // Cap check | |
| if ( ! current_user_can( 'edit_theme_options' ) ) { | |
| return; | |
| } | |
| // If the current theme is not a child theme, bail. | |
| if ( $theme->get_stylesheet() === $theme->get_template() ) { | |
| return; | |
| } | |
| // The form that does the migration... | |
| add_action( 'tool_box', 'child_theme_settings_migrator_tool_box' ); | |
| if ( isset( $_POST['action'] ) && 'migrate-child-theme-settings' === $_POST['action'] ) { | |
| check_admin_referer( 'migrate-child-theme-settings_' . $theme->get_stylesheet() ); | |
| $parent_theme_mods = get_option( 'theme_mods_' . $theme->get_template() ); | |
| $child_theme_mods = get_option( 'theme_mods_' . $theme->get_stylesheet() ); | |
| // Later values replace previous values -- will not overwrite any already set for child. | |
| $new_theme_mods = array_merge( $parent_theme_mods, $child_theme_mods ); | |
| update_option( 'theme_mods_' . $theme->get_stylesheet(), $new_theme_mods ); | |
| wp_safe_redirect( add_query_arg( 'migrate-success', 'true', wp_get_referer() ) ); | |
| } | |
| } | |
| function child_theme_settings_migrator_tool_box() { | |
| $theme = wp_get_theme(); | |
| ?> | |
| <div class="card"> | |
| <h2 class="title"><?php esc_html_e( 'Migrate Parent Theme Settings to Child Theme' ); ?></h2> | |
| <p><?php esc_html_e( 'This operation will migrate all theme mods from the parent theme to your current theme. It is non-destructive, and will skip any settings that have already been generated by the child theme.' ); ?></p> | |
| <?php if ( isset( $_GET['migrate-success'] ) && 'true' === $_GET['migrate-success'] ) : ?> | |
| <p><strong><?php esc_html_e( 'Done!' ); ?></strong></p> | |
| <?php else : ?> | |
| <form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="POST"> | |
| <input type="hidden" name="action" value="migrate-child-theme-settings" /> | |
| <?php wp_nonce_field( 'migrate-child-theme-settings_' . $theme->get_stylesheet() ); ?> | |
| <?php submit_button( __( 'Migrate Settings' ) ); ?> | |
| </form> | |
| <?php endif; ?> | |
| </div> | |
| <?php | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment