Home > UI/UX Development > JavaScript > WordPress – Jquery get dynamic urls(admin_url, template_directory)

WordPress – Jquery get dynamic urls(admin_url, template_directory)

This post was most recently updated on July 31st, 2024

WordPress provides a series of methods and global variables for getting the current url, template directory, plugin directory, includes directory, admin directory that saves us from using absolute paths. On the other hand JQuery under wordpress does not include such urls so we end up using absolute urls. 
A workaround for this can be uding the following code:

/* Add scripts to admin and frontend */
add_action(‘admin_enqueue_scripts’, ‘scripts’);
add_action(‘wp_enqueue_scripts’, ‘scripts’);
function scripts() {
?>

Similarly you can add other urls. If added in pages directly or under other hooks it throws an “Headers already sent” error.

This Article is TAGGED in , , , . BOOKMARK THE permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">