$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Looking to create an "update theme" functionality for a custom front-end dashboard|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Looking to create an "update theme" functionality for a custom front-end dashboard

matteradmin9PV0评论

I've created a theme that includes a custom dashboard outside of wp-admin.

I would like a user who doesn't have access to the wp-admin (but does have access to my custom dashboard) to be able to receive an update notification for their theme.

If that's impossible, I could also work with the user just receiving the update notification, and sending them to wp-admin/themes.php, but that wouldn't be ideal.

Any ideas on how to push that notification to the front-end?

Edit -- This theme is hosted on a private GitHub repo, and updates are sent with this plugin.

I've created a theme that includes a custom dashboard outside of wp-admin.

I would like a user who doesn't have access to the wp-admin (but does have access to my custom dashboard) to be able to receive an update notification for their theme.

If that's impossible, I could also work with the user just receiving the update notification, and sending them to wp-admin/themes.php, but that wouldn't be ideal.

Any ideas on how to push that notification to the front-end?

Edit -- This theme is hosted on a private GitHub repo, and updates are sent with this plugin.

Share Improve this question edited Feb 2, 2019 at 0:26 Paul Elrich asked Feb 1, 2019 at 18:32 Paul ElrichPaul Elrich 1006 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You could add a REST endpoint to check if the current theme has an update. Here's a quick example to get you started:

function wpd_register_themecheck_route(){
    register_rest_route(
        'themecheck/v1',
        '/updates/',
        array( 
            'methods' => 'GET',
            'callback' => 'wpd_check_theme'
        )
    );
}
add_action( 'rest_api_init', 'wpd_register_themecheck_route' );

function wpd_check_theme(){
    $current_theme = get_option( 'template' );
    $theme_updates = get_option( '_site_transient_update_themes' );
    $return = false;
    if( array_key_exists( $current_theme, $theme_updates->response ) ){
        $return = $theme_updates->response[$current_theme];
    }
    $response = new WP_REST_Response( $return );
    return $response;
}

To get the updates, I ended up loading update text from the themes.php page by doing this:

HTML:

<div id="ajax-test"></div>

jQuery:

    $('#ajax-test').load('<?php bloginfo('url'); ?>/wp-admin/themes.php .theme.active .update-message', function(){
        $('#ajax-test').append('<a class="update-now" href="<?php bloginfo('url'); ?>/wp-admin/themes.php?theme=your-theme-name" target="_blank">Update Now</a>');
        $('#ajax-test').addClass('active');
    });

Css to hide unless update is found:

#ajax-test .button-link{
    display:none;
}
.update-now{
    display:none;
}
.update-message + .update-now{
    display:inline-block;
}

For a non-admin user, you need to give them access to theme editing and theme updating using add_cap( $role, $cap ), and styling out everything that isn't necessary for theme updating.

There are two issues with this that I can see: If a user edits CSS, they can use dashboard navigation, and they can change their theme.

I have a redirect set up in my functions.php that only allows access to this page in wp_admin:

$role = get_role( 'editor' );
$role->add_cap( 'update_themes' );
$role->add_cap( 'switch_themes' );

add_action( 'init', 'blockusers_init' );

function blockusers_init() {
    if ( is_admin() && ! current_user_can( 'administrator' ) && strpos($_SERVER['REQUEST_URI'], 'wp-admin/themes.php') == false && strpos($_SERVER['REQUEST_URI'], 'wp-admin/update.php?action=upgrade-theme') == false &&
! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
wp_redirect( home_url() );
exit;
};

For the theme updating, if a user wants to deliberately edit the html to brick their own site, I'm willing to let them.

Maybe it's not the most elegant solution, but it's the best option I've found for my situation.

Post a comment

comment list (0)

  1. No comments so far