skip to Main Content

Putting a basic js arcade game in a single post on WP website with Newspaper theme – hosted locally by WAAMP on my laptop. In functions.php the javascript files links are added to the footer. The wp_upload_dir() derived path I use for those files is being over ridden somewhere and results in GET 404 (not found) error.

This path is overridden:

Upload URL: http://localhost/oka-digital/wp-content/uploads

By this path somehow:

GET http://localhost/arcade-js/app.js net::ERR_ABORTED 404 (Not Found)

How do I stop the override?

Here is the function.php code:

/ Single post addition of javascript for arcade game project
function wpb_hook_javascript_footer() {
    if (is_single ('317')) {

    // Get the upload directory information
    $upload_dir = wp_upload_dir();

    // Access specific information
    $upload_base_url = $upload_dir['baseurl']; // URL to the uploads directory
    $upload_path = $upload_dir['path'];         // Server path to the uploads directory
?>
        <script src="<?php $upload_base_url ?>/arcade-js/resources.js"></script>
        <script src="<?php $upload_base_url ?>/arcade-js/app.js"></script>
        <script src="<?php $upload_base_url ?>/arcade-js/engine.js"></script>
<?php
}
}
add_action('wp_footer', 'wpb_hook_javascript_footer');

Errors:

localhost/:1460 GET http://localhost/arcade-js/resources.js net::ERR_ABORTED 404 (Not Found)
localhost/:1461 GET http://localhost/arcade-js/app.js net::ERR_ABORTED 404 (Not Found)
localhost/:1462 GET http://localhost/arcade-js/engine.js net::ERR_ABORTED 404 (Not Found)

2

Answers


  1. Chosen as BEST ANSWER

    I had to abandon the wp_upload_dir() approach.

    This worked:

    In functions.php:

    Used a conditional to target the specific post by i.d. Used wp_enqueue_script, get_template_directory_uri() to load js files. Created an array of relative paths to images using get_template_directory_uri(). Made this array accessible to the js files using wp_localize_script. Set post relative image paths in the javascript files using this array.


  2. I believe you need to use

    $upload_base_url = wp_get_upload_dir()["basedir"];
    

    however you are already doing that meaning you have set the uploades directory earlier outside the scope of the code you supplied.

    More information can be found in the WordPress documentation here

    your code is getting the top level url not the uploads directory.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search