//REST API 投稿の画像URLを簡単に取得できるようにする k.arima add_action('rest_api_init', 'custom_wp_rest_api_post'); function custom_wp_rest_api_post() { register_rest_field(['post','page'], 'featured_image', array( 'get_callback' => 'rest_api_add_image', 'update_callback' => null, 'schema' => null, ) ); } add_action('rest_api_init', 'custom_wp_rest_api_page'); function custom_wp_rest_api_page() { register_rest_field('page', 'featured_image', array( 'get_callback' => 'rest_api_add_image', 'update_callback' => null, 'schema' => null, ) ); } function rest_api_add_image($object, $field_name, $request) { $feat_img_array = wp_get_attachment_image_src($object['featured_media'], 'large', true); return [ 'src' => $feat_img_array[0], 'width' => $feat_img_array[1], 'height' => $feat_img_array[2], ]; } // Register a new rest route add_action( 'rest_api_init', 'my_rest_routes' ); function my_rest_routes() { register_rest_route( 'alexi/v1', '/output_css_uri/', [ 'methods' => 'GET', 'callback' => 'output_css_callback', ] ); } add_action( 'rest_api_init', 'register_rest_category_name'); if ( ! function_exists( 'register_rest_category_name' )) { function register_rest_category_name() { register_rest_field( 'post', 'category_name', array( 'get_callback' => 'get_category_name' )); } function get_category_name( $object ) { $category = get_the_category($object[ 'id' ]); return $category[0]->cat_name; } } //カテゴリ名を取得する関数を登録 add_action( 'rest_api_init', 'register_category_name' ); function register_category_name(){ //register_rest_field関数を用いget_category_name関数からカテゴリ名を取得し、追加する register_rest_field( 'post', 'custom_categories', array( 'get_callback'=>'get_custom_category_name' ) ); } //$objectは現在の投稿の詳細データが入る function get_custom_category_name( $object ) { $category = get_the_category($object[ 'id' ]); for ($i = 0; $i < count($category); ++$i) { $cat_name[$i]['id'] = $category[$i]->term_id; $cat_name[$i]['name'] = $category[$i]->name; $cat_name[$i]['slug'] = $category[$i]->slug; } return $cat_name; } add_action( 'rest_api_init', 'register_tag_name' ); function register_tag_name() { //register_rest_field関数を用いget_category_name関数からカテゴリ名を取得し、追加する register_rest_field( 'post', 'custom_tags', array( 'get_callback' => 'get_tag_name' ) ); } function get_tag_name( $object ) { $tag = get_the_tags($object[ 'id' ]); for ($i = 0; $i < count($tag); ++$i) { $cat_name[$i]['id'] = $tag[$i]->term_id; $cat_name[$i]['name'] = $tag[$i]->name; $cat_name[$i]['slug'] = $tag[$i]->slug; } return $cat_name; } function balloon_shortcode($atts, $content = null) { $content = do_shortcode( shortcode_unautop( $content ) ); $path = get_stylesheet_directory_uri().'/img/'; // athomemade delete $dir_info = wp_upload_dir(); // ベースディレクトリ情報 $basedir = $dir_info['basedir']; // ベースURL情報 $baseurl = $dir_info['baseurl']; extract(shortcode_atts(array( 'face' => 'man1', 'name' => '', 'align' => 'left', 'border' => 'gray', 'bg' => 'none', 'style' => '', ),$atts)); switch ($face) { case "man1": $imageUrl = get_template_directory_uri().'/img/man1.png'; break; case "man2": $imageUrl = get_template_directory_uri().'/img/man2.png'; break; case "man3": $imageUrl = get_template_directory_uri().'/img/man3.png'; break; case "woman1": $imageUrl = get_template_directory_uri().'/img/woman1.png'; break; case "woman2": $imageUrl = get_template_directory_uri().'/img/woman2.png'; break; case "woman3": $imageUrl = get_template_directory_uri().'/img/woman3.png'; break; default: // athomemade // ファイル名からファイルのパスを取得 $result = array(); foreach(glob($basedir . '/*/*/'.$face) as $file) { $result[] = $file; } if (empty($result)) { $result[0] = ""; } // ファイルパスをURLに変更 $path_result = str_replace($basedir, $baseurl, $result[0]); $imageUrl = $path_result; //任意のアバター画像を挿入する break; } return '
'.$name.'
'.$name.'
'.$content.'
'; } add_shortcode('chat','balloon_shortcode');