admin管理员组文章数量:1289404
When requesting to WordPress API for any plugin or theme information for the versions field
.1/?action=theme_information&request[per_page]=12&request[slug]=twentyfifteen&request[fields][tags]=0&request[fields][sections]=0&request[fields][screenshot_url]=0&request[fields][versions]=1
I get JSON array response. Something like this:
"1.0":".1.0.zip", "1.1":".1.1.zip", "1.2":".1.2.zip",
Also These are Dynamically generated.
So I want to dynamically convert the version number as simple text and the download link as a button href URL. Also I want to wrap the whole section into a nice table with two columns, one with the version number another with the download button. If anybody can help me with the code, I will be highly grateful. Thanks in advance.
When requesting to WordPress API for any plugin or theme information for the versions field
https://api.wordpress/themes/info/1.1/?action=theme_information&request[per_page]=12&request[slug]=twentyfifteen&request[fields][tags]=0&request[fields][sections]=0&request[fields][screenshot_url]=0&request[fields][versions]=1
I get JSON array response. Something like this:
"1.0":"https://downloads.wordpress/theme/twentyfifteen.1.0.zip", "1.1":"https://downloads.wordpress/theme/twentyfifteen.1.1.zip", "1.2":"https://downloads.wordpress/theme/twentyfifteen.1.2.zip",
Also These are Dynamically generated.
So I want to dynamically convert the version number as simple text and the download link as a button href URL. Also I want to wrap the whole section into a nice table with two columns, one with the version number another with the download button. If anybody can help me with the code, I will be highly grateful. Thanks in advance.
1 Answer
Reset to default 0There's quite a bit to this... I feel like I'm doing your job for you, but here it is.
A table automatically build to display download buttons for each version of a theme returned in the themes_api()
request.
You must run this at an appropriate time - at least after WordPress init
:
Simply call the function with the theme slug you want:
function display_version_downloads_table_for_theme( $slug ) {
if ( !function_exists( 'themes_api' ) ) {
echo "You're calling this function display_version_downloads_table_for_theme() too soon.";
return;
}
$result = themes_api( 'theme_information', [
'slug' => $slug,
'fields' => [
'versions' => true,
'sections' => false,
'screenshot_url' => false,
'tags' => false,
]
] );
$versions = ( is_object( $result ) && !empty( $result->versions ) && is_array( $result->versions ) ) ? $result->versions : [];
foreach ( $versions as $version => $link ) {
if ( empty( $version ) ) {
unset( $versions[ $version ] );
}
}
if ( empty( $versions ) ) {
echo 'Themes API request either failed, or there were no versions available for the requested theme';
}
else {
?>
<script type="text/javascript">
function runDownload( button ) {
window.open( button.getAttribute( 'data-href' ), '_blank' );
}
</script>
<table>
<thead>
<tr>
<th>Version</th>
<th>Download</th>
</tr>
</thead>
<tbody>
<?php foreach ( $versions as $version => $href ) : ?>
<tr>
<td><?= $version ?></td>
<td>
<button onclick="runDownload( this )" data-href="<?= esc_url( $href ) ?>">
Download
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php }
}
display_version_downloads_table_for_theme( 'twentyfifteen' );
本文标签: jsonWordPress ThemePlugin Information API Response to Text and Button
版权声明:本文标题:json - WordPress ThemePlugin Information API Response to Text and Button 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741424403a2377991.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论