• Hi,

    I generally work with multi-site networks and like to code my own websites from scratch and turn it into a wordpress theme.

    Then they are broken up by websites (themes). All the files that I use in my project are in the theme files

    For example if I build a html website from scratch I might have a file struct like below:

    Folder (News)
    index.html
    about-us.html
    contact.html

    Folder (CSS)
    screen.css

    Folder (JS)
    main.js

    Then I will convert it into a theme

    Then I use ACF Pro to add CMS functionality.

    I normally do this by making pages as per below:

    page-home.php (Home page) then set that as the static home page. The files reside in the wp-content/themes

    I understand there is a way to enqueue the style sheet and JavaScript in functions.php but this is time consuming and in my case unnecessary as I generally all my code is loaded first anyway then I’ll add the WordPress tag after it anyways.

    My question is, with HTML files and websites a lot of references to links are relative for example:

    <link href=”css/screen.css” rel=”stylesheet”>

    WordPress doesn’t like this and will not include my styles or scripts like this unless I manually enqueue theme in the functions.php

    Is there a word-press PHP function that will generate the absolute path for my link instead of manually enqueue them in the functions.php

    • This topic was modified 4 years, 10 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not a Developing with WordPress topic
Viewing 3 replies - 1 through 3 (of 3 total)
  • Honestly, once you get used to wp_enqueue_scripts() function is is faster and easier to manage IMO. Worth getting used to.

    If you’re looking to generate a URL to your theme you can use get_template_directory_uri() or if you plan on supporting child themes you can use get_stylesheet_directory_uri().

    This will return a full URL to your theme like so:

    https://example.com/wp-content/themes/your-theme

    Note it does not return a trailing slash.

    – – –

    If you’re looking to get the path of the file you can remove the _uri portion of the above functions: get_template_directory() or for child theme support get_stylesheet_directory()

    Thread Starter slapsites

    (@slapsites)

    Hi, thanks for your explanation

    Just a few questions. Considering I make my themes from scratch. mine would not be a child theme but a theme.

    So I would use:

    get_template_directory_uri()

    Also what is the different between

    get_template_directory_uri()
    get_template_directory()

    Would my code look like:

    <link href=”<?php get_template_directory_uri()?>/css/screen.css” rel=”stylesheet”>

    or:

    <link href=”<?php get_template_directory()?>/css/screen.css” rel=”stylesheet”>

    Thanks for suggesting enqueue but honestly I can’t see justifying the added effort for what I’m trying to achieve. I could just add ??? or whatever before the path and use a search and replace all in the text editor with the correct code.

    Thanks again for your help.

    Regards,
    Chris

    Hello,

    The easiest way to find the difference between the two functions it to check the docs, also linked above.

    get_template_directory_uri() returns a URL to your theme.

    https://developer.www.ads-software.com/reference/functions/get_template_directory_uri/

    get_template_directory() returns a Path to your theme.

    – – – –

    So if you’re linking to assets you want to use get_template_directory_uri()

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Absolute link code in wordpress’ is closed to new replies.