How do i locate wordpress plugin directory? - wordpress

I am trying to add a function from plugin 1(wp job manager) to plugin 2(woocommerce).
I have decided to do this by including the php file from plugin 1, however I am unable to locate the file directory. I have used:
include( plugin_dir_path( __FILE__ ) . 'wp-job-manager/includes/class-wp-job-manager-applications.php');
but it returns the following error:
Warning:
include(/home/content/p3pnexwpnas05_data02/78/2394078/html/wp-content/themes/listify-child/wp-job-manager/includes/class-wp-job-manager-applications.php): failed to open stream: No such file or directory in
/home/content/p3pnexwpnas05_data02/78/2394078/html/wp-content/themes/listify-child/functions.php
on line 77
Please advise me as I've been stuck on this issue for really long... Thanks!!!

Wordpress setups have a constant ABSPATH defined (look at the bottom lines of wp_config.php) which points to the full and absolute path of the Wordpress setup, so in your case echo ABSPATH; would return /home/content/p3pnexwpnas05_data02/78/2394078/html/.
For most installations, appending wp-content/plugins/ to that string would point you to your plugins directory.
However, in a Wordpress configuration one can also customize the wp-content and or plugins directory to their own preference, so building plugins on ABSPATH.'wp-content/plugins/ is not recommended. Unfortunately Wordpress simply doesn't have a get_absolute_pluginspath() function or something available. A trick would be to fetch the plugins-URL, and remove the site-URL from it, so the remaining data is wp-content/plugins/ (or whatever the user has made of it). In code:
$plugins_directory = ABSPATH.str_replace(site_url()."/","",plugins_url())."/";
Which in your case would return:
/home/content/p3pnexwpnas05_data02/78/2394078/html/wp-content/plugins/

You probably mean:
plugin_dir_path(__FILE__)
That gives you the directory path to the file that statement is in. So what that returns depends on where you run it. If you use this statement
include( plugin_dir_path(__FILE__) . 'wp-job-manager/includes/class-wp-job-manager-applications.php');
in the main plugin file for wp_job_manager (probably wp_job_manager.php), then plugin_dir_path(__FILE__) give the path of the directory that file is in (the plugin directory).
If you use it in some other file, you will need to adjust the rest of the path string accordingly.

Related

Get WordPress plugin directory URI

I want to include a WooCommerce file while running a plugin. Here is the code I am using,
include_once(include( content_url() .'/plugins/woocommerce/includes/wc-core-functions.php'));
This gives me below error,
include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /nas/content/staging/squatwolf/wp-content/plugins/wpfront-notification-bar/classes/class-wpfront-notification-bar.php on line 58
I understand its being caused because the content_url function is giving complete url instead of relative url. I also tried,
plugin_dir_url(__FILE__)
but it gives the url of the plugin I am in, not the wordpress plugin directory url.
you want like this : /home/user/var/www/wordpress/wp-content/plugins/?
try $dir = plugin_dir_path( __DIR__ );

Unable to access analytics.txt once uploaded, returns 404

I've been asked to add an analytics.txt file to a wordpress website so I've created the file and uploaded it to the server document root but when I go to it via the url www.examples.com/analytics.txt all I get is a 404 error.
I've checked the file permissions and I've cleared the wordpress cache but neither have helped.
Any ideas?
The folder structure is as follows:
wp-admin
wp-content
wp-includes
analytics.txt <-- added this file, but cannot seem to access it via a web browser
index.php
etc...
This is NOT the solution but it is a work-around while I carry on trying to figure out why wordpress won't allow me to access my file.
So if you're desperate and HAVE to get it sorted right now, here is what you could do, but I warn you, it's ugly! Open your index.php file and you should see something like this:
<?php
define('WP_USE_THEMES', true);
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
?>
Make a backup copy first and then add the wrapping if statement:
<?php
if ($_SERVER[REQUEST_URI] == '/analytics.txt') {
die('Put the text that you were instructed to put into your analytics.txt file in here');
} else {
define('WP_USE_THEMES', true);
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
}
?>
Like I said, this is a dirty solution but when needs must and the client is getting impatient, this could help in the mean time.
Hoping that someone will have a better solution though!
I was able to get this to work with the following for the Sage theme:
Upload the analytics.txt through the theme administration panel
Copy the url of the upload and remove the hostname. For me it looked like this: app/uploads/2018/09/analytics.txt
Open the functions.php file and add the following:
function analytics_txt_rewrite(){
add_rewrite_rule('^analytics\.txt$','<route to your analytics.txt file>','top');
}
add_action('init','analytics_txt_rewrite');
Flush and regenerate the rewrite rules database: From WordPress Administration Screens, Select Settings -> Permalinks and just click Save Changes without any changes.

