This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @see https://since1979.dev/snippet-005-simple-custom-rest-api-route/ | |
* | |
* @uses $.ajax() https://api.jquery.com/jquery.ajax/ | |
* @uses console.log() https://developer.mozilla.org/en-US/docs/Web/API/Console/log | |
*/ | |
$(document).ready(function () { | |
let post = 9; | |
let meta = '_thumbnail_id'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* mytheme_handle_get_meta_value. | |
* | |
* Handle calls to the Wp Rest Api /meta end-point, | |
* | |
* @see https://since1979.dev/snippet-005-simple-custom-rest-api-route/ | |
* | |
* @uses get_post_meta() https://developer.wordpress.org/reference/functions/get_post_meta/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* mytheme_get_meta_value. | |
* | |
* Register a custom Wp Rest Api end-point, | |
* | |
* @see https://since1979.dev/snippet-005-simple-custom-rest-api-route/ | |
* | |
* @uses register_rest_route() https://developer.wordpress.org/reference/functions/register_rest_route/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$posts = maybeCache('remote_posts_data', 7200, function () { | |
return do_remote_get('https://jsonplaceholder.typicode.com/posts/'); | |
}); | |
foreach ($posts as $post) { | |
echo "<h2>{$post->title}</h2>"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* maybeCache. | |
* | |
* Check if transient cache exist, else set it, | |
* | |
* @see https://since1979.dev/snippet-008-using-transients-to-cache-data/ | |
* | |
* @uses get_transient() https://developer.wordpress.org/reference/functions/get_transient/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function triggerEvent( ellement , type ) | |
{ | |
if ( 'createEvent' in document ) | |
{ | |
var e = document.createEvent( 'HTMLEvents' ); | |
e.initEvent( type , false , true ); | |
ellement.dispatchEvent( e ); | |
} | |
else | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function removeEvent( ellement , type , callback ) | |
{ | |
if ( ellement.detachEvent ) | |
{ | |
ellement.detachEvent( 'on' + type , callback ); | |
} | |
else | |
{ | |
ellement.removeEventListener( type , callback ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addEvent( ellement , type , callback ) | |
{ | |
if ( ellement.attachEvent ) | |
{ | |
ellement.attachEvent( 'on' + type , callback ); | |
} | |
else | |
{ | |
ellement.addEventListener( type , callback ); | |
} |