Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress - How does one make a URL return dynamic JSON with custom Content Type?

Some background first, I'm trying to follow the directions here:

https://developer.apple.com/library/archive/documentation/General/Conceptual/AppSearch/UniversalLinks.html#//apple_ref/doc/uid/TP40016308-CH12-SW1

to get universal links working with my website for iOS devices. My wordpress site is in a shared hosting environment, therefore I do NOT have access to the server or any root files. I placed the file at the root of my site (httpdocs) and when I navigate to the page it does not load. In googling around, I found a stackoverflow post stating you had to config the server to let it know to serve up that file with a content type of "application/pkcs7-mime" (Config apple-app-site-association file with wordpress). As mentioned I do not have access to that.

I have a subdomain which is a microsoft web api project that I am very familiar with. I made an endpoint for "apple-app-site-association" and dynamically built the json response in code and set the content type and returned it. This ended up working and Apple detects it and everything. This is interesting as I do NOT need a file on my server as I generate it on the fly, and I don't need to change anything server wise.

The issue however is I need the main site domain to be the universal link, not the windows subdomain. The main site is wordpress. I'm thinking if may be possible to do the same thing in wordpress where I navigate to mydomain.com/apple-app-site-association and I generate the json and content-type on the fly and serve it up.

I've never done any coding with wordpress and don't even know where to start.

If possible I would like clear direction in the following:

Making mydomain.com/apple-app-site-association lead to a custom function or page that serves up json. Have code that makes the json get served up dynamically. Have code that sets the content-type of the response to "application/pkcs7-mime". Of course if someone knows how to solve the file not being served no a shared server without this method, I'm open to that as well. From what I can see, this may be the only way assuming this can be done.

like image 324
SolidSnake4444 Avatar asked Jun 23 '19 22:06

SolidSnake4444


1 Answers

Note: I'm assuming WordPress is installed in the root folder.

So if you want http://example.com/apple-app-site-association to serve a JSON content like this and have the content type (Content-Type header) set to application/pkcs7-mime, here are some options you can choose from:

Dynamic Content

Because you're using WordPress, this might be a better option for you than manually editing the .htaccess file.

You can use the parse_request hook; this way, you don't need any custom (WordPress) rewrite rules, no need for a custom Page (post type of page), and you don't need to create any JSON file.

And make sure there's no file named apple-app-site-association in the root folder, or a WordPress Page having the slug apple-app-site-association.

So this would go in your theme functions file (e.g. wp-content/themes/your-theme/functions.php):

<?php
add_action( 'parse_request', 'serve_apple_app_site_association', 0 );
function serve_apple_app_site_association( $wp ) {
    // Check if the request is /apple-app-site-association
    if ( 'apple-app-site-association' !== $wp->request ) {
        return;
    }

    // Array version of the JSON data.
    $data = array(
        'applinks'    => array(
            'apps'    => array(),
            'details' => array(
                array(
                    'appID' => '9JA89QQLNQ.com.apple.wwdc',
                    'paths' => array(
                        '/wwdc/news/',
                        '/videos/wwdc/2015/*',
                    ),
                ),
                array(
                    'appID' => 'ABCD1234.com.apple.wwdc',
                    'paths' => array(
                        '*',
                    ),
                ),
            ),
        ),
    );

    // Send headers.
    status_header( 200 );
    nocache_headers();
    header( 'Content-Type: application/pkcs7-mime' );

    // And serve the JSON data.
    echo wp_json_encode( $data );
    exit;
}

Static Content

  1. Place the JSON data in a file named apple-app-site-association (i.e. no extension) and save the file in the root folder where you could see the wp-config.php and .htaccess files.

  2. Add this to your .htaccess file:

<Files apple-app-site-association>
        Header set Content-Type application/pkcs7-mime
</Files>

If the <Files> and/or Header don't/doesn't work for you, then you could just use the first option above, but use something like readfile() to read the static file.

Alternatively (and specifically if URL rewriting is not available/supported on your site), you could create a folder named apple-app-site-association in the root folder and add index.php to that folder and in that file:

<?php
header( 'Content-Type: application/pkcs7-mime' );
// Read the static file or just generate a dynamic content.
@readfile( __DIR__ . '/apple-app-site-association.json' );
exit;
like image 123
Sally CJ Avatar answered Nov 03 '22 17:11

Sally CJ