Wordpress in subdirectory using wrong path for enqueing scripts - wordpress

I have been searching for a while but I haven't really seen anyone talk about this or found solutions.
At least nothing other then some hacks to help out from the functions.php
For a WordPress install at work I had to move the install into its own folder.
I used the guide here: https://wordpress.org/support/article/giving-wordpress-its-own-directory/#method-ii-with-url-change
Everything is working great and the folder structure is as expected:
- /
|-- Wordpress
|-- wp-content
|-- themes
|-- WebsiteTheme
So no worries there.
The functions.php has, for the moment, the following 2 lines in the wp-config.php
define( 'WP_HOME', 'https://dev.website.nl/' );
define( 'WP_SITEURL', 'https://dev.website.nl/wordpress' );
All in accordance with the guide from the official WordPress website.
But here is where things go weird.
When I try to Enqueue a script like this
$path = '/wp-content/themes/website/js/app.js';
wp_register_script('someName-'.$entry, $path, [], false, true);
wp_enqueue_script('someName-'.$entry);
the path eventually is set as
/wordpress/wp-content/themes/website/js/app.js
which of course won't work.
It looks like the path is pre-fixed with the SITEURL instead of the HOME url.
Currently I am using a filter that hooks into the script_loader_tag and str_replace the "/wordpress" part out of the path if a conditional statement is true based on the name I gave the script.
But as I just followed the WordPress guide set this up, I thought this would not be an issue.
Any idea if I am missing something here?
Thanks

The second parameter of wp_register_script() accepts $src as the full URL of the script OR the path of the script relative to the WordPress root directory.
To prevent this issue caused by the latter default, you'd probably be better off passing the full URL.
I think you can do so by prepending get_template_directory_uri() or get_stylesheet_directory_uri().
E.g.:
$path = '/js/app.js';
wp_register_script('someName-'.$entry, get_template_directory_uri() . $path, [], false, true);

Related

Elementor Not Loading On WordPress Subdirectory Installation With Composer

WordPress in its own directory using Composer
Troubleshooting Elementor Infinite Loading Screen
Inspired by roots/bedrock, I started a project to install WordPress to its own subdirectory using Composer. Plugins and Themes are installed from WPackagist, separately from the core WordPress files, also using Composer.
I ran into an issue with Elementor, which was stupid simple to fix. However, when I was in the process of fixing it, my Stack Overflow query, 'wordpress composer “elementor” -visual', returned nothing interesting. So I set out to write a quick troubleshooting guide for my own oversight. I have been obsessed with using Composer for WordPress dependencies lately, so I'm excited to write about it.
Anyway, after setting up the WordPress core and plugins using Composer, and setting up my database credentials in wp-config, I can login to the dashboard just fine.
Here's the Issue
In the dashboard, I see an issue immediately; the Elementor icon is missing, showing a generic gear instead. Here is the first clue that Elementor cannot find its own assets.
Furthermore, when I try to create a new page in Elementor, I see only an infinite, white Loading screen. Additionally, the "Edit with Elementor" button is missing when creating new posts and pages.
Initial Debugging Methods
Turn on PHP debugging in wp-config.php -> Revealed nothing enlightening.
Remove all other plugins besides Elementor -> No changes.
Turn on Safe Mode in Elementor > Tools -> Revealed nothing enlightening.
What next?
I will tell you how I fixed this issue.
Developer Console Insights
To finally resolve this issue, I opened the developer console (CTRL+SHIFT+I) and inspected some of the 404 links in the Network tab. An issue was immediately apparent: the filepath was incorrect. My project's folder is called "subdir-wp-elementor", not "subdir-wordpress".
Here is what Elementor was trying (and failing) to load:
http://localhost/subdir-wordpress/app/plugins/elementor/assets/js/editor-modules.min.js?ver=2.9.12
Resolution: Update CONTENT_URL in wp-config
I realized that I had forgotten to update the CONTENT_URL definition in wp-config.php
My SITE_URL was http://localhost/subdir-wp-elementor/, but here was my CONTENT_URL:
// ========================
// Custom Content Directory
// ========================
define( 'WP_CONTENT_DIR', dirname( __FILE__ ) . '/app' );
define( 'WP_CONTENT_URL', 'http://' . $_SERVER['HTTP_HOST'] . '/subdir-wordpress/app' );
Setting the correct CONTENT_URL to the below allowed Elementor to properly load.
define( 'WP_CONTENT_URL', 'http://' . $_SERVER['HTTP_HOST'] . '/subdir-wp-elementor/app' );

Move wordpress to root

A friend of mine asked me to help him to move his website from website.com/wp to the root website.com/
Its Wordpress installation is automatized by some script on register.it
I followed this guide, I log on, I changed both WordPress and Site Address from website.com/wp to website.com and saved.
Then from the ftp, I copied and pasted all my public/www/wp one step higher to public/www/ but the site was down and I couldn't even reach website.com/wp-admin or website.com/wp-login.php
Then I restored one of the backups I had and I tried this other guide, I moved the index.php one step higher by changing
/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
to
/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp/wp-blog-header.php' );
And of course this didn't work neither..
Now I start also wondering if that one was the real Wordpress root, this is the structure
How can I make the website be reachable on the root?
I solved it.
I tried the whole time playing with the /public/www folder, while instead the real Wordpress root was under /web/

wordpress change themes location

Usually all the wordpress themes are uploaded and saved over the server say http://example.com/wp-content/themes/ . I am developing a plug-in to change this path to something like http://xyz.com/themeFolder/ . So i have to develop such a functionality where my wordpress installation will be on one server and the themes and plug-ins folders will be on another server.
Any help, will be highly appreciated.
Thanks in advance to all the genius people out there :)
Since Version 2.6, you can move the wp-content directory, which holds your themes, plugins, and uploads, outside of the WordPress application directory.
Set WP_CONTENT_DIR to the full local path of this directory (no trailing slash), e.g.
define( 'WP_CONTENT_DIR', $_SERVER['DOCUMENT_ROOT'] . '/blog/wp-content' );
Set WP_CONTENT_URL to the full URI of this directory (no trailing slash), e.g.
define( 'WP_CONTENT_URL', 'http://example/blog/wp-content');
SOURCE
Do not forget to check THIS page as well.

Blank Page Encountered When Moved Wp-content Directory Outside

I'm trying to install Wordpress with clean subversion repositories, as in http://codex.wordpress.org/Installing_WordPress_With_Clean_Subversion_Repositories. I have followed every step accordingly until the second last step, where it says 'You should now be able to view the site via the root URL (http://example.com)', and indeed I can see my website on, say, http://example.com.
Once I go to the final step of changing the wp-config.php though, http://example.com draws a blank page. I am quite sure the wp-config.php is the problem, i.e. Wordpress doesn't know that it should retrieve my wp-content contents from outside the core Wordpress directory. My directory structure is as such:
.htaccess
core/
custom/
wp-config.php
, where core/ holds the files checked out with Subversion, custom/ holds my themes/ and plugins/ directory, and wp-config.php contains the below lines:
define('WP_CONTENT_DIR', dirname(__FILE__) . '/custom');
define('WP_CONTENT_URL', 'http://example.com/custom');
I have tried other variations, like:
define('WP_CONTENT_DIR', $_SERVER['DOCUMENT_ROOT'] . '/custom');
define('WP_CONTENT_URL', 'http://example.com/custom');
I have also tried hardcoding the local path, like:
define('WP_CONTENT_DIR', '/home/MYUSERNAME/example.com/custom');
define('WP_CONTENT_URL', 'http://example.com/custom');
, but none of the above works. Then I renamed the custom/ folder to wp-content/, but again nothing. I even went into Dreamhost to prevent redirecting http://example.com to http://www.example.com and vice versa, but again to no avail. I don't believe core/ is the problem, because I can access the Dashboard via http://example.com/core/wp-login.php, and the Dashboard says it can't detect my themes, which confirms my suspicion that WP doesn't know to retrieve the files from the custom/ folder.
By now, I have run out of ideas as to where could the problem be. Can anyone experienced in this area please give me some advice? Thanks a million.
Ensure that your statements come BEFORE the call for wp-settings.php, which is usually around line 90. Wp-settings (or more accurately default-constants.php) checks if the paths and URLs have already been defined, so you want to make sure you define them beforehand.
If that isn't the issue then check if $_SERVER['DOCUMENT_ROOT'] is returning the values you expect; you might have issues if working in a sub-directory.

Wordpress: how to get plugin's folders permission

SOLVED: plugin_dir_path(--FILE--).'MyFolder';
I have built a wordpress plugin (wp-content/plugins/MyPlugin) and I would to check some folders permission inside the MyPlugin directory.
My problem is that I don't what Wordpress constant or what PHP function I should use to target on of these directories.
if( !is_writable(WP_PLUGIN_URL.'MyFolder') )
{
echo 'NOK'.'<hr>';
}
This piece of code always returns 'NOK' whereas I am 100% sure 'MyFolder' is actually writable
.
Can you give me some piece of advice here to get the right file in order to target any folder that would be in my plugin directory?
thanks

Resources