I'm using wp_remove_get
to get the WordPress theme style content (style.css) and then get the theme version number. However I don't know exactly what regex I should use to get the version code:
<?php
$response = wp_remote_get( '.css' );
if( is_array($response) ) {
$content = $response['body']; // Remote get the file content. Now get the version number in $content.
}
?>
Also I'm going to get the version of about 20 themes (sites) on the same page, what I need to do to decrease the page load?
Thanks.
I'm using wp_remove_get
to get the WordPress theme style content (style.css) and then get the theme version number. However I don't know exactly what regex I should use to get the version code:
<?php
$response = wp_remote_get( 'http://example/wp-content/themes/theme-name/style.css' );
if( is_array($response) ) {
$content = $response['body']; // Remote get the file content. Now get the version number in $content.
}
?>
Also I'm going to get the version of about 20 themes (sites) on the same page, what I need to do to decrease the page load?
Thanks.
Share Improve this question edited Nov 1, 2015 at 16:41 jas 1,52511 silver badges23 bronze badges asked Nov 1, 2015 at 15:42 Thomas W.Thomas W. 1 3- What you're doing is inherently expensive/slow, but you should focus on how to do it in this question, and ask follow up questions in new questions once you've got an answer. Of note, regex should be unnecessary, WP already has code to do this in Core – Tom J Nowell ♦ Commented Nov 1, 2015 at 19:16
- What does "20 themes (sites) on the same page" mean? Is this multisite? – s_ha_dum Commented Nov 1, 2015 at 20:07
- It's a theme listing page, no multisite – Thomas W. Commented Nov 3, 2015 at 1:52
3 Answers
Reset to default 0Please check out this answer to a similar question about regex for getting info from the theme's style.css
.
When it comes to the second part of your question - if you are concerned about page load times, you could always use AJAX to get the information about each theme separately. You would display the page with all themes listed but without their version numbers at first. Once the page is loaded, you could send an AJAX request to wp-admin for each theme, process the information on the backend (get the theme's CSS, run regex to find version number) and formulate a response with the theme's version number. Display the version number on your page and you are done! Repeat for every theme on your list.
You can grab the version number like this:
preg_match('/[\n\r].*Version:\s*([^\n\r]*)/', $content, $matches);
$ver = $matches[1];
Do you have control over the theme?
If you do, you can create a file inside your theme, called version.php
for example, and add the following code -
<?php
header("Access-Control-Allow-Origin: https://www.example");
$parse_uri = explode('wp-content', $_SERVER['SCRIPT_FILENAME']);
require $parse_uri[0] . 'wp-load.php';
$currentTheme = wp_get_theme();
echo json_encode($currentTheme->get('Version'));
You can also get the theme name -
echo json_encode($currentTheme->get('Version'));
You can then use PHP or (better) JavaScript to access the version.php
on each site and get the theme version. Use promises or async()
/await()
to get hundreds of themes on the same page.