Giter Club home page Giter Club logo

Comments (3)

grupie001 avatar grupie001 commented on July 30, 2024

I think I am looking for something similar. I would like to connect this "sample" to my wordpress api and useless WP just as headless Backend and this "sample" as front. Any suggestion how to do it?

from samples.

nisargrthakkar avatar nisargrthakkar commented on July 30, 2024

Yes, I am looking for the same. Rest API call in AMP page.

from samples.

westonruter avatar westonruter commented on July 30, 2024

Do you mean requesting data from the WordPress REST API? If using the official AMP plugin for WordPress, the necessary CORS headers are added automatically. The headers are added automatically by AMP_HTTP::send_cors_headers() whenever the request includes the __amp_source_origin query param, which AMP adds automatically. So as long as WordPress is handling the fetch request, then the necessary CORS headers should all be supplied automatically.

So you just use the rest_url() that WordPress provides. For example, here is a shortcode that shows the latest posts:

add_shortcode(
	'amp_latest_posts',
	function() {
		$endpoint = rest_url( 'wp/v2/posts' );

		/*
		 * Enqueue scripts for when shortcode is used on non-AMP pages. These are unnecessary on AMP pages because the
		 * post-processor will automatically add them when it encounters <amp-list>.
		 */
		wp_enqueue_script( 'amp-list' );
		wp_enqueue_script( 'amp-mustache' );

		ob_start();
		?>
		<amp-list src="<?php echo esc_url( $endpoint ); ?>" items="." layout="fixed-height" height="300" width="auto">
			<template type="amp-mustache">
				<div>
					<a href="{{link}}">{{title.rendered}}</a>
				</div>
			</template>
		</amp-list>
		<?php
		return ob_get_clean();
	}
);

from samples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.