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.

Share Improve this question asked Jul 22, 2021 at 19:13 InianInian 1
Add a comment  | 

1 Answer 1

Reset to default 0

There'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