wp_replace_insecure_home_url()


WordPressのwp_replace_insecure_home_url()関数は、ホームURLのプロトコルをHTTPからHTTPSに置き換えるために使用されます。この関数は、セキュリティを強化するために、サイトのURLが安全でない場合に自動的に修正を行います。

構文

wp_replace_insecure_home_url( string $content );
  • $content(string) — 置き換えを行う対象のコンテンツ。

例1: 基本的な使用法

この例では、コンテンツ内のホームURLを安全なHTTPSに置き換えます。

echo wp_replace_insecure_home_url( get_the_content() );

例2: 投稿のコンテンツを修正

投稿のコンテンツ内のURLを修正するために使用します。

$post_content = get_post_field( 'post_content', get_the_ID() ); echo wp_replace_insecure_home_url( $post_content );

例3: カスタムフィールドのURLを修正

カスタムフィールドに含まれるURLを修正します。

$custom_field = get_post_meta( get_the_ID(), 'custom_url', true ); echo wp_replace_insecure_home_url( $custom_field );

例4: ウィジェット内のURLを修正

ウィジェットのテキスト内のURLを修正します。

$widget_text = get_option( 'widget_text' ); echo wp_replace_insecure_home_url( $widget_text[2]['text'] );

例5: コメント内のURLを修正

コメント内のURLを修正します。

$comment_text = get_comment_text( get_comment_ID() ); echo wp_replace_insecure_home_url( $comment_text );

例6: メニューのURLを修正

メニュー項目のURLを修正します。

$menu_items = wp_get_nav_menu_items( 'primary' ); foreach ( $menu_items as $item ) { echo wp_replace_insecure_home_url( $item->url ); }

例7: ショートコード内のURLを修正

ショートコードの出力内のURLを修正します。

echo wp_replace_insecure_home_url( do_shortcode( '[custom_shortcode]' ) );

例8: テーマオプションのURLを修正

テーマオプションに保存されているURLを修正します。

$theme_option = get_theme_mod( 'custom_url' ); echo wp_replace_insecure_home_url( $theme_option );

注意点

  • この関数は、コンテンツ内のホームURLのみを修正します。他のURLには影響しません。
  • HTTPSへの移行が完了している場合に使用することが推奨されます。
  • データベース内のURLを直接変更するわけではないため、永続的な修正を行う場合はデータベースの更新が必要です。

関連機能: