php WordPress - REST API

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php WordPress - REST API相关的知识,希望对你有一定的参考价值。

<?php

// Allow cross origin requests
add_filter( 'allowed_http_origin', '__return_true' );

// Se greffe à la route "authentification" de l'api rest native
add_filter( 'rest_index', function ( $response_object ) {
	if ( empty( $response_object->data['authentication'] ) ) {
		$response_object->data['authentication'] = array();
	}

	$response_object->data['authentication']['password_reset'] = array(
		'url' => wp_lostpassword_url(),
	);
	return $response_object;
});

add_action( 'rest_api_init', function () {
	// register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
	register_rest_field(
		'document',
		'meta',
		array(
			'get_callback' => function ( $object ) {
				$metas = get_post_meta($object["id"]);

				$count = 1;

				while ( isset($metas[ "pj_".$count ]) ) {
					if($metas[ "pj_".$count ][0] !== ""){
						$meta_content = $metas[ "pj_".$count ][0];
						$regex_url = '/((http|https):\/\/)[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=éè]*)?\g';

						$preg_match = preg_match($regex_url, $meta_content);

						// Si ce n'est pas une url
						if($preg_match === false){
							$attachement_url = wp_get_attachment_url( $meta_content );
							$metas[ "pj_".$count ][0] = $attachement_url;
						}
					}
					$count ++;
				}

				return $metas;
			},
			'schema' => null,
		)
	);

	register_rest_field(
	'document',
	'hasDefaultFeaturedImage',
	array(
		'get_callback' => function ( $object ) {
				$post_id = $object['id'];

				$post_thumb_id = get_post_thumbnail_id( $post_id );
				$default_thumb_id = get_option('dfi_image_id', true);

				if ($default_thumb_id === $post_thumb_id){
					return true;
				} else {
					return false;
				}

			},
			'schema' => null,
		)
	);
});

?>
<?php

// In your functions.php file
add_action( 'rest_api_init', function () {
  
  $postTypes = array('post', 'page');
  foreach( $postTypes as $postType ){
    register_rest_field (
      $postType,
      $restFieldName,
      array (
        'schema'        => null,
        'get_callback'  => function ( $post ) {
          $post_id = $post['id'];
          return get_post_meta( $post_id );
        }
      )
    )
  }
  
} );

?>
<?php

// Allow cross origin requests
add_filter( 'allowed_http_origin', '__return_true' );

?>
<?php
// Registering multi rest fields for multi post types

/**
 * Register restFields given for each postTypes given
 *
 * @method registerRestFields
 *
 * @param  array              $postTypes  Array containing the postTypes slugs to add the restfields given
 * @param  array              $restFields Array of Array that containins the fieldName and the fieldCallback
 */
function registerRestFields ( array $postTypes, array $restFields ) {

  foreach ( $postTypes as $postType) {
    foreach ( $restFields as $field ) {

      register_rest_field(
        $postType,
        $field["name"],
        array(
          'schema'        => null,
          'get_callback'  => $field["callback"]
        )
      );

    }
  }

};

\!h add_action( 'rest_api_init', function() {
  \!h $postTypes = array('post', 'page', 'calendar-event');
  \!h $restFields = array(
    \!h array(
      \!h 'name'            => 'meta',
      \!h 'callback'        => function( $post ){
        \!h return get_post_meta( $post["id"] );
    \!h  },
  \!h  ),
    \!h array(
      \!h 'name'            => 'featuredImage',
      \!h 'callback'        => function( $post ){
        \!h return get_the_post_thumbnail_url_for_all_sizes($post["id"]);
    \!h   },
\!h     )
\!h   );
  \!h registerRestFields( $postTypes, $restFields );
\!h }

?>

以上是关于php WordPress - REST API的主要内容,如果未能解决你的问题,请参考以下文章

php Wordpress REST API基础知识

php 用于WordPress的SAAS REST API

php Wordpress在WP REST API上添加自定义帖子类型

wordpress rest api 授权

WordPress插件:使用REST API

如何在 wordpress functions.php 中发送一系列类别和帖子?