WordPressに独自リライトルールと独自URLパラメータの追加($wp_rewrite/$wp_query) WordPressは内部的に常にURLのリライトを行いながら、 サイトのトップに配置している「index.php」が処理をしている事はご存知の事と思います。 WordPressの中のコンテンツで独自のURLを認識させるためには、 リライトルールを定義する必要があります。 尚、本ページは以下の1コンテンツとして紹介しているものです。 一連の流れは以下をご参照ください。
Webサーバのリライト機能(Apacheではmod_rewrite)でアクセスURLが変換されます。指定したURLに対応するファイル、ディレクトリが存在しない場合、index.phpにアクセスするようにURLが変換されます。よって、/index.phpとなります。 WordPressのリライトルールが適用されます。リライトルール取得・適用は 「/wp-includes/class-wp.php」内のparse_request関数で次のように行っています。 $rewrite = $wp_rewrite->wp_rewrite_rules(); foreach ( (array) $rewrite as $match => $query ) { } 今回の場合、リライトルール ([^/]+)(/[0-9]+)?/?$ にマッチして、/index.php?name=hello-world&pag
WordFes Nagoya 2016 でお話したLTのスライドです。
WordPressプラグイン開発のバイブルという本がありまして、大絶賛大好評販売中なのですが、ボツ原稿を載せていきたいと思います。 ご購入はこちらから。 WordPressにたくさんあるAPIのうち、Rewrite APIを取り上げます。ここは、大幅に簡略化したところでして、ものすごく長いのでシリーズ物にしたいと思います。Rewrite APIはWordPressをアプリケーション・プラットフォームとして利用する際にとても便利なものですので、WordPressを使ってアプリを作るぞ!みたいな方々のお役に立てると思います。 第一弾はRewrite APIの前提となる「Rewriteとパーマリンク」の関係についてです。それでは、以下、ボツ原稿です(校正をするまえにボツになったので間違いも含むかもしれません。一応チェックはしました)。 次回は、デフォルトの状態のリライトを拡張して、WordPre
お世話になります。 Bogoを使って、多言語サイトを構築しておりますが、カスタム投稿タイプにて使っているスラッグ名を固定ページのURLとしても使っており 第1言語では問題無く意図した動作をしますが、第2言語ではトップページにリライトされてしまいます。 これを多言語全てで動作させるにはどうすれば良いでしょうか? ちなみに、2年前のフォーラム https://ja.forums.wordpress.org/topic/24252?replies=16 と近しい内容に感じますが WordPress 4.3, Bogo 2.5に https://ja.forums.wordpress.org/topic/152931?replies=22 のToro_Unit さんパッチを追記した環境で意図した動作をしていません。 ■前提 ・固定ページ(例としてnewsページ)を作成し、カスタム投稿タイプで制作し
WordPressサイトに独自のURLを追加するのに一番簡単な方法は、add_rewrite_endpoint()関数を使うことです。 この関数を使うと、たとえばhttp://example.com/に対してhttp://example.com/events/みたいなURLを追加するには以下のようにすればオッケーです。 /* Plugin Name: おれおれイベントカレンダー */ register_activation_hook(__FILE__, 'my_activation_callback'); function my_activation_callback() { add_rewrite_endpoint('events', EP_ROOT); flush_rewrite_rules(); } 上記のソースをプラグインとして有効化してhttp://example.com/eve
リライト処理 † WordPressでは、独自のリライトルールを定義することができる。リライトのルールは、wp-includes/wp-class.phpのWP::parse_request()で参照されるが、そこで独自に定義したルールにマッチングするようにルールを設定してやればよい。 // wp-includes/wp-class.php // 134行目付近 // Fetch the rewrite rules. $rewrite = $wp_rewrite->wp_rewrite_rules(); // 198行目付近 if ( preg_match("#^$match#", $request_match, $matches) || preg_match("#^$match#", urldecode($request_match), $matches) ) { // Got a m
≫新しい記事「カスタム投稿タイプ使用時のパーマリンクをPostnameからPost_Idにする(2)」があります。 カスタム投稿タイプで投稿した内容のパーマリンクは「/post_type/postname」が標準でスラングを使用していないとタイトルがURLになる。wikiのような表示と言えば分りやすいだろうか。 他のページはpost_id表示しているので、これもpost_id表示にしたい・・・ // function.php add_action('init', 'myposttype_rewrite'); function myposttype_rewrite() { global $wp_rewrite; $queryarg = 'post_type=myposttype&p='; $wp_rewrite->add_rewrite_tag('%post_id%', '([^/]+)',
カスタム投稿のパーマリンクはデフォルトだと /カスタム投稿名/投稿タイトル になってて、これが結構使いづらいんですよ。日本語タイトルだとURLまで日本語になってしまったり。 また、月別アーカイブとかが無かったりで色々と面倒。なので、functions.phpで色々カスタマイズします。 2011-12-28追記:プラグインにしました。今後はこちらをお使い下さい。Custom Post Type Permalinks [php] class set_custom_rewrite { var $post_type; function set_custom_rewrite($post_type){ $this->post_type = $post_type; add_action(‘init’, array(&$this,’set_rewrite’)); add_action(‘init’, ar
WordPress で投稿のURLは、 /article/%postname%/ とか /blog/%post_id%/ にしたいけど、パーマリンク設定で article や blog といったディレクトリを入れてしまうと、カテゴリーやアーカイブの表示でも、このディレクトリが付いてしまってかっこ悪いですよね。 今回は、このちょっとしたモヤモヤを解消するべく、投稿の詳細表示の時のみ特定のディレクトリを追加する方法を紹介します。 投稿詳細ページのパーマリンクを変えるには、リンクを出力する時と、アクセスされた際に条件を判定するリライトルールの作成時の二箇所にフックをする必要があります。 前者は、pre_post_link と言うフィルターフックを利用します。このフィルターフックは、get_permalink 関数内に存在し、パーマリンク設定の内容をフィルタリングすることができます。ここで、ディレ
WordPressをCMSとしてに利用する場合、作成者(投稿者)アーカイブが必要ないケースがあります。 基本的に作成者アーカイブは作成されてしまうので、これを「作成されない」方法をまとめておきます。(Twitterでのやりとりをまとめただけです) ちなみにTwitterでつぶやいたら、@jim0912 さんに光の速さで返信をもらいました。 @gatespace_k authorのリライトルール作るところ削ればおk — まがぞん (@jim0912) May 15, 2012 @gatespace_k add_filter( 'author_rewrite_rules', '__return_empty_array' ); — まがぞん (@jim0912) May 15, 2012 ステキです。さすがです。 ということで、テーマのfunctions.phpに以下を記述。 add_filte
設定方法 function.phpのregister_post_type()の箇所で、下記のように’rewrite’を設定する事でカスタム投稿タイプのパーマリンク設定ができます。 ‘rewrite’ => true, にするとダッシュボードで設定したパーマリンクが反映されるようになります。 register_post_type( 'tempatetag', array( 'label' => 'Templateタグ', 'public' => true, 'hierarchical' => true, 'can_export' => true, 'show_in_nav_menus' => true, 'rewrite' => //↑rewriteの指定をする array( 'slug' => 'parmarink', 'with_front' => 'false' ), ) ) !注意点
Copyright (C) 2012 Prime Strategy co.,ltd. Prime Strategy Copyright (C) 2012 Prime Strategy co.,ltd. Prime Strategy 2 Copyright (C) 2012 Prime Strategy co.,ltd. Prime Strategy 3 Copyright (C) 2012 Prime Strategy co.,ltd. Prime Strategy 4 Chapter 0 自己紹介 Copyright (C) 2012 Prime Strategy co.,ltd. Prime Strategy 5 Chapter 0 自己紹介 PS Auto Sitemap PS Disable Auto Formatting PS Taxonomy Expander Prime St
WordPressでリライトをコントロールしているのはWP_Rewriteというクラスで、$wp_rewriteというインスタンスが在ります。このWP_Rewriteによってリライトルールが生成され、みんな大好きパーマリンクを実現しています。 リライトルールはキーを正規表現、値をリライトするURLとする連想配列で保存されています。 $rules = $wp_rewrite->wp_rewrite_rules(); foreach ( $rules as $regex => $rule ) { echo $regex . " => " . $rule . '<br />'; } こんな感じで展開して確認してみるといいです。 このルールをWP_Rewriteを使って生成しているわけですが、大きな流れとしては、
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く