How should I protect a static Wordpress file directory from the outside world?

I'm taking over the admin of a WP site that serves static docs from a dedicated directory. The current directory resides on the top level (/public_html/docs) which seems susceptible to snooping. The site sits behind a login firewall.
The file directory contains >500 individual files, so uploading and hand-editing individual links seems absurd. (At least to me.)
Should I move this directory to a more secure location within the WP directory? Or, what is the preferred way to configure .htaccess?
At a minimum I would disable Directory Listing by using an .htaccess file that sits inside your /public_html/docs directory.
IndexIgnore *
Possibly a more secure method would be to move the docs directory outside your public_html directory. Then use a PHP script that can serve your document by passing a variable, such as www.site.com/serve_doc.php?name=xxxx.pdf.
Here is some code to accomplish this:
// get the file name
$file = $_GET['name'];
$dir = "/home/xxxx/docs/";
$fp = fopen($dir.$file, 'rb');
if(!$fp) { exit; }
// open the file
$finfo = finfo_open();
$filetype = finfo_file($finfo, $file, FILEINFO_MIME);
finfo_close($finfo);
$filename = $file;
// send the right headers
header("Cache-Control: ");// leave blank to avoid IE errors
header("Pragma: ");// leave blank to avoid IE errors
header("Content-Type: " .$filetype );
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Length: " . filesize($dir.$file));
// dump the file and stop the script
fpassthru($fp);
exit();
Of course you'll want to add some authentication to validate whether the user should be able to call this PHP script. One possible way would be to call the Wordpress function is_user_logged_in in that script before serving the file.

Wordpress - Get root directory?

How can I get the root directory of my site? I want to use it as the src for <img>.
For example If I install the wordpress on www.mysite.com/blog/, the root is /blog.
But If I install it to www.mysite.com/test/blog, the root is /test/blog.
I tried to use the snippet from this question, but it returns the whole path as seen in FTP. So instead of returning just /blog, it returns /home/public_html/blog which won't work if used as src in image tag.
Any solution? Thanks.
You may use site_url() (eventually with echo) to get absolute path with server name and directory. Also, have a look at wordpress documentation about similar functions & what they provide.
You may have better luck over at the Wordpress Stack Exchange site :)
And this suggestion to use ABSPATH didn't help on that thread? https://stackoverflow.com/a/2356467/413254
site_url() return the path with http, in some cases, it is not allowed if the server turns off url inclusion. You can use the function below to get the root path of wordpress installation folder:
function get_wp_installation()
{
$full_path = getcwd();
$ar = explode("wp-", $full_path);
return $ar[0];
}

WP Super Cache caching broken

I am having an ambiguous error. The path in the error is correct:
Warning! WP Super Cache caching broken! The script advanced-cache.php
could not load wp-cache-phase1.php.
Please edit /wp-content/advanced-cache.php
and make sure the path to
/wp-content/plugins/wp-super-cache/wp-cache-phase1.php
is correct.
What needs to be fixed?
The problem is that the constant is not defined until after the plugin loads. This error is possible if the line "require_once(ABSPATH . 'wp-settings.php');" is present in wp-config.php . WPCACHEHOME is probably being defined after this line, but needs to be defined above it:
define( 'WPCACHEHOME', '<site root>/wp-content/plugins/wp-super-cache/' ); //Added by WP-Cache Manager
require_once(ABSPATH . 'wp-settings.php');
http://wangweiqiang.net/warning-wp-super-cache-caching-broken-the-script-advanced-cache-php-could-not-load-wp-cache-phase1-php/
This worked like a charm for me...!
This is a permission issue, U should check that the paths mantioned in the error notice have 777 permisions, BUT, my advice on this issue is simply to NOT use this plugin, I had it installed on some of my site, and almost in all of them it caused errors on diffrent elements on my site.
I'm not saying that using this plugin will cause problems for sure, but this plugin is known as problematic, and for my opinion it doesn't justify itself.
Please use this in your wp-config.php file
define('WPCACHEHOME', dirname(__FILE__) . '/wp-content/plugins/wp-super-cache/');
before
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');

Resources