0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

wordpress関数をjsファイルで扱えるようにする

Posted at

ワードプレスである便利な関数をjsでも扱えるようにする方法のメモです。
今回はhome_url()関数を使えるようにしてみます

function custom_enqueue() {
  $tmp_path_arr = array(
    'home_url' => home_url(),
  );
  wp_enqueue_script('my_js', get_stylesheet_directory_uri() . '/js/script.js', array(), date("ymdHis", filemtime(get_stylesheet_directory() . '/js/script.js')));
  // home_url()関数をjsで使えるようにする
  wp_localize_script('my_js', 'tmp_path', $tmp_path_arr);
}
add_action('wp_enqueue_scripts', 'custom_enqueue');

functions.phpに上のコードを記述します。

  const wp_home_url = tmp_path.home_url;

上のコードを使いたいファイル先(script.js)に記述します。
以上、便利に使いまわせてお得だね〜

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